Created
October 31, 2011 06:47
-
-
Save zachbrowne/1327042 to your computer and use it in GitHub Desktop.
Send HTML Email with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good