send email with attachment using php
-
I used this code to send email with attachment using php, but there is something error in the attachment since I receive an email and the attachment appears in the content. before I use the same code and it worked successfully. why??? <?php // sending email with attachments function sendEmail($to,$from,$file,$ext){ $to = "[email protected]"; $from = "[email protected]"; $subject = "Translation Request"; $random_hash = md5(date('r', time())); $headers = "From: [email protected]\r\nReply-To: [email protected]"; $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; $attachment = chunk_split(base64_encode(file_get_contents("Test.doc"))); $output = " --PHP-mixed-$random_hash; Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash' --PHP-alt-$random_hash Content-Type: text/plain; charset='iso-8859-1' Content-Transfer-Encoding: 7bit Hello World! This is the simple text version of the email message. --PHP-alt-$random_hash Content-Type: text/html; charset='iso-8859-1' Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is the <b>HTML</b> version of the email message.</p> --PHP-alt-$random_hash-- --PHP-mixed-$random_hash Content-Type: application/doc; name=Test.doc Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --PHP-mixed-$random_hash--"; $send = @mail($to, $subject, $output, $headers); return $send; } ?> please help.
-
Answer:
You can either do it the way http://www.geekology.co.za/blog/2009/06/sending-emails-with-attachments-using-php-mail-function/ tutorial describes, or you can use one of the http://pear.php.net/manual/en/package.mail.mail-mime.php modules to send an email with an attachment the way http://pear.php.net/manual/en/package.mail.mail-mime.example.php tutorial describes. Using PEAR is probably a better option as it's easier to do. The only caveat is that PEAR isn't available on all hosts.
sasola at Stack Overflow Visit the source
Other answers
for what reason no use http://phpmailer.worxware.com/? example for attach: function mandaMail ($nombredest, $maildest, $asunto, $cuerpo) { require_once("mailer/class.phpmailer.php"); $mail = new PHPMailer(true); $mail->IsSMTP(); try { $mail->Host = "xxxx"; $mail->Port = 25; // smtp server $mail->SMTPAuth = true; $mail->Username = "xxxx"; // smtp username $mail->Password = "xxxx"; // smtp pass $mail->AddReplyTo("xxxx", "xxxx"); // email & name $mail->SetFrom("xxxx", "xxxx"); // similar to up value $mail->AddAddress($maildest, $nombredest); $mail->Subject = $asunto; $mail->MsgHTML(file_get_contents($cuerpo)); $mail->AddAttachment("xxxx", "xxxx"); // attachments directory, attachment name (ie: dir/blah.jpg, blah.jpg) $mail->Send(); } catch (phpmailerException $e) { echo $e->errorMessage(); } catch (Exception $e) { echo $e->getMessage(); } }
wiitohzjeh
I'm using this : function attachfile($file, $type = "application/octetstream") { if(!($fd = fopen($file, "r"))) { $this->errstr = "Error opening $file for reading."; return 0; } $_buf = fread($fd, filesize($file)); fclose($fd); $fname = $file; for($x = strlen($file); $x > 0; $x--) if($file[$x] == "/") $fname = substr($file, $x, strlen($file) - $x); // Convert to base64 becuase mail attachments are not binary safe. $_buf = chunk_split(base64_encode($_buf)); $this->attachments[$file] = "\n--" . $this->boundary . "\n"; $this->attachments[$file] .= "Content-Type: $type; name=\"$fname\"\n"; $this->attachments[$file] .= "Content-Transfer-Encoding: base64\n"; $this->attachments[$file] .= "Content-Disposition: attachment; " . "filename=\"$fname\"\n\n"; $this->attachments[$file] .= $_buf; return 1; }
Mohit Bumb
Related Q & A:
- How To Build Business Directory Using Php Mysql?Best solution by Stack Overflow
- how delete node in xml file using php?Best solution by Stack Overflow
- How to read csv file using php?Best solution by Stack Overflow
- How can I send a big attachment in my mail?Best solution by Yahoo! Answers
- Why can I send email but can not send email with attachments?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.