PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送

Posted

技术标签:

【中文标题】PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送【英文标题】:PHP Sending Emails with File Attachments - Email Not Sending At All 【发布时间】:2012-06-15 18:58:51 【问题描述】:

在尝试阅读有关使用 php 发送带有附件的电子邮件的各种文章(我习惯于使用 VBScript 的 ASP)之后,我编写了以下代码。不幸的是,它根本不起作用。它不仅没有发送带有附件的电子邮件,而且电子邮件似乎根本没有发送,即使我的脚本说它确实发送了。我哪里出错了?我没有使用表单来上传文件。这是一个静态脚本。

<?php

$EmailTo = "Me@here.com";
$EmailFrom = "You@There.com";
$EmailSubject = "The Email Subject";

$MailBoundary = md5(uniqid(time()));

$Headers = "To: ". $EmailTo . "\r\n";
$Headers .= "From: ". $EmailFrom . "\r\n";
$Headers = "MIME-Version: 1.0\r\n";
$Headers .= "Content-type: multipart/mixed;boundary=\"$MailBoundary \"";
$Headers .= "\r\n\r\n";
$Headers .= "This is a multi-part message in MIME format.";
$Headers .= "\r\n\r\n";

$FileAttachment = "AttachedFile.pdf";
$File = fopen($FileAttachment, "r");
$FileData = fread($File, filesize($FileAttachment));
$FileData = chunk_split(base64_encode($FileData));
$FileName = basename($FileAttachment);

$EmailBody = "--$MailBoundary\r\n";
$EmailBody .= "Content-type: text/html; charset=iso-8859-1\r\n";
$EmailBody .= "Content-transfer-encoding: 8bit\r\n\r\n";

$EmailBody .= "<html>" . chr(13) .
              "<head>" . chr(13) .
              "<style>" . chr(13) .
              ".breg font-family:arial;font-size:10pt;color:#000000;padding:5px;" . chr(13) .
              "</style>" . chr(13) .
              "</head>" . chr(13) .
              "<body>" . chr(13) .
              "<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) .
              "The message text body goes here" . chr(13) .
              "</div>" . chr(13) .
              "</body>" . chr(13) .
              "</html>";

$EmailBody .= "--$MailBoundary\r\n";


$EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName\r\n";
$EmailBody .= "Content-transfer-encoding:base64\r\n\r\n";
$EmailBody .= $FileData. "\r\n\r\n";

$EmailBody .= " --$MailBoundary--";

if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers))

 echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13);

else

 echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13);


?>

【问题讨论】:

您是否尝试过删除所有额外的内容,只发送最少的电子邮件以确保邮件呼叫正常工作?如果可行,您可以慢慢重新添加功能,直到找到阻止它工作的部分。 我会停止尝试使用 php mail() 它的功能非常弱,看看 phpmailer 或其他 calss 也许你应该改用 PHPMailer 类!?谷歌这个 是的,我可以发送一封基本的电子邮件。所以我知道 mail() 正在工作 我不知道我正在使用的服务器上是否有 PHPMailer。 【参考方案1】:

我认为您的电子邮件内容有误, 使用 imap_mail_compose 函数生成您的电子邮件内容:

mail('', 'message subject', '', imap_mail_compose(array header, array body)); 

【讨论】:

【参考方案2】:
For sending mail with attachment using php mail().Try this code:

<?php

 //If there is no error, send the email
 if(isset($_POST['ur_submit_button_name'])) 
  $EmailTo = "Me@here.com";
  $EmailFrom = "You@There.com";
  $EmailSubject = "The Email Subject";


  $separator = md5(time());

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

  // attachment name
  $filename = "ip.zip";//store that zip file in ur root directory
  $attachment = chunk_split(base64_encode(file_get_contents('ip.zip')));

  // main header
  $headers  = "From: ".$from.$eol;
  $headers .= "MIME-Version: 1.0".$eol; 
  $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

  // no more headers after this, we start the body! //

  $body = "--".$separator.$eol;
  $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  $body .= "This is a MIME encoded message.".$eol;

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

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

  // send message
  if (mail($to, $subject, $body, $headers)) 
  $mail_sent=true;
  echo "mail sent";
   else 
  $mail_sent=false;
  echo "Error,Mail not sent";

 


?>

【讨论】:

邮件没有发送我们必须做的事情。? 太棒了!只有在我注意到你在字符集周围的双引号后它才对我有用:"Content-Type:text/plain; charset=\"iso-8859-1\"".$eol

以上是关于PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送的主要内容,如果未能解决你的问题,请参考以下文章

用PHP发送带有附件的电子邮件,只显示字符没有附加文件

PHP 发送带有 PDF 附件的电子邮件

在 PHP 中,如何发送带有附件的 HTML 电子邮件?

如何使用带有附件的 PEAR Mail 包使用 PHP 发送电子邮件

使用 S/MIME (PHP) 发送带有附件的电子邮件

功能发送带有附件错误的电子邮件