简体   繁体   English

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

[英]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.">\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!";
    }

Following the reusability principles, you can use https://github.com/PHPMailer/PHPMailer 遵循可重用性原则,您可以使用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 来源: 如何附加两个或多个文件并使用PHP发送邮件

This is what I came up with for multiple files with form file name userfile: 这是我为多个文件格式为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;
}

For those who want to send multiple files using phpMailer and input file multiple. 对于那些想要使用phpMailer发送多个文件并输入多个文件的用户。 I joined and used the above two codes of @Rishi and @Matheno to achieve this result that dinamically add attachments selecteds by user. 我加入并使用@Rishi和@Matheno的上述两个代码来实现此结果,该结果会动态添加用户选择的附件。

On your input file name remember to put brackets: 在输入文件名上,请记住使用方括号:

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

On your php send file: 在您的php发送文件上:

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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM