【问题标题】:sending mail with attachment trouble发送带有附件问题的邮件
【发布时间】:2018-05-13 23:56:59
【问题描述】:

我在发送带有附件的邮件时遇到问题。我有这个功能:

function mail_att($to, $subject, $message, $anhang) { 
    $absender = "Sender"; 
    $absender_mail = "noreply@example.org"; 
    $reply = "noreply@example.org"; 

    $path = $anhang;
$uploadname = "Anhang.pdf";

$trenner = md5( time() );
    // Mail Header 
    $mailheader = "Reply-To: " .$absender. "<" .$absender_mail. ">\r\n";
    $mailheader .= "Return-Path: ".$absender_mail."\r\n";
    $mailheader .= "Message-ID: <".$absender_mail.">\r\n";
    $mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n";
    $mailheader .= "From: ".$absender."<".$absender_mail.">\r\n";
    $mailheader .= "MIME-Version: 1.0\r\n";
    $mailheader .= "Content-Type: multipart/mixed;\r\n";
    $mailheader .= " boundary = " .$trenner;
    $mailheader .= "\r\n\r\n";
 
    // Mailbody 
    $mailbody  = "This is a multi-part message in MIME format\r\n";
    $mailbody .= "--" .$trenner. "\r\n";
    $mailbody .= "Content-Type: text/html; charset=UTF-8\r\n";
    $mailbody .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
    $mailbody .= $message. "\r\n\r\n";
 
    // Attachment 
    $mailbody .= "--" .$trenner. "\r\n";
    $mailbody .= "Content-Type: application/pdf; name=\"" .$uploadname. "\"\r\n";
    $mailbody .= "Content-Transfer-Encoding: base64\r\n";
    $mailbody .= "Content-Disposition: attachment; filename=\"" .$uploadname. "\"\r\n\r\n";
    $mailbody .= chunk_split( base64_encode( file_get_contents( $path ) ) );
    $mailbody .= "\n";
 
    // Send
    mail( $to, htmlspecialchars( $subject ), $mailbody, $mailheader );
} 

但是当我想发送邮件时,我什么也得不到。问题出在哪里?我确定我输入了正确的接收者电子邮件!并且邮件服务器没有问题,因为正常的电子邮件工作正常。


已解决

哦,我从没想过要使用 phpmailer。这使一切变得更简单并且有效。我所有的工作都是徒劳的。

【问题讨论】:

  • 简单回答:不要建立自己的 mime 电子邮件,尤其是带有附件的电子邮件。使用 PHPMailer 或 Swiftmailer,这两者都会将整个代码块减少到大约 4 或 5 行。同样,您只是假设mail() 正在工作。你检查过它的返回值吗?失败时返回 false。你检查你的外发服务器的邮件日志了吗?也许这封邮件被当作垃圾邮件转储了。
  • 您要发送到 gmail 帐户吗?谷歌因过滤来自他们认为是“自动”来源的附件的电子邮件而臭名昭著。
  • 你什么都没得到?我看到你正试图发送到 >> $to 尚未定义 .它在网络空间的某个地方。 添加这个 $to = "your_email@example.com"; 下面 $reply = "noreply@example.org";.
  • @Fred 我认为 $to 是函数的参数,因此它将与所有其他参数一起传递。
  • @Sébastien 好的,我想我现在明白你的意思了,谢谢。

标签: php email


【解决方案1】:

您使用 PHP 的内置 mail() 函数发送消息。

因此,PHP 将尝试通过在运行 PHP 的同一台服务器上运行的本地 MTA 发送消息,正如您的 php.ini 文件中的 sendmail_path 所指定的那样。您首先要查看的是本地 MTA 的日志。
这些应该告诉你

  1. 本地 MTA 是否收到来自您的 PHP 脚本的消息,如果收到,
  2. 当本地 MTA 尝试将邮件传递到远程 MTA 时发生了什么。

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2017-09-13
    • 1970-01-01
    • 2017-07-31
    相关资源
    最近更新 更多