PHP: Sending Email (Text/HTML/Attachments)


 / Published in: PHP
 

URL: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php?print=Y

  1. PHP: Sending Email (Text/HTML/Attachments)
  2.  
  3. Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.
  4.  
  5. So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.
  6.  
  7. * Sending a Simple Text Email
  8. * Sending HTML Email
  9. * Sending Email with Attachments
  10.  
  11. Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server.
  12.  
  13. Sending a Simple Text Email
  14.  
  15. At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise. Have a look at the example:
  16. <?php
  17. //define the receiver of the email
  18. $to = '[email protected]';
  19. //define the subject of the email
  20. $subject = 'Test email';
  21. //define the message to be sent. Each line should be separated with \n
  22. $message = "Hello World!\n\nThis is my first mail.";
  23. //define the headers we want passed. Note that they are separated with
  24.  
  25. $headers = "From: [email protected]
  26. Reply-To: [email protected]";
  27. //send the email
  28. $mail_sent = @mail( $to, $subject, $message, $headers );
  29. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  30. echo $mail_sent ? "Mail sent" : "Mail failed";
  31. ?>
  32.  
  33. As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don't receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.
  34.  
  35. Back to top
  36.  
  37. Sending HTML Email
  38.  
  39. The next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup. Have a look at the example:
  40. <?php
  41. //define the receiver of the email
  42. $to = '[email protected]';
  43. //define the subject of the email
  44. $subject = 'Test HTML email';
  45. //create a boundary string. It must be unique
  46. //so we use the MD5 algorithm to generate a random hash
  47. $random_hash = md5(date('r', time()));
  48. //define the headers we want passed. Note that they are separated with
  49.  
  50. $headers = "From: [email protected]
  51. Reply-To: [email protected]";
  52. //add boundary string and mime type specification
  53. $headers .= "
  54. Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
  55. //define the body of the message.
  56. ob_start(); //Turn on output buffering
  57. ?>
  58. --PHP-alt-<?php echo $random_hash; ?>
  59. Content-Type: text/plain; charset="iso-8859-1"
  60. Content-Transfer-Encoding: 7bit
  61.  
  62. Hello World!!!
  63. This is simple text email message.
  64.  
  65. --PHP-alt-<?php echo $random_hash; ?>
  66. Content-Type: text/html; charset="iso-8859-1"
  67. Content-Transfer-Encoding: 7bit
  68.  
  69. <h2>Hello World!</h2>
  70. <p>This is something with <b>HTML</b> formatting.</p>
  71.  
  72. --PHP-alt-<?php echo $random_hash; ?>--
  73. <?
  74. //copy current buffer contents into $message variable and delete current output buffer
  75. $message = ob_get_clean();
  76. //send the email
  77. $mail_sent = @mail( $to, $subject, $message, $headers );
  78. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  79. echo $mail_sent ? "Mail sent" : "Mail failed";
  80. ?>
  81.  
  82. In the preceding example we add one additional header of Content-type:multipart/alternative and boundary string that marks the different areas of the email. Note that the content type of the message itself is sent as a mail header, while the content types of the individual parts of the message are embedded in the message itself. This way, mail clients can decide which part of the message they want to display.
  83.  
  84. Sending Email with Attachment
  85.  
  86. The last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:
  87. <?php
  88. //define the receiver of the email
  89. $to = '[email protected]';
  90. //define the subject of the email
  91. $subject = 'Test email with attachment';
  92. //create a boundary string. It must be unique
  93. //so we use the MD5 algorithm to generate a random hash
  94. $random_hash = md5(date('r', time()));
  95. //define the headers we want passed. Note that they are separated with
  96.  
  97. $headers = "From: [email protected]
  98. Reply-To: [email protected]";
  99. //add boundary string and mime type specification
  100. $headers .= "
  101. Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
  102. //read the atachment file contents into a string,
  103. //encode it with MIME base64,
  104. //and split it into smaller chunks
  105. $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
  106. //define the body of the message.
  107. ob_start(); //Turn on output buffering
  108. ?>
  109. --PHP-mixed-<?php echo $random_hash; ?>
  110. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  111.  
  112. --PHP-alt-<?php echo $random_hash; ?>
  113. Content-Type: text/plain; charset="iso-8859-1"
  114. Content-Transfer-Encoding: 7bit
  115.  
  116. Hello World!!!
  117. This is simple text email message.
  118.  
  119. --PHP-alt-<?php echo $random_hash; ?>
  120. Content-Type: text/html; charset="iso-8859-1"
  121. Content-Transfer-Encoding: 7bit
  122.  
  123. <h2>Hello World!</h2>
  124. <p>This is something with <b>HTML</b> formatting.</p>
  125.  
  126. --PHP-alt-<?php echo $random_hash; ?>--
  127.  
  128. --PHP-mixed-<?php echo $random_hash; ?>
  129. Content-Type: application/zip; name="attachment.zip"
  130. Content-Transfer-Encoding: base64
  131. Content-Disposition: attachment
  132.  
  133. <?php echo $attachment; ?>
  134. --PHP-mixed-<?php echo $random_hash; ?>--
  135.  
  136. <?php
  137. //copy current buffer contents into $message variable and delete current output buffer
  138. $message = ob_get_clean();
  139. //send the email
  140. $mail_sent = @mail( $to, $subject, $message, $headers );
  141. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  142. echo $mail_sent ? "Mail sent" : "Mail failed";
  143. ?>
  144.  
  145. As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64, split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

Report this snippet  

You need to login to post a comment.