使用应在outlook中打开的PHP Mail()发送附件

使用应在outlook中打开的PHP Mail()发送附件,php,phpmailer,Php,Phpmailer,我需要在电子邮件中发送pdf,在gmail、yahoo等中接收罚款,但在Microsoft outlook中发送问题,邮件正文在outlook中作为附件发送。请检查以下代码并告诉我缺少什么:-- 函数sendMailWithAttachment($fromName='fromName',$frommail,$recipient,$mail\u body,$subject,$filenamewithpath='',$cc='',$bcc=''){ $fromEmail=info@domain.co

我需要在电子邮件中发送pdf,在gmail、yahoo等中接收罚款,但在Microsoft outlook中发送问题,邮件正文在outlook中作为附件发送。请检查以下代码并告诉我缺少什么:--

函数sendMailWithAttachment($fromName='fromName',$frommail,$recipient,$mail\u body,$subject,$filenamewithpath='',$cc='',$bcc=''){
$fromEmail=info@domain.com';
$filename=explode(“/”,$filenamewithpath);
$filename=$filename[计数($filename)-1];
$content=file\u get\u contents($filenamewithpath);
$content=chunk_split(base64_encode($content));
//发送混合内容时需要随机散列
$separator=md5(time());
$separator=“==Multipart_Boundary_x{$separator}x”;
//回车类型(RFC)
$eol=“\n”;
//$eol=“\r\n”;
//主标题(多部分必填)
$headers=“From:”.$fromName.\r\n”;
如果(!空($cc)){
$headers.=“抄送:\r\n”;
}
如果(!空($bcc)){
$headers.=“密件抄送:\r\n”;
}
$headers.=“MIME版本:1.0”。$eol;
$headers.=“内容类型:多部分/混合;边界=\”“.$separator.\”“.$eol;
$headers.=“内容传输编码:7bit”。$eol;
$headers.=“这是一条MIME编码的消息。”。$eol;
//信息
$body=“-->$分隔符.$eol;
$body.=“内容传输编码:7bit”。$eol.$eol;
//$body.=“这是一条MIME编码的消息。”。$eol;
//信息
$body.=“-”$separator.$eol;
$body.=“内容类型:文本/html;字符集=\“iso-8859-1\”。$eol;
$body.=“内容传输编码:8位”。$eol.$eol;
$body.=$mail\U body.$eol;
/*$body=“-->$分隔符.$eol;
$body.=“内容类型:文本/普通;字符集=\“iso-8859-1\”。$eol;
$body.=“内容传输编码:base64”。$eol;
$body.=$mail\U body.$eol*/
如果($filename!=''&&$content!=''){
//附件:
$body.=“-”$separator.$eol;
$body.=“内容类型:应用程序/八位字节流;名称=\”“.$filename.\”“.$eol;
$body.=“内容传输编码:base64”。$eol;
$body.=“内容处置:附件;文件名=\”.$filename.\”.$eol;
$body.=$content.$eol;
$body.=“--”“分隔符”“--”;
}
//寄信
if(邮件($recipient、$subject、$body、$headers)){
返回true;
}否则{
返回false;
}
}

可能与否重复表单中只有一个浏览字段,问题是仅将邮件正文作为outlook中的附件发送。因此,请仔细阅读一次该问题。您不能在7位多部件中包含8位CTE部件;多部分上的编码适用于它包含的所有部分。base64 CTE不需要8位容器作为7位安全容器。如果您发布一个生成消息的示例,可能会有所帮助。当然,如果你停止重新发明轮子,使用一个在几年前修复过并且已知有效的库,也会有所帮助,就像你在这个问题上加了标签一样。是的,我知道PHPMailer,我使用过很多次,但我真的很想了解核心代码的问题,这就是我发布此邮件的原因。可能与否重复表单中只有一个浏览字段,问题是仅在outlook中以附件形式发送邮件正文。因此,请仔细阅读一次该问题。您不能在7位多部件中包含8位CTE部件;多部分上的编码适用于它包含的所有部分。base64 CTE不需要8位容器作为7位安全容器。如果您发布一个生成消息的示例,可能会有所帮助。当然,如果你停止重新发明轮子,使用一个在几年前修复的库,并且已知它可以工作,就像你在这个问题上加了标签一样,这也会有所帮助。是的,我知道PHPMailer,我使用过很多次,但我真的想了解核心代码的问题,这就是我发布这个问题的原因。
function sendMailWithAttachment($fromName = 'from name', $fromEmail, $recipient, $mail_body, $subject, $filenamewithpath = '', $cc='',$bcc=''){



    $fromEmail  =   'info@domain.com';


    $filename   =   explode('/', $filenamewithpath);
    $filename   =   $filename[count($filename) - 1];

    $content = file_get_contents($filenamewithpath);
    $content = chunk_split(base64_encode($content));

    // a random hash will be necessary to send mixed content
    $separator = md5(time());
    $separator = "==Multipart_Boundary_x{$separator}x"; 

    // carriage return type (RFC)
    $eol = "\n";
    //$eol = "\r\n";

    // main header (multipart mandatory)
    $headers   = "From: ". $fromName . " <" . $fromEmail . ">\r\n";
    if(!empty($cc)){
        $headers    .=  "Cc: <" . $cc . ">\r\n";
    }
    if(!empty($bcc)){
        $headers    .=  "Bcc: <" . $bcc . ">\r\n";
    }
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
    $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    $headers .= "This is a MIME encoded message." . $eol;

    // message
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    //$body .= "This is a MIME encoded message.".$eol;

    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $mail_body.$eol;

    /* $body  = "--" . $separator . $eol;
    $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= $mail_body . $eol; */

    if($filename != '' && $content != ""){
        // attachment
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol;
        $body .= "Content-Disposition: attachment; filename=\"".$filename."\"" . $eol;
        $body .= $content . $eol;
        $body .= "--" . $separator . "--";
    }

    //SEND Mail
    if(mail($recipient, $subject, $body, $headers)){

        return true;
    }else{
        return false;
    }
}