How To Fix Multiple Attachments Problem in PHPMailer?

Before sending email with multiple attachments, you need to fix some bugs in the PHPMailer library. You can download the PHPMailer library from https://github.com/PHPMailer/PHPMailer

To be able to send multiple attachments in email via PHPMailer, following patch has to be applied to 'class.phpmailer.php' (methods "AddAttachment" and "AddStringAttachment"))
ORIGINAL CODE:
$this->attachment[] = array(
0 => $path,
1 => $filename,
2 => $name,
3 => $encoding,
4 => $type,
5 => false, // isStringAttachment
6 => 'attachment',
7 => 0
);

PATCHED CODE:
$this->attachment[] = array(
0 => $path,
1 => $filename,
2 => $name,
3 => $encoding,
4 => $type,
5 => false, // isStringAttachment
6 => 'attachment',
7 => uniqid()
);


The problem is caused by the cid-uniqueness check in AttachAll(), in combination with attachment[7] being set to 0 in AddAttachment and AddStringAttachment.

Now after patching the library, to send multiple attachments is very easy in PHPMailer. You need to simply add a following piece of codes and you are ready to send email with multiple attachments.
$mail->addAttachment('/tmp/image1.jpg', '1.jpg');    // Attachment 1
$mail->addAttachment('/tmp/image2.jpg', '2.jpg');    // Attachment 2

Ref: https://code.google.com/a/apache-extras.org/p/phpmailer/issues/detail?id=151

Comments

Popular Posts