簡體   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