Return to Snippet

Revision: 38229
at April 6, 2011 13:46 by relish


Updated Code
<?php
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Test HTML email';
 
    // Generate a random boundary string 
    $mime_boundary = '_x'.sha1(time()).'x'; 
 
    // Using the heredoc syntax to declare the headers 
    $headers = <<<HEADERS
From: Test <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="PHP-alt$mime_boundary"
HEADERS;
 
    // Use our boundary string to create plain text and HTML versions 
    $message = <<<MESSAGE
--PHP-alt$mime_boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

This Is a Plain Text Email

This message has no HTML. http://w3schools.com

--PHP-alt$mime_boundary
Content-type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

<html>
<body>
<h1>This Is an HTML Email</h1>
<p>
This message is composed in <a href="http://w3schools.com">HTML</a>.
</p>
</body>
</html>
--PHP-alt$mime_boundary--
MESSAGE;
 
    // Send the message 
    if(!mail($to, $subject, $message, $headers)) 
    { 
        // If the mail function fails, return an error message 
        echo "Something went wrong!"; 
    } 
    else 
    { 
        // Return a success message if nothing went wrong 
        echo "Message sent successfully. Check your email!"; 
    } 
?>

Revision: 38228
at December 29, 2010 02:34 by relish


Initial Code
<?php
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Test HTML email';
 
    // Generate a random boundary string 
    $mime_boundary = '_x'.sha1(time()).'x'; 
 
    // Using the heredoc syntax to declare the headers 
    $headers = <<<HEADERS
From: Test <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="PHP-alt$mime_boundary"
HEADERS;
 
    // Use our boundary string to create plain text and HTML versions 
    $message = <<<MESSAGE
--PHP-alt$mime_boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

This Is a Plain Text Email

This message has no HTML. http://w3schools.com

--PHP-alt$mime_boundary
Content-type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

<html>
<body>
<h1>This Is an HTML Email</h1>
<p>
This message is composed in <a href="http://w3schools.com">HTML</a>.
</p>
</body>
</html>
--PHP-alt$mime_boundary--
MESSAGE;
 
    // Send the message 
    if(!mail($to, $subject, $message, $headers)) 
    { 
        // If the mail function fails, return an error message 
        echo "Something went wrong!"; 
    } 
    else 
    { 
        // Return a success message if nothing went wrong 
        echo "Message sent successfully. Check your email!"; 
    } 
?>

Initial URL


Initial Description


Initial Title
PHP: Send Plaintext + HTML Email

Initial Tags
email, php, html

Initial Language
PHP