7

I would like to know about attaching multiple attachment in single mail and send . Please refer my following oode. In this only one file is getting attached. That is second file. First file is not at all considering for attaching. But file is being created properly in the path specified.

$filename=array($filenamee1 ,$filenamee2);
    for($x=0;$x<count($filename);$x++){
        echo $path.$filename[$x];
    $file = $path.$filename[$x];
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "cc: ".$mailtoCC."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename[$x]."\"\r\n"; 
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename[$x]."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
}
    if (mail($mailto, $subject, "", $header)) {
        echo "<br>mail sent Successfully... OK"; 
    } else {
        echo "<br>mail send ... ERROR!";
    }

3 Answers3

17

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

Community
  • 1
  • 1
Matheno
  • 4,112
  • 6
  • 36
  • 53
  • Thanks a lot for your answer. I would like to know what i have done mistake in my code and why its not getting attached as multiple attachment. – Abinaya Palanisamy Dec 04 '14 at 14:36
  • 4
    That's rather the point of using a library. There are so many ways to go wrong with email it's not worth the pain. I suggest you try generating the same message with PHPMailer then compare it with yours to see where you are going wrong. Could also try running your message through IETF msglint. – Synchro Dec 04 '14 at 14:52
  • 1
    Thanks dude. I will start follow your way – Abinaya Palanisamy Dec 05 '14 at 12:07
2

This is what I came up with for multiple files with form file name userfile:

for($ct=0;$ct<count($_FILES['userfile']['tmp_name']);$ct++)
{
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'][$ct]));
    $filename =$_FILES['userfile']['name'][$ct];
    if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
        $mail->addAttachment($uploadfile, $filename);
    }

}

if ($mail->send()) {
    echo "Sent";
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Rishi
  • 228
  • 4
  • 12
2

For those who want to send multiple files using phpMailer and input file multiple. I joined and used the above two codes of @Rishi and @Matheno to achieve this result that dinamically add attachments selecteds by user.

On your input file name remember to put brackets:

<input type="file" multiple="multiple" name="attach_file[]" />

On your php send file:

Instead of:

$mail->addAttachment('/var/tmp/file.tar.gz');

Use:

for($ct=0;$ct<count($_FILES['attach_file']['tmp_name']);$ct++){
    $mail->AddAttachment($_FILES['attach_file']['tmp_name'][$ct],$_FILES['attach_file']['name'][$ct]);
}
Felipe Lima
  • 443
  • 1
  • 10
  • 19