1

I need to send a pdf in email and its receiving fine in gmail, yahoo etc but its giving problem in Microsoft outlook, mail body going as attachment in outlook.Please check below code and tell me what i am missing:--

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;
    }
}
  • Possible duplicate of [Send File Attachment from Form Using phpMailer and PHP](http://stackoverflow.com/questions/11764156/send-file-attachment-from-form-using-phpmailer-and-php) – Ali.Mojtahed Feb 08 '17 at 08:24
  • 1
    No there is only one browse field in form and the question is about sending mail body as attachment in outlook only. So please read the issue properly once. – Senior PHP Developer Feb 08 '17 at 08:29
  • You can't have an 8bit CTE part inside a 7bit multipart; the encoding on the multipart applies to all parts it contains. A base64 CTE doesn't require an 8bit container as its 7bit-safe. It might help if you posted an example of a generated message. Of course it would also help if you stopped reinventing the wheel, used a library that fixed this years ago and is known to work, like [PHPMailer](https://github.com/PHPMailer/PHPMailer) that you tagged this question with. – Synchro Feb 08 '17 at 08:58
  • yes, i know about PHPMailer and i used it many time but i really wants to understand the problem with core code that is why i posted this. – Senior PHP Developer Feb 08 '17 at 11:04

0 Answers0