Send Beautiful HTML Email using PHP


Using PHP mail() function we can easily send the text email. But most of the cases we need to send nice HTML email to the users. Like registration welcome email, contact request submission email, invoice email etc. You can easily send the HTML email using PHP mail() function with our PHP HTML email script.

We have created a welcome email HTML template and sent to the email. From the Demo link you can receive the HTML email template into inbox. Download full source code and HTML email template from the Download link.

beautiful-html-email-template-by-codexworld

PHP Code:

Content-type header is mandatory for sending HTMl email. The additional headers are used for adding From, Cc, Bcc etc. $htmlContent variable hold the HTML contents of the email.

<?php
$to 
'user@example.com';
$subject "Beautiful HTML Email using 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: 300px; height: 200px;">
            <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: CodexWorld<sender@example.com>' "\r\n";
$headers .= 'Cc: welcome@example.com' "\r\n";
$headers .= 'Bcc: welcome2@example.com' "\r\n";

// Send email
if(mail($to,$subject,$htmlContent,$headers)):
    
$successMsg 'Email has sent successfully.';
else:
    
$errorMsg 'Email sending fail.';
endif;
?>

If you want to add huge HTML contents you can put the HTML contents into a file (email_template.html) and get the file contents using file_get_contents() function.

$htmlContent file_get_contents("email_template.html");

11 Comments

  1. Anas Said...
    • CodexWorld Said...
  2. Gautam Nagraj Said...
  3. Ankit Yaduwanshi Said...
  4. Kevin Said...
  5. Hiepnh Said...
  6. Lutfu Can Said...
  7. Kamlesh Said...
  8. Amit Tiwari Said...
  9. Cp Said...
  10. Carl Said...

Leave a reply

Connect With CodexWorld