簡體   English   中英

PHP mail() 函數不發送電子郵件

[英]PHP mail() function not sending email

我正在嘗試使用mail() PHP函數發送電子郵件。 我一直在工作,直到我試圖給它一個“用戶注冊”的主題,然后郵件沒有發送!

這是代碼(已經大大簡化了)

$to = $this->post_data['register-email'];
$message = 'Hello etc';
$headers = 'From: noreply@example.com' . "\r\n" ;
$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $message, $headers);

我還嘗試使用包含文本字符串的變量,但沒有用。

為什么添加主題異常時不發送郵件?

謝謝

編輯:更新的代碼仍然無法正常工作

$to = $this->post_data['register-email'];
$message = 'Hello etc';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'From: Website <admin@example.com>';
mail($to, 'User Registration', $message, $headers);

在您的第 4 行,您使用'將其中的所有內容作為字符串處理,因此請更改

$headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';

到:

$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

並且在評論中提到改變charesetcharset

編輯:

如果您發送 txt/html 郵件,您也可以根據文檔在標題中設置 mime,所以試試這個

    $to = $this->post_data['register-email'];
    $message = 'Hello etc';

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= 'From: Website <admin@example.com>' . "\r\n";
    mail($to, 'User Registration', $message, $headers);

如果它仍然不起作用,您可以嘗試調試您的代碼,只需添加

error_reporting(E_ALL);
ini_set('display_errors', '1');

在頁面頂部,然后從那里取出,如果您仍然無法自己解決,請將其張貼在這里,我會盡力幫助您。

我在我的大部分項目中使用此代碼:

$subject = 'subject';
$message = 'message';
$to = 'user@gmail.com';
$type = 'plain'; // or HTML
$charset = 'utf-8';

$mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
$uniqid   = md5(uniqid(time()));
$headers  = 'From: '.$mail."\n";
$headers .= 'Reply-to: '.$mail."\n";
$headers .= 'Return-Path: '.$mail."\n";
$headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
$headers .= 'X-Priority: 3'."\n";
$headers .= 'X-MSMail-Priority: Normal'."\n";
$headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
$headers .= '------------'.$uniqid."\n";
$headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
$headers .= 'Content-transfer-encoding: 7bit';

mail($to, $subject, $message, $headers);

我建議使用 PHP_EOL 而不是 \\r\\n 或 \\n 因為換行符將由您的環境決定......

$headers  = 'MIME-Version: 1.0' . PHP_EOL;

等等...希望這可能最終解決您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM