Advertisement
Traz_99

HTML Mail

Jan 18th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.40 KB | None | 0 0
  1. <?php
  2. //define the receiver of the email
  3. $to = 'youraddress@example.com';
  4. //define the subject of the email
  5. $subject = 'Test HTML email';
  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/alternative; boundary=\"PHP-alt-".$random_hash."\"";
  13. //define the body of the message.
  14. ob_start(); //Turn on output buffering
  15. ?>
  16. --PHP-alt-<?php echo $random_hash; ?>  
  17. Content-Type: text/plain; charset="iso-8859-1"
  18. Content-Transfer-Encoding: 7bit
  19.  
  20. Hello World!!!
  21. This is simple text email message.
  22.  
  23. --PHP-alt-<?php echo $random_hash; ?>  
  24. Content-Type: text/html; charset="iso-8859-1"
  25. Content-Transfer-Encoding: 7bit
  26.  
  27. <h2>Hello World!</h2>
  28. <p>This is something with <b>HTML</b> formatting.</p>
  29.  
  30. --PHP-alt-<?php echo $random_hash; ?>--
  31. <?
  32. //copy current buffer contents into $message variable and delete current output buffer
  33. $message = ob_get_clean();
  34. //send the email
  35. $mail_sent = @mail( $to, $subject, $message, $headers );
  36. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  37. echo $mail_sent ? "Mail sent" : "Mail failed";
  38. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement