Skip to content

Instantly share code, notes, and snippets.

@zachbrowne
Created October 31, 2011 06:47
Show Gist options
  • Save zachbrowne/1327042 to your computer and use it in GitHub Desktop.
Save zachbrowne/1327042 to your computer and use it in GitHub Desktop.
Send HTML Email with PHP
<?php
define("EmailNewLine", "\r\n");
define("EmailXMailer", "PHP-EMAIL, Samplephpcodes.com");
//** the default charset values for both text and HTML emails.
define("DefaultCharset", "iso-8859-1");
function htmlmail($to, $subject,$content,$cc,$bcc)
{
$theboundary = "-----" . md5(uniqid("EMAIL"));
$headers = "Date: " . date("r", time()) . EmailNewLine .
"From: $from" . EmailNewLine;
if(strlen(trim(strval($cc))) > 0)
$headers .= "CC: $cc" . EmailNewLine;
if(strlen(trim(strval($bcc))) > 0)
$headers .= "BCC: $bcc" . EmailNewLine;
$baseContentType = "multipart/mixed";
$headers .= "X-Mailer: " . EmailXMailer . EmailNewLine .
"MIME-Version: 1.0" . EmailNewLine .
"Content-Type: $baseContentType; " .
"boundary=\"$theboundary\"" . EmailNewLine . EmailNewLine;
$theemailtype = "text/html";
$Charset = DefaultCharset;
//** add the encoding header information for the body to the content.
$thebody = "--$theboundary" . EmailNewLine .
"Content-Type: $theemailtype; charset=$Charset" .
EmailNewLine . "Content-Transfer-Encoding: 8bit" .
EmailNewLine . EmailNewLine . $content .
EmailNewLine . EmailNewLine;
//** loop over the attachments for this email message and attach the files
//** to the email message body. Only if not multipart alternative.
$thebody .= "--$theboundary--";
return mail($to, $subject, $thebody, $headers);
}
$from="Your EMAIL ADDRESS"; // Change it to your Email Address
$to='TO ADDRESS'; // Change it to Sender Address
$subject= 'MY MAIL SUBJECT'; // Change it to your Mail Subject
$content="<font face='verdana' color='blue'><b>You just learned how to sent HTML EMAIL From <a href='<a class="linkclass" href="http://samplephpcodes.com">http://samplephpcodes.com</a>'>Sample PHP Codes</a></b></font>"; // change it to your Content
$cc=''; // change it to your cc
$bcc=''; // change it to your bcc
if (htmlmail($to, $subject,$content,$cc,$bcc) ) {
echo 'The email has been sent!<br/><a href="<a class="linkclass" href="http://samplephpcodes.com">http://samplephpcodes.com</a>">Sample PHP Codes</a>';
} else {
echo 'The email has failed!<br/><a href="<a class="linkclass" href="http://samplephpcodes.com">http://samplephpcodes.com</a>">Sample PHP Codes</a>';
}
?>
@asirihewage
Copy link

good

@NotClynt
Copy link

good

not good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment