繁体   English   中英

无法使用PHP发送电子邮件

[英]Unable to send email with PHP

好的,尽管我之前曾问过一个相关的问题并且整个上午都在努力,但我对此仍然很认真。

问题是基本的-我无法从正在启动的新应用程序中使用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");
}

我已经消除了邮件主机(localhost)作为问题的原因以及PHP.ini文件。 我想问题的另外两个原因仅在于代码本身以及我不知道的未知原因。 该代码对我来说很好...

是什么赋予了? 为何无法通过mail()发出正确的错误消息?

要检查的一些事情:

在您的php.ini中,确保将sendmail_path设置为的输出。

which sendmail

如果没有,则进行相应设置:

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

尝试使用sendmail发送消息,并确保您不在黑名单中:

sendmail -v someone@someaddress.com "hello"

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

问题是来自领域。 我正在颠倒名称和地址的顺序。

不正确:

$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";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM