2016-01-08 161 views
0

我試圖發送電子郵件重置用戶密碼使用PHP郵件(),但它發送到垃圾郵件文件夾在Gmail帳戶獨自在雅虎郵件我可以得到此郵件收件箱內。我GOOGLE了一下,看到有些人說通過phpmailer使用smtp郵件服務器。郵件標記爲垃圾郵件獨自通過PHP郵件()以及smtp

我試過太多,但我得到相同的結果PHP mail()函數做了..在這裏我的PHP mail()函數的代碼

$headers = 'From: [email protected]' . "\r\n"; 
$headers .= 'Reply-To: '.$to.''; 
$headers .= 'Subject: ' . $subject ." \r\n"; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset="iso-8859-1"'. "\r\n"; 
@mail($to, $subject, $msg, $headers); 

和php郵件的SMTP代碼

date_default_timezone_set('Etc/UTC'); 

require_once 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

//Tell PHPMailer to use SMTP 
$mail->isSMTP(); 

//Enable SMTP debugging 
// 0 = off (for production use) 
// 1 = client messages 
// 2 = client and server messages 
$mail->SMTPDebug = 2; 

//Ask for HTML-friendly debug output 
$mail->Debugoutput = 'html'; 

//Set the hostname of the mail server 
$mail->Host = 'mydomain.us'; 

// use 
// $mail->Host = gethostbyname('smtp.gmail.com'); 
// if your network does not support SMTP over IPv6 
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
$mail->Port = 465; 

//Set the encryption system to use - ssl (deprecated) or tls 
$mail->SMTPSecure = 'ssl'; 

//Whether to use SMTP authentication 
$mail->SMTPAuth = true; 

//Username to use for SMTP authentication - use full email address for gmail 
$mail->Username = "[email protected]"; 

//Password to use for SMTP authentication 
$mail->Password = "pass"; 

//Set who the message is to be sent from 
$mail->setFrom('[email protected]', 'sender name'); 

//Set an alternative reply-to address 
$mail->addReplyTo('[email protected]', 'sender name'); 

//Set who the message is to be sent to 
$mail->addAddress($to, 'John Doe'); 

//Set the subject line 
$mail->Subject = $subject; 

//Read an HTML message body from an external file, convert referenced images to embedded, 
//convert HTML into a basic plain-text alternative body 
$mail->msgHTML($msg); 

//Replace the plain text body with one created manually 
//$mail->AltBody = 'This is a plain-text message body'; 

//Attach an image file 
//$mail->addAttachment('images/phpmailer_mini.png'); 

//send the message, check for errors 
if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 
+0

您的郵件是否以垃圾郵件結尾並不會與您發送郵件的方式有太大關係,而是與您發送的內容以及收件人的處理方式有關。如果收件人將其標記爲垃圾郵件,那麼gmail默認會開始這樣做。 Gmail幾乎將所有內容都標記爲垃圾郵件,而且情況越來越糟糕。 – Synchro

回答

0

你代碼看起來不錯,但如果您的服務器的IP地址已被添加到用於過濾垃圾郵件的垃圾郵件黑名單中,則無論您如何從服務器發送郵件,都無關緊要。

做一個Google搜索類似「郵件服務器黑名單」,並測試您的服務器的IP地址,看看它是否被列入黑名單。如果您是從託管公司租用服務器,則您的IP地址已被其他人使用。

您可以嘗試爲您的公司創建一個測試Gmail帳戶,並嘗試使用該SMTP帳戶通過SMTP從您的服務器發送和發送電子郵件,並查看電子郵件是否已發送。該電子郵件將以您的名義從Gmail服務器發送。

另一種選擇是使用郵件API服務,如Mailgin

相關問題