如何在php中发送单个邮件中的多个附件

时间:2014-12-04 14:21:17

标签: php phpmailer email-attachments

我想知道在单个邮件中附加多个附件并发送。请参考我的以下oode。在这只有一个文件附加。那是第二档。第一个文件根本没有考虑附加。但是文件正在指定的路径中正确创建。

$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 个答案:

答案 0 :(得分:11)

遵循可重用性原则,您可以使用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';

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

答案 1 :(得分:1)

这是我为表单文件名为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;
}

答案 2 :(得分:1)

对于那些想要使用phpMailer发送多个文件并输入多个文件的人。 我加入并使用了@Rishi和@Matheno的上述两个代码来实现这个结果,即用户添加选择的附件。

在输入文件名上,记​​得放括号:

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

在你的php发送文件中:

而不是:

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

使用:

for($ct=0;$ct<count($_FILES['attach_file']['tmp_name']);$ct++){
    $mail->AddAttachment($_FILES['attach_file']['tmp_name'][$ct],$_FILES['attach_file']['name'][$ct]);
}