469,082 Members | 1,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,082 developers. It's quick & easy.

PHP E-mail with file attachment

Hello Guys
would you know about php email with his attachment
Working on linux platform.......................................... .........
The code as below:

mail.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //define the receiver of the email
  3. $to = 'swanandkumar@gmail.com';
  4. //define the subject of the email
  5. $subject = 'Test email with attachment';
  6. //create a boundary string. It must be unique
  7. //so we use the MD5 algorithm to generate a random hash
  8. $random_hash = md5(date('r', time()));
  9. //define the headers we want passed. Note that they are separated with \r\n
  10. $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
  11. //add boundary string and mime type specification
  12. $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  13. //read the atachment file contents into a string,
  14. //encode it with MIME base64,
  15. //and split it into smaller chunks
  16. $attachment = chunk_split(base64_encode(file_get_contents('c:\\info.txt')));
  17. //define the body of the message.
  18. ob_start(); //Turn on output buffering
  19. ?>
  20. --PHP-mixed-<?php echo $random_hash; ?>
  21. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  22.  
  23. --PHP-alt-<?php echo $random_hash; ?>
  24. Content-Type: text/plain; charset="iso-8859-1"
  25. Content-Transfer-Encoding: 7bit
  26.  
  27. Hello World!!!
  28. This is simple text email message.
  29.  
  30. --PHP-alt-<?php echo $random_hash; ?>
  31. Content-Type: text/html; charset="iso-8859-1"
  32. Content-Transfer-Encoding: 7bit
  33.  
  34. <h2>Hello World!</h2>
  35. <p>This is something with <b>HTML</b> formatting.</p>
  36.  
  37. --PHP-alt-<?php echo $random_hash; ?>--
  38.  
  39. --PHP-mixed-<?php echo $random_hash; ?>
  40. Content-Type: application/zip; name="attachment.zip"
  41. Content-Transfer-Encoding: base64
  42. Content-Disposition: attachment
  43.  
  44. <?php echo $attachment; ?>
  45. --PHP-mixed-<?php echo $random_hash; ?>--
  46.  
  47. <?php
  48. //copy current buffer contents into $message variable and delete current output buffer
  49. $message = ob_get_clean();
  50. //send the email
  51. $mail_sent = @mail( $to, $subject, $message, $headers );
  52. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  53. echo $mail_sent ? "Mail sent" : "Mail failed";
  54. ?>
  55.  
Aug 16 '07 #1
1 2757
Atli
5,058 Expert 4TB
Hi.

Please post your code within [code] tags.

As for your problem.
Check out PHPMailer.
A tutorial on how to use attachments with PHPMailer can be found here.
Aug 17 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Mike | last post: by
1 post views Thread by Sam | last post: by
1 post views Thread by Trapulo | last post: by
1 post views Thread by Second Chance | last post: by
9 posts views Thread by deepaks85 | last post: by
2 posts views Thread by Erik Witkop | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.