2013-05-06 338 views
0

好吧,儘管以前有人問過相關的問題,並且在整個上午都參與了這個工作,但我仍然很擔心這一點。無法用PHP發送電子郵件

問題是基本的 - 我無法使用PHP從一個新的應用程序開始發送電子郵件。郵件主機是本地主機,它不需要認證。我可以簽出我以前使用PHP郵件功能編寫的應用程序,它可以工作。 php.ini文件在兩種情況下都是相同的,因此這兩種情況都使用localhost。

工作應用程序和新應用程序都使用作曲家安裝了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('[email protected]', '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文件。我認爲這個問題的唯一另外兩個來源是代碼本身以及我不知道的未知原因。代碼看起來對我來說很好...

什麼給?爲什麼這個heck不能從郵件()中獲得體面的錯誤信息?

+1

如果你想好了錯誤消息和一個更好的庫你應該看看'PHPMailer'。 – h2ooooooo 2013-05-06 16:53:49

+1

您是否在'mailog'文件中收到任何PHP錯誤或錯誤?我也建議使用像PHPMailer或Zend – ajtrichards 2013-05-06 16:53:58

+0

我第三'PHPMailer'。 [這是一個鏈接](https://github.com/Synchro/PHPMailer)。 – Kermit 2013-05-06 16:54:46

回答

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

有些事情要檢查:

在php.ini確保sendmail_path設置爲

which sendmail 

輸出,然後如果未進行相應設置:

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

嘗試發送帶有sendmail的消息並確保您未被列入黑名單:

sendmail -v [email protected] "hello" 

(CTR + D將發送,您可以看到來自接收服務器的響應)

+0

我可以從命令行發送郵件。 – Lurk21 2013-05-06 19:08:55