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

时间:2017-02-08 08:20:29

标签: php phpmailer

我需要在电子邮件中发送pdf并在gmail,yahoo等中收到罚款,但是它在Microsoft Outlook中提供问题,邮件正在作为outlook中的附件。请检查下面的代码并告诉我我缺少的内容: -

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;
    }
}

0 个答案:

没有答案