0

好的,尽管之前已经提出了一个相关的问题并且整个上午都在研究这个问题,但我还是非常坚持这一点。

问题很基本 - 我无法从我正在启动的新应用程序中使用 PHP 发送电子邮件。邮件主机是 localhost,它不需要身份验证。我可以签出我以前编写的一个应用程序,该应用程序也使用 PHP 邮件功能并且它可以工作。两种情况下的 php.ini 文件相同,因此两种情况都使用 localhost。

工作应用程序和新应用程序都使用 composer 安装了 swiftmailer,但在工作示例和本示例中都没有使用 swiftmailer。

这是我想要工作的实际代码:

    // Determine headers
    $uid = md5(uniqid(time()));
    $headers = "From: " . $this->fromAddress . "  <" . $this->fromName . ">\r\n";
    $headers.= "Reply-To: " . $this->fromAddress . " <" . $this->fromName . ">\r\n";
    if ($this->cc != "") { $headers .= "CC: ".$this->cc."\r\n"; }
    if ($this->bcc != "") { $headers .= "BCC: ".$this->bcc."\r\n"; }
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
    $headers .= "This is a multi-part message in MIME format.\r\n";
    $headers .= "--" . $uid . "\r\n";
    $headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $headers .= $this->body . "\r\n\r\n";

    // Optionally attach a file
    foreach ($this->attachments as $attachment) {
        $fileName = basename($attachment);
        $fileSize = filesize($attachment);
        $handle = fopen($attachment, "r");
        $content = fread($handle, $fileSize);
        fclose($handle);
        $content = chunk_split(base64_encode($content));

        $headers .= "--" . $uid . "\r\n";
        $headers .= "Content-Type: application/octet-stream; name=\"" . $fileName . "\"\r\n";
        $headers .= "Content-Transfer-Encoding: base64\r\n";
        $headers .= "Content-Disposition: attachment; filename=\"" . $fileName . "\"\r\n\r\n";
        $headers .= $content."\r\n\r\n";

        unlink($attachment);
    }

    // Conclude headers
    $headers .= "--".$uid."--";

    // Send the email
    $mail_sent = mail($this->toAddress,$this->subject,'',$headers);

    if (!$mail_sent) {
        throw new Exception('Email failed to send');
    }

此代码引发异常“电子邮件发送失败”。我可以确认 $this->toAddress 是有效的电子邮件地址,$this->subject 是有效的主题,$this->fromAddress 是有效的电子邮件地址,$this->body 是有效的正文,只有几个字符长。

为了将其归结为最简单的示例,我尝试了以下代码:

<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
$result = mail('lowens@mycompany.com', 'My Subject', $message);

if (!$result) {
    error_log("fail");
}
?>

记录“失败”。

只是为了确认 localhost 有效,我重新检查了有效的代码。这是有效的代码:

// Determine headers
$uid = md5(uniqid(time()));
$headers= "From: " . $login->getUser() . " <" . $login->getUserEmail() . ">\r\n";
$headers.= "Reply-To: " . $login->getUser() . " <" . $login->getUserEmail() . ">\r\n";
if ($bcc != "") { $headers .= "BCC: ".$bcc."\r\n"; }
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$headers .= "This is a multi-part message in MIME format.\r\n";
$headers .= "--".$uid."\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= $message."\r\n\r\n";

// Optionally attach a file
foreach ($attachedFilePaths as $attachedFilePath) {
    $fileName = basename($attachedFilePath);
    $fileSize = filesize($attachedFilePath);
    $handle = fopen($attachedFilePath, "r");
    $content = fread($handle, $fileSize);
    fclose($handle);
    $content = chunk_split(base64_encode($content));

    $headers .= "--".$uid."\r\n";
    $headers .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n"; // use different content types here
    $headers .= "Content-Transfer-Encoding: base64\r\n";
    $headers .= "Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n";
    $headers .= $content."\r\n\r\n";

    unlink($attachedFilePath);
}

$headers .= "--".$uid."--";

// Send the email
$mail_sent = @mail($toAddr,$subject,'',$headers);

// Save this email as a task
require_once('../classes/task.class.php');
$task = new Task();
$task->saveMailAsTask($CustomerId, $toAddr, $bcc, $subject, $message);

// This can be used to return a success or failure
if ($mail_sent) {
    redirect("http://$domainName/admin/index.php?task=account&event=viewdetails&id=$CustomerId&emailSentOK=true&d=emailResponse");
} else {
    redirect("http://$domainName/admin/index.php?task=account&event=viewdetails&id=$CustomerId&emailSentOK=false&d=emailResponse");
}

我已经消除了导致问题的邮件主机(本地主机)和 PHP.ini 文件。我认为问题的另外两个来源是代码本身和我不知道的未知原因。代码对我来说看起来不错...

是什么赋予了?为什么不能从 mail() 中发出一个像样的错误消息?

4

2 回答 2

0

问题是从字段。我正在颠倒姓名和地址的顺序。

不正确:

$headers = "From: " . $this->fromAddress . "  <" . $this->fromName . ">\r\n";
$headers.= "Reply-To: " . $this->fromAddress . " <" . $this->fromName . ">\r\n";

正确的:

$headers = "From: " . $this->fromName . "  <" . $this->fromAddress . ">\r\n";
$headers.= "Reply-To: " . $this->fromName . " <" . $this->fromAddress . ">\r\n";
于 2013-05-06T19:21:54.567 回答
0

需要检查的一些事项:

在您的 php.ini 中确保将 sendmail_path 设置为

which sendmail

如果不是,则进行相应设置:

sendmail_path = '/usr/sbin/sendmail -t'

尝试使用 sendmail 发送消息,并确保您没有被列入黑名单:

sendmail -v someone@someaddress.com "hello"

(CTR+D 将发送,您可以看到接收服务器的响应)

于 2013-05-06T16:57:27.867 回答