当前位置: 首页 > 面试题库 >

PHP mail()附件问题

芮明知
2023-03-14
问题内容

有人可以帮我弄清楚为什么它总是返回假吗?

$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

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

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

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$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
if (mail($to, $subject, "", $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

问题答案:
$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

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

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

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// 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)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

我所做的:

  • 将主体与标题分离
  • 删除了放在分隔符之前的多余EOL


 类似资料:
  • 我有发送邮件与谷歌SMTP和最新版本的PHPMailer的问题。这里有很多问题,但是答案对我不起作用。我使用免费托管与免费子域。这是我的标准PHP代码发送电子邮件从文档: 我得到了这个错误: .... 2017-06-03 15:15:33服务器- 如果将与端口一起使用,我会看到相同的错误。 在我的电子邮件帐户2中,工厂身份验证处于和处于。 我真的不知道该怎么办。

  • 我正在尝试使用PHPMailer和我的Google套件电子邮件发送PHP电子邮件(info@mydomain.com). 在阅读了Stack Overflow的其他帖子并尝试了一切之后,我无法使其工作。我还在正确的g套件帐户中将“允许不太安全的应用程序”设置为“true”。 以下是我不断遇到的错误: 使用SSL和端口465: 对于TLS和端口587: 以下是我的PHP代码: 我只想发送一些普通的电

  • 我使用下面的代码将pdf文件附加到邮件(JAVAMAIL)中。这个位置工作完美,并添加附件到我的邮件,但这邮件没有任何正文。 当我将下面的行添加到我的代码中时,它会删除附件并发送给我只包含文本的邮件。 请帮助我添加附件和测试体到我的邮件。

  • 问题内容: 我正在制作一个备份程序,我希望将要备份的程序显示在JTextArea上。很好,它可以工作,但是仅在程序完成备份后才可以。我该如何解决?我运行此代码在这里: 备份方法 复制文件夹类: CopyFile类 预先感谢您的帮助,如果您能提供任何帮助,请让我知道。我对此进行了谷歌搜索,这确实是一个很大的问题,但我只是想不出如何解决它。哦,请不要仅仅因为它不适用于您而拒绝投票,这会使情况更加恶化。

  • 激光发射模块 磁吸板包 磁吸连接线 USB数据线 固线夹橡皮筋混合包

  • 首先,我读了corda关于使用附件的文档。然而,我仍然有问题的过程上传和下载附件。 我的任务是编写一个简单的cordapp,用于将文件从NodeA传输到NodeB。从NodeA shell上传一个zip文件后,我收到一个散列,然后将其包含在事务中。流动成功。然而,在NodeB中,我无法取回该文件。然后我尝试使用该散列从NodeA获取文件。但是,shell返回错误消息并表示InvalidInputS