Sending Multiple Attachment in an Email Using PHP

How to send multiple attachment in single mail in php

Following the reusability principles, you can use https://github.com/PHPMailer/PHPMailer

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'jswan'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('josh@example.net', 'Josh Adams'); // Add a recipient
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name

$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}

echo 'Message has been sent';

Source: How to attach two or multiple files and send mail in PHP

How to Add Multiple Attachments to an Email Using PHPMailer

This question was answered by synchro on another thread. https://github.com/PHPMailer/PHPMailer/issues/2098

"The threads you pointed at are years old!

The article about unique IDs is long obsolete; inline attachments with duplicate cid values will still be ignored, but that's expected behavior, and only applies to inline attachments created using addEmbeddedImage() and addStringEmbeddedImage().

The key problem here is that you're just not handling uploads properly. How you should handle uploads is covered in the PHP docs, and all that occurs before PHPMailer has any involvement.

First of all, you need to understand how file inputs work. These determine what shows up in the $_FILES superglobal that PHP populates for you. You can either have multiple file-type inputs that select a single file each, or you can have a single one that allows you to select multiple files. PHPMailer doesn't care either way, but you have to.

Next, you need to make sure you use move_uploaded_file or at least is_uploaded_file in order to validate what's in the $_FILES superglobal, otherwise, it's not safe.

Thirdly, you need to check whether the calls to addAttachment() are successful – at present, you're just assuming they work and have no error checking at all.

So, I recommend you take a look at the single and multiple file upload examples, both of which do all of the above, and there are no known problems with adding multiple attachments."

How to send multiple attachments to email

When you upload files in PHP, they are uploaded to a temporary directory and given a random name. This is what is stored in the 'tmp_name' key for each of the files. It also explains why each file does not have an extension when sent via email, as they are simply stored as files in the temporary directory. The original file name is stored in the 'name' key.

The easiest way to deal with this issue is to rename the files to their appropriate file names and then send them, because it doesn't seem that WordPress supports a second field to provide the file name for each file.

$uploaddir = '/var/www/uploads/'; //Or some other temporary location
$myFile = $_FILES['my_file'];
$fileCount = count($myFile["name"]);
for ($i = 0; $i < $fileCount; $i++) {
$uploadfile = $uploaddir . basename($_FILES['my_file']['name'][$i]);
if (!move_uploaded_file($_FILES['my_file']['tmp_name'][$i], $uploadfile)) {
//If there is a potential file attack, stop processing files.
break;
}
$attachments[$i] = $uploadfile;

}
wp_mail($to, $subject, $message, $headers, $attachments);

//clean up your temp files after sending
foreach($attachments as $att) {
@unlink($att);
}

When dealing with files it's also a good practice to validate MIME types and restrict the types of files that you support.

WordPress wp_mail: https://developer.wordpress.org/reference/functions/wp_mail/
PHP POST Uploads: http://php.net/manual/en/features.file-upload.post-method.php



Related Topics



Leave a reply



Submit