PHP 发送带附件的多部分编码邮件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 发送带附件的多部分编码邮件。相关的知识,希望对你有一定的参考价值。

/**
 * 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, 'from@me.com');  // 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;
}//}}}

以上是关于PHP 发送带附件的多部分编码邮件。的主要内容,如果未能解决你的问题,请参考以下文章

发送带附件的 PHP HTML 邮件

PHP - 发送带附件的电子邮件不显示邮件内容

php 发送带附件邮件的实现代码

使用 perl 发送带有文件附件的多部分文本/html 替代消息,

python - 发送带各种类型附件的邮件

jquery 发送带附件的邮件