Return to Snippet

Revision: 2990
at June 1, 2007 21:44 by IanLewis


Updated Code
/**
 * Sends mail to the e-mail address given.
 * Supports attaching files and multiple encodings.
 * @param string The email address of the recipient
 * @param string The name to include in the from header
 * @param string The e-mail address to include in the from header
 * @param string the e-mail subject
 * @param string the e-mail body.
 * @param boolean true if the body is html or false if plain text.
 * @param string a path to a file on the server to attach to the e-mail. Null or an empty string indicates that there is no attachment.
 * @param string the encoding with which to encode the subject, from, and body.
 */
function SendMail($emailaddress,
                   $from, $fromaddress,
                   $emailsubject="",
                   $body="", $html = true,
                   $attachment="",
                   $encoding="utf-8") {//{{{
  
  # Is the OS Windows or Mac or Linux
  if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
    $eol="
";
  } elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
    $eol="\r";
  } else {
    $eol="\n";
  } 
  
  //set subject encoding
  if (!empty($emailsubject)) {
    $emailsubject = encode_mail_string($emailsubject, $encoding);
  }
  $from = encode_mail_string($from, $encoding);
  if ($encoding != "utf-8" && !empty($body)) {
    $body = mb_convert_encoding($body, $encoding, "utf-8");
  }
  
  $msg = "";
  
  # Common Headers
  $headers .= "From: ".$from." <".$fromaddress.">".$eol;
  $headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol;
  $headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol;    // these two to set reply address
  $headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
  $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters
  $headers .= 'MIME-Version: 1.0'.$eol;
  
  if (!empty($attachment)) {
    //send multipart message
    # Boundry for marking the split & Multitype Headers
    $mime_boundary=md5(time());
    $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
    
    # File for Attachment
    
    $f_name = $attachment;
    $handle=fopen($f_name, 'rb');
    $f_contents=fread($handle, filesize($f_name));
    $f_contents=chunk_split(base64_encode($f_contents));//Encode The Data For Transition using base64_encode();
    $f_type=filetype($f_name);
    fclose($handle);
    
    # Attachment
    $msg .= "--".$mime_boundary.$eol;
    $msg .= "Content-Type: application/jpeg; name=\"".$file."\"".$eol;
    $msg .= "Content-Transfer-Encoding: base64".$eol;
    $msg .= "Content-Disposition: attachment; filename=\"".basename($attachment)."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $f_contents.$eol.$eol;
    # Setup for text OR html
    $msg .= "Content-Type: multipart/alternative".$eol;
    
    
    $contentType = "text/plain";
    if ($html) {
      $contentType = "text/html";
    }
  
    # Body
    $msg .= "--".$mime_boundary.$eol;
    $msg .= "Content-Type: ".$contentType."; charset=\"".$encoding."\"".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $body.$eol.$eol;
    
    # Finished
    $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.
  } else {
    $headers .= "Content-Type: text/plain; charset=\"".$encoding."\"".$eol;
    $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $body.$eol.$eol;
  }
  
  // SEND THE EMAIL
  //LogMessage("Sending mail to: ".$emailaddress." => ".$emailsubject);
  
  //ini_set(sendmail_from, '[email protected]');  // the INI lines are to force the From Address to be used !
  ini_set(sendmail_from, $fromaddress); //needed to hopefully get by spam filters.
  $success = mail($emailaddress, $emailsubject, $msg, $headers);
  ini_restore(sendmail_from);
  
  return $success;
}//}}}

Revision: 2989
at May 21, 2007 20:49 by IanLewis


Initial Code
/**
 * Sends mail to the e-mail address given.
 * Supports attaching files and multiple encodings.
 * @param string The email address of the recipient
 * @param string The name to include in the from header
 * @param string The e-mail address to include in the from header
 * @param string the e-mail subject
 * @param string the e-mail body.
 * @param boolean true if the body is html or false if plain text.
 * @param string a path to a file on the server to attach to the e-mail. Null or an empty string indicates that there is no attachment.
 * @param string the encoding with which to encode the subject, from, and body.
 */
function SendMail($emailaddress,
                   $from, $fromaddress,
                   $emailsubject="",
                   $body="", $html = true,
                   $attachment="",
                   $encoding="utf-8") {//{{{
  
  # Is the OS Windows or Mac or Linux
  if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
    $eol="
";
  } elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
    $eol="\r";
  } else {
    $eol="\n";
  } 
  
  //set subject encoding
  if (!empty($emailsubject)) {
    $emailsubject = encode_mail_string($emailsubject, $encoding);
  }
  $from = encode_mail_string($from, $encoding);
  if ($encoding != "utf-8" && !empty($body)) {
    $body = mb_convert_encoding($body, $encoding, "utf-8");
  }
  
  $msg = "";
  
  # Common Headers
  $headers .= "From: ".$from." <".$fromaddress.">".$eol;
  $headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol;
  $headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol;    // these two to set reply address
  $headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
  $headers .= "X-Mailer: PHP v".phpversion().$eol;          // These two to help avoid spam-filters
  $headers .= 'MIME-Version: 1.0'.$eol;
  
  if (!empty($attachment)) {
    //send multipart message
    # Boundry for marking the split & Multitype Headers
    $mime_boundary=md5(time());
    $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
    
    # File for Attachment
    
    $f_name = $attachment;
    $handle=fopen($f_name, 'rb');
    $f_contents=fread($handle, filesize($f_name));
    $f_contents=chunk_split(base64_encode($f_contents));//Encode The Data For Transition using base64_encode();
    $f_type=filetype($f_name);
    fclose($handle);
    
    # Attachment
    $msg .= "--".$mime_boundary.$eol;
    $msg .= "Content-Type: application/jpeg; name=\"".$file."\"".$eol;
    $msg .= "Content-Transfer-Encoding: base64".$eol;
    $msg .= "Content-Disposition: attachment; filename=\"".basename($attachment)."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $f_contents.$eol.$eol;
    # Setup for text OR html
    $msg .= "Content-Type: multipart/alternative".$eol;
    
    
    $contentType = "text/plain";
    if ($html) {
      $contentType = "text/html";
    }
  
    # Body
    $msg .= "--".$mime_boundary.$eol;
    $msg .= "Content-Type: ".$contentType."; charset=\"".$encoding."\"".$eol;
    $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $body.$eol.$eol;
    
    # Finished
    $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.
  } else {
    $headers .= "Content-Type: text/plain; charset=\"".$encoding."\"".$eol;
    $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
    $msg .= $body.$eol.$eol;
  }
  
  # SEND THE EMAIL
  LogMessage("Sending mail to: ".$emailaddress." => ".$emailsubject);
  
  //ini_set(sendmail_from, '[email protected]');  // the INI lines are to force the From Address to be used !
  ini_set(sendmail_from, $fromaddress); //needed to hopefully get by spam filters.
  $success = mail($emailaddress, $emailsubject, $msg, $headers);
  ini_restore(sendmail_from);
  
  return $success;
}//}}}

Initial URL


Initial Description
Sends mail to the e-mail address specified. Supports attaching files, multi-part message, and e-mail encodings. Works well with Asian cell phones.

Initial Title
Send multi-part encoded mail with attachments.

Initial Tags
mail

Initial Language
PHP