Email Attachment


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/local/bin/php
  2. <?php
  3.  
  4. $to = $argv[1];
  5. $subject = $argv[2];
  6. $attachmentName = $argv[3];
  7. $messageBoundry = md5(date('r', time()));
  8. $headers = "From: [email protected]
  9. Reply-To: [email protected]";
  10. $headers .= "
  11. Content-Type: multipart/mixed; boundary=\"PHP-mixed-" . $messageBoundry . "\"";
  12. $attachment = chunk_split(base64_encode(file_get_contents($attachmentName)));
  13. ob_start(); //Turn on output buffering
  14.  
  15. ?>
  16. --PHP-mixed-<?= $messageBoundry ?>
  17. Content-Type: multipart/alternative; boundary="PHP-alt-<?= $messageBoundry; ?>"
  18.  
  19. --PHP-alt-<?=$messageBoundry?>
  20. Content-Type: text/plain; charset="iso-8859-1"
  21. Content-Transfer-Encoding: 7bit
  22.  
  23. --PHP-alt-<?= $messageBoundry ?>
  24. Content-Type: text/html; charset="iso-8859-1"
  25. Content-Transfer-Encoding: 7bit
  26.  
  27. <h3><?= $subject ?></h3>
  28.  
  29. --PHP-alt-<?= $messageBoundry ?>
  30.  
  31. <?= $subject ?>
  32.  
  33. --PHP-mixed-<?= $messageBoundry ?>
  34. Content-Type: application/zip; name="<?= $attachmentName ?>"
  35. Content-Transfer-Encoding: base64
  36. Content-Disposition: attachment
  37.  
  38. <?= $attachment ?>
  39.  
  40. --PHP-mixed-<?= $messageBoundry; ?>
  41.  
  42. <?php
  43.  
  44. $message = ob_get_clean();
  45. mail( $to, $subject, $message, $headers );
  46.  
  47. ?>

URL: http://code.cshaiku.com/code_php_email_attachment.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.