send mail to bcc with mailgun with attached mpdf file

1k Views Asked by At

I'm sending attachment by following mpdf documentation mpdf E-mail a PDF file, and i'm using mailgun API to send mails.

My Code is

$header = "From: ".'donotreply@'.$domain." \r\n";
            $header = "Subject: ".'My Subject'." \r\n";
            $header .= "MIME-Version: 1.0\r\n";
            $header .= "Content-Type: multipart/mixed; boundary=\"".$solution->user->id."\"\r\n\r\n";
            $header .= "This is a multi-part message in MIME format.\r\n";
            $header .= "--".$solution->user->id."\r\n";
            $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
            $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            $header .= $body."\r\n\r\n";
            $header .= "--".$solution->user->id."\r\n";
            $header .= "Content-Type: application/pdf; name=\"MySetSolution-".$solution->user->id.".pdf\"\r\n";
            $header .= "Content-Transfer-Encoding: base64\r\n";
            $header .= "Content-Disposition: attachment; filename=\"MySetSolution-".$solution->user->id.".pdf\"\r\n\r\n";
            $header .= $content."\r\n\r\n";
            $header .= "--".$solution->user->id."--";
            $mailgun->sendMessage($domain, array(
                    'from' => 'donotreply@'.$domain,
                    'to' => $email_address,
                    'subject' => 'My Subject',
                    'text' => $body 
                ),
                $header
            );

I hv tried both code

$header .= 'Bcc: $emailList';

and

'bcc' => $email_address

but unable to send mail for bcc recipients.

Thanks for any idea if possible.

1

There are 1 best solutions below

0
On

According to the documentation you should be able to add attachments in an extra array:

$result = $mgClient->sendMessage($domain, array(
    'from'    => 'Excited User <YOU@YOUR_DOMAIN_NAME>',
    'to'      => '[email protected]',
    'cc'      => '[email protected]',
    'bcc'     => '[email protected]',
    'subject' => 'Hello',
    'text'    => 'Testing some Mailgun awesomness!',
    'html'    => '<html>HTML version of the body</html>'
), array(
    'attachment' => array('/path/to/file.txt', '/path/to/file.txt')
));

I don't think you can add them directly to the header, since the header is added before the body. It will probably result in an incorrect email.