Send Text and HTML Email using PHP

Sending email from the script is a very useful and most used feature for the web application. The email functionality is used for many purposes – sending a welcome email on account registration, sending newsletter, contact or feedback form, etc. By sending an email from the script, the mail is sent dynamically to the recipient without manual interaction.

If your web application is built with PHP, the email can be sent easily from the script using a predefined function. The PHP mail() function is the easiest way to send an email from the code level. You can easily send text and HTML emails using the built-in mail() function in PHP. Generally, the text message is sent via email to the recipient. If you want to send a nice formatted email, HTML content needs to be added to the message body of the email. In this tutorial, we will show you how to send text and HTML emails using PHP mail() function.

Send Text Email with PHP

The following code sends an email from the script with text message content using PHP.

Use the PHP mail() function and pass the required parameters.

  • to – Recipient email address.
  • subject – Subject of the email.
  • message – Message to be sent.
  • headers – From, Cc, Bcc, Content-type headers.
$to 'user@example.com'; 
$from 'sender@email.com';
$fromName 'Sender_Name';

$subject "Send Text Email with PHP by CodexWorld";

$message "First line of text\nSecond line of text";

// Additional headers
$headers 'From: '.$fromName.'<'.$from.'>';

// Send email
if(mail($to$subject$message$headers)){
   echo 
'Email has sent successfully.';
}else{
   echo 
'Email sending failed.';
}

Send HTML email in PHP

The following code sends an email from the script with HTML message content using PHP.

  • Use the PHP mail() function and provide the required parameters.
    • to – Recipient email address.
    • subject – Subject of the email.
    • message – Message to be sent.
    • headers – From, Cc, Bcc, Content-type headers.
  • The content-type header is mandatory for sending HTML formatted email.
  • The additional headers are used for adding From, Cc, Bcc, etc.
  • $htmlContent variable holds the HTML contents of the email.
$to 'user@example.com'; 
$from 'sender@example.com';
$fromName 'SenderName';

$subject "Send HTML Email in PHP by CodexWorld";

$htmlContent '
    <html>
    <head>
        <title>Welcome to CodexWorld</title>
    </head>
    <body>
        <h1>Thanks you for joining with us!</h1>
        <table cellspacing="0" style="border: 2px dashed #FB4314; width: 100%;">
            <tr>
                <th>Name:</th><td>CodexWorld</td>
            </tr>
            <tr style="background-color: #e0e0e0;">
                <th>Email:</th><td>contact@codexworld.com</td>
            </tr>
            <tr>
                <th>Website:</th><td><a href="http://www.codexworld.com">www.codexworld.com</a></td>
            </tr>
        </table>
    </body>
    </html>'
;

// Set content-type header for sending HTML email
$headers "MIME-Version: 1.0" "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" "\r\n";

// Additional headers
$headers .= 'From: '.$fromName.'<'.$from.'>' "\r\n";
$headers .= 'Cc: welcome@example.com' "\r\n";
$headers .= 'Bcc: welcome2@example.com' "\r\n";

// Send email
if(mail($to$subject$htmlContent$headers)){
    echo 
'Email has sent successfully.';
}else{
   echo 
'Email sending failed.';
}

Send Email with HTML Content from File

If you want to send email with large HTML content, it’s always a good idea to separate message content in HTML file. This functionality is very useful when you want to use the dynamic email template for sending mail.

  • Put the HTML content of the message in an HTML file (email_template.html).
  • Use file_get_contents() function to retrieve HTML content from the file.
// Get HTML contents from file 
$htmlContent file_get_contents("email_template.html");

Creating a Simple Contact Form with PHP

Conclusion

This example script helps you to send HTML email with Cc or Bcc address in PHP. You can use this code to send multiple emails dynamically from the script using PHP. Specify the additional headers to send email with attachment in PHP.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

30 Comments

  1. Acscaar Said...
  2. Michael Said...
  3. Kunal Said...
  4. Fahad Ullah Said...
  5. Vijay Bahuguna Said...
  6. Ville Mononen Said...
  7. Muhammad Umar Said...
  8. Aceout Said...
  9. Andreas Said...
    • CodexWorld Said...
  10. Chris Said...
  11. WebPhenom Said...
  12. Liza Said...
  13. Nida Akram Said...
  14. Amit Kumar Mishra Said...
  15. Muzammil Said...
  16. Jeff Said...
  17. Anas Said...
    • CodexWorld Said...
  18. Gautam Nagraj Said...
  19. Ankit Yaduwanshi Said...
  20. Kevin Said...
  21. Hiepnh Said...
  22. Lutfu Can Said...
  23. Kamlesh Said...
  24. Amit Tiwari Said...
  25. Cp Said...
  26. Carl Said...

Leave a reply

keyboard_double_arrow_up