How to Attach Two or Multiple Files and Send Mail in 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."

codeigniter multiple file filed send in email

use this code

first we need to upload data on server with name
then we append name of files
then we make create loop then send email

 if($this->input->post('attachment') && !empty($_FILES['userFiles']['name'])){
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$uploadPath = './uploads/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|txt';

$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$this->load->library('email');
$this->email->from('abc@gmail.com');
$this->email->to('abc@gmail.com');
$this->email->subject('New query');
$this->email->message($message);
$pathToUploadedFile = $fileData['full_path'];
$this->email->attach($pathToUploadedFile);
}
}

$this->email->send();
}

Send attachments with PHP Mail()?

I agree with @MihaiIorga in the comments – use the PHPMailer script. You sound like you're rejecting it because you want the easier option. Trust me, PHPMailer is the easier option by a very large margin compared to trying to do it yourself with PHP's built-in mail() function. PHP's mail() function really isn't very good.

To use PHPMailer:

  • Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer
  • Extract the archive and copy the script's folder to a convenient place in your project.
  • Include the main script file -- require_once('path/to/file/class.phpmailer.php');

Now, sending emails with attachments goes from being insanely difficult to incredibly easy:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); //Name is optional
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );

$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';

$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );

return $email->Send();

It's just that one line $email->AddAttachment(); -- you couldn't ask for any easier.

If you do it with PHP's mail() function, you'll be writing stacks of code, and you'll probably have lots of really difficult to find bugs.

How to attach multiple files to two different e-mails using PHPMailer?

I solved the problem like this:

// First e-mail to recipient 1

$mail = new PHPMailer;
$mail->setFrom('example@example.com');
$mail->addAddress('recipient1@example.com');
$mail->Subject = 'Subject';
$mail->isHTML(true);
$mail->Body = '...';

// Attach multiple files one by one
for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
$filename = $_FILES['userfile']['name'][$ct];
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
$mail->addAttachment($uploadfile, $filename);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}


// Altered e-mail to recipient 2

$mail->ClearAddresses(); // avoid recipient 1 getting this altered mail
$mail->addAddress('recipient2@example.com');
$mail->Subject = 'New subject overwriting the first one';
$mail->Body = 'New body overwriting the first one';


$mail->send(); // send both mails

By that, the same mail is basically sent twice, including the attachments, but with some changes being made by overwriting e. g. the subject and body.



Related Topics



Leave a reply



Submit