Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

phpmailer - How to send multiple attachment in single mail in php

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.">
";
    $header .= "cc: ".$mailtoCC."
";
    $header .= "MIME-Version: 1.0
";
    $header .= "Content-Type: multipart/mixed; boundary="".$uid.""

";
    $header .= "This is a multi-part message in MIME format.
";
    $header .= "--".$uid."
";
    $header .= "Content-type:text/html; charset=iso-8859-1
";
    $header .= "Content-Transfer-Encoding: 7bit

";
    $header .= $message."

";
    $header .= "--".$uid."
";
    $header .= "Content-Type: application/octet-stream; name="".$filename[$x].""
"; 
    $header .= "Content-Transfer-Encoding: base64
";
    $header .= "Content-Disposition: attachment; filename="".$filename[$x].""

";
    $header .= $content."

";
    $header .= "--".$uid."--";
}
    if (mail($mailto, $subject, "", $header)) {
        echo "<br>mail sent Successfully... OK"; 
    } else {
        echo "<br>mail send ... ERROR!";
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 = '[email protected]';
$mail->FromName = 'Mailer';
$mail->addAddress('[email protected]', '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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...