Created
November 12, 2009 17:26
-
-
Save evaisse/233089 to your computer and use it in GitHub Desktop.
html email php function
This file contains 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 #-*- coding: utf-8 -*- | |
/*** | |
* | |
* @return bool true if email sent, false otherwise | |
*/ | |
function email($to, $from, $subject, $html, $attachments=array(), | |
$bcc=false, $reply=false ) | |
{ | |
foreach(array('to','bcc','from','reply','subject') as $varname) | |
{ | |
/** | |
* remove dangerous chars for headers from a string( \n \l \r ) | |
*/ | |
$$varname = strtr($$varname, | |
array("\n" => '',"\l" => '',"\r" => '')); | |
} | |
/* | |
Generate boundary & headers | |
*/ | |
$reply = $reply ? $reply : $from; | |
$text = strip_tags($html); | |
$headers = array(); | |
$headers[] = "X-Priority: 1 (Higuest)"; | |
$headers[] = "X-MSMail-Priority: High"; | |
$headers[] = "Importance: High"; | |
$headers[] = 'From: '.$from; | |
$headers[] = 'Return-Path: '.$reply; | |
if($bcc) $headers[] = 'Bcc: '.$reply; | |
$headers[] = 'MIME-Version: 1.0'; | |
$msg_boundary = '-------=part' . md5(uniqid(mt_rand())); | |
$headers[] = 'Content-Type: multipart/mixed; boundary="'.$msg_boundary.'"'; | |
$headers[] = 'This is a multi-part message in MIME format.'; | |
$headers[] = '--'.$msg_boundary; | |
/* | |
text | |
*/ | |
$boundary = '-------=part' . md5(uniqid(mt_rand())); | |
$headers[] = 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'; | |
$headers[] = '--'.$boundary; | |
$headers[] = 'Content-Type: text/plain; charset="utf-8"'; | |
$headers[] = 'Content-Transfer-Encoding: quoted-printable'; | |
$headers[] = 'Content-Disposition: inline'; | |
$headers[] = ""; | |
$headers[] = $text; | |
$headers[] = ""; | |
/* | |
html part | |
*/ | |
$headers[] = '--'.$boundary; | |
$headers[] = 'Content-Type: text/html; charset="utf-8"'; | |
$headers[] = 'Content-Transfer-Encoding: quoted-printable'; | |
$headers[] = 'Content-Disposition: inline'; | |
$headers[] = ""; | |
$headers[] = $html; | |
$headers[] = ""; | |
$headers[] = ''; | |
$headers[] = '--'.$boundary.'--'; | |
$headers[] = ''; | |
/* | |
handle attachments | |
*/ | |
if( !is_array($attachments) ) | |
$attachments = array(); | |
foreach( $attachments as $file ) | |
{ | |
if(!file_exists($file)) | |
trigger_error('[email] file '.$file.' does not exits'); | |
$mimetype = mimetype($file); | |
$name = urlize(basename($file)); | |
$headers[] = '--'.$msg_boundary; | |
$headers[] = 'Content-Type: '.$mimetype.'; name="'.$name.'"'; | |
$headers[] = 'Content-Transfer-Encoding: base64'; | |
$headers[] = 'Content-Disposition:inline; ' | |
.'filename="'.$name.'"'; | |
$headers[] = ''; | |
$headers[] = chunk_split(base64_encode(file_get_contents($file))); | |
$headers[] = ''; | |
} | |
$headers[] = ''; | |
$headers[] = '--'.$msg_boundary.'--'; | |
$headers[] = ''; | |
return mail($to,$subject,'',join("\n",$headers) ); | |
} | |
//- email() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment