1

我有以下代码可以发送带有附件的邮件,但我无法将其调整为发送 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");
4

1 回答 1

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');

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

于 2012-10-23T15:09:54.867 回答