无法使用PHP发送电子邮件

时间:2013-05-06 16:52:12

标签: php email

好吧,尽管我之前已经提出过相关问题并且整个上午一直在努力,但我仍严重坚持这一点。

问题是基本的 - 我无法使用PHP从我刚开始的新应用程序发送电子邮件。 mailhost是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');
    }

此代码抛出异常,&#34;电子邮件无法发送&#34;。我可以确认$ this-&gt; toAddress是一个有效的电子邮件地址,并且$ this-&gt;主题是有效的主题,并且$ this-&gt; fromAddress是一个有效的电子邮件地址,并且$ this-&gt;正文是一个有效的身体,只有几个字符长。

在尝试将其简化为最简单的示例时,我尝试了以下代码:

<?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");
}
?>

记录&#34;失败&#34;。

为了确认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");
}

我已经删除了作为问题原因的邮件主机(localhost)和PHP.ini文件。我认为问题的另外两个来源是代码本身和一个我不知道的未知原因。代码对我来说很好......

是什么给出的?为什么heck可以通过mail()发出一个不错的错误信息?

2 个答案:

答案 0 :(得分:0)

要检查的一些事项:

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

的输出
which sendmail

然后相应地设置,如果不是:

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

尝试使用sendmail发送邮件,并确保您未被列入黑名单:

sendmail -v someone@someaddress.com "hello"

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

答案 1 :(得分: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";