2016-10-10 57 views
1

我在HTML網站中使用此代碼作爲我的聯繫表單,但郵件未收到Gmail收件箱。使用此php代碼不會發送電子郵件到Gmail收件箱

任何人都可以幫助我,我試圖解決這個問題,但我沒有任何指導。

<?php 
session_cache_limiter('nocache'); 
$subject = $_REQUEST['subject']; // Subject of your email 
$to = "[email protected]"; //Recipient's E-mail 

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

$message = 'Name: ' . $_REQUEST['name'] . "<br>"; 
$message .= 'Company: ' . $_REQUEST['company'] . "<br>"; 
$message .= $_REQUEST['message']; 

if (@mail($to, $subject, $message, $headers)) 
{ 
    // Transfer the value 'sent' to ajax function for showing success message. 
    echo 'sent'; 
    // header('Location: ../index.html'); 
} 
else 
{ 
    // Transfer the value 'failed' to ajax function for showing error message. 
    echo 'failed'; 
} 
?> 
+0

使用郵件功能,而不是at.mail,at.mail功能沒有拋出所有的警告/錯誤 – user3099298

+0

檢查您的垃圾郵件。 – Veer

+0

請注意,您需要提及正確的發件人ID。 我幾天前面臨同樣的問題。 –

回答

0

如果您正在本地服務器中開發此程序。電子郵件將不會發送到您的Gmail帳戶。

如果您想在本地機器上測試您的代碼,請安裝Test Mail Server Tool

在本地機器上運行時不會傳送電子郵件,但您會了解電子郵件的外觀。

當您在網站託管服務器上運行相同的電子郵件時,該電子郵件將被傳送到$to字段中指定的電子郵件ID。

1

設置標題有一些問題。 最重要的是,您需要在From部分中定義正確和有效的電子郵件ID,因爲谷歌通常用於驗證來自該域的郵件。

如果在google結束時沒有列入白名單,那麼它會自動將郵件終止到垃圾郵件,現在正如我所想。

問題很簡單,PHP郵件功能沒有使用配置良好的SMTP服務器。 如今,電子郵件客戶端和服務器對電子郵件發送服務器執行大量檢查,如反向DNS查詢,重複列表和查詢。所有這些測試都會失敗,使用php mail()函數。如果您使用動態IP,則更糟糕。使用PHPMailer-Class並將其配置爲使用smtp-auth以及配置良好的專用SMTP服務器(本地或遠程服務器),並且您的問題是

您可以嘗試下面的代碼。

$headers = "From: [email protected]\r\n"; 
$headers .= "Reply-To: [email protected]\r\n"; 
$headers .= "Return-Path: [email protected]\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "BCC: [email protected]\r\n"; 

參考鏈接。 https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer

還有另外一個代碼,你可以嘗試:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 
$file = $path.$filename; 
$file_size = filesize($file); 
$handle = fopen($file, "r"); 
$content = fread($handle, $file_size); 
fclose($handle); 
$content = chunk_split(base64_encode($content)); 
$uid = md5(uniqid(time())); 
$header = "From: ".$from_name." <".$from_mail.">\r\n"; 
$header .= "Reply-To: ".$replyto."\r\n"; 
$header .= "MIME-Version: 1.0\r\n"; 
$header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n"; 
$header .= "This is a multi-part message in MIME format.\r\n"; 
$header .= "--".$uid."\r\n"; 
$header .= "Content-type:text/plain;charset=iso-8859-1\r\n"; 
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
$header .= $message."\r\n\r\n"; 
$header .= "--".$uid."\r\n"; 
$header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n"; 
// use different content types here$header .= "Content-Transfer-Encoding: base64\r\n"; 
$header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n"; 
$header .= $content."\r\n\r\n"; 
$header .= "--".$uid."--"; 
if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK"; 
    // or use booleans here} else {echo "mail send ... ERROR!"; 
} 
} 
+0

請注意,您需要提及正確的From Mail Id。 –

相關問題