【问题标题】:send email with pdf attachment发送带有 pdf 附件的电子邮件
【发布时间】:2011-10-09 07:16:15
【问题描述】:

我正在尝试使用 php 中的 mail() 函数发送一封带有 pdf 附件的电子邮件。 我在本地机器上运行脚本。我在php.ini 中设置了smtp ip。 我可以完美地发送文本电子邮件,但带有附件时出现以下错误:

Warning: mail() [function.mail]: SMTP server response: 503 Unexpected command or sequence of commands in C:\AppServ\www\PhpProject1\CV-Generator\testemail2.php on line 55

谁能告诉我怎么了?

这是我的代码:

<?php
// download fpdf class (http://fpdf.org)
require('./pdf/fpdf.php');

// fpdf object
$pdf = new FPDF();

// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");

// email stuff (change data below)
$to = $_GET['send']; 
$from = "info@asaltechd.com"; 
$subject = "send email with pdf attachment"; 
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "example.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));


// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol; 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
mail($to, $subject, "", $headers);

?>

【问题讨论】:

  • 判断第 55 行是哪一行将是一个好的开始。否则,此错误是您尝试将邮件发送到的 远程 服务器的答案。
  • @Bobby - 因为代码中只有一行可以与 SMTP 服务器通信,所以我认为我们可以安全地猜测 - 但是 (@Amjad) 指出这一点是一个好习惯。

标签: php email pdf


【解决方案1】:

附件不在标题中!他们应该只声明 MIME 标头:

// main header (multipart mandatory)
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol; // see below 
$headers .= "Content-Transfer-Encoding: 7bit".$eol;


// message
$msg = "--".$separator.$eol;
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $message.$eol.$eol;

// attachment
$msg .= "--".$separator.$eol;
$msg .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment".$eol;
$msg .= $attachment.$eol;
$msg .= "--".$separator."--".$eol;

// send message
mail($to, $subject, $msg, $headers);

还请注意,您不应该在标头中有 2 个连续的行终止 - SMTP 使用空行作为标头和正文之间的分隔符。

此外,EOL 不应是操作系统的默认值 - 它应该是 SMTP 定义的 EOL 序列 - 即 CR+LF

【讨论】:

    【解决方案2】:

    我使用 PHP 的 SwiftMailer (http://swiftmailer.org/):

    require_once('../lib/swiftMailer/lib/swift_required.php');
    
    ...
    
    
    $body="Dear $fname,\n\nPlease find attached, an invoice for the period $startDate - $endDate\n\nBest regards,\n\nMr X";
    
    $message = Swift_Message::newInstance('Subject goes here')
        ->setFrom(array($email => "no-reply@mydomain.com"))
        ->setTo(array($email => "$fname $lname"))
        ->setBody($body);
    $message->attach(Swift_Attachment::fromPath("../../invoices_unpaid/$id.pdf"));
    
    $result = $mailer->send($message);
    

    【讨论】:

      【解决方案3】:

      我建议您使用PHP Mailer 从您的 PHP 发送电子邮件。我在许多不同的配置上都成功地使用了它。该类具有处理编码、附件、客户标头、通过 sendmail 等发送等所有必要的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-20
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 2014-05-29
        • 1970-01-01
        相关资源
        最近更新 更多