带有2个附件的PHP Mail

时间:2012-10-23 15:05:44

标签: php email attachment

我有以下代码发送带有附件的邮件,而我无法将其调整为发送2个附件而不只是一个。我需要发送.docx和.pdf

我应该修改什么?

    function mail_attachment($filename1, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
        $file = $path.$filename;
        $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 .= "Reply-To: ".$replyto."\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/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "Content-Type: application/msword; name=\"".$filename."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        mail($mailto, $subject, "", $header);  
    }

这就是我所说的......

    mail_attachment("attachment.docx", "","recipient@mail.com","admin@mail.com","Admin","admin@mail.com","Subject","Mail body");

1 个答案:

答案 0 :(得分:3)

我建议您使用PHPMailer - 一个易于使用且可以完成所有工作的稳定库! http://code.google.com/a/apache-extras.org/p/phpmailer/

$mail = new PHPMailer();
$mail->IsMail();

$mail->AddAddress("email@example.com");
$mail->Subject = "Test 1";
$mail->Body = "Test 1 of PHPMailer.";

您可以在一个字符串中添加附件:

$mail->AddAttachment('/home/mywebsite/public_html/file1.zip', 'file1.zip');
$mail->AddAttachment('/home/mywebsite/public_html/file2.zip', 'file2.zip');

它更容易,更灵活。不要做猴子的工作。它已经为你完成了!
程序员一定是懒惰的!输入更少的代码)