2015-05-29 134 views
0

我試圖從PHP本地主機發送電子郵件。這是我的代碼:PHP郵件程序無法發送電子郵件

<?php 

// $email and $message are the data that is being 
// posted to this page from our html contact form 
$email = $_REQUEST['email'] ; 
$message = $_REQUEST['message'] ; 

require_once('class.phpmailer.php'); 
require 'PHPMailerAutoload.php'; 
require 'class.smtp.php'; 

$mail = new PHPMailer(); 

$mail->IsSMTP(); 

$mail->Host = "localhost"; // specify main and backup server 

$mail->SMTPAuth = false;  // turn on SMTP authentication 

$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = ""; // SMTP password 
$mail->Port = 25; 
$mail->SMTPSecure = ''; 

$mail->From = $email; 

$mail->AddAddress("[email protected]", "Brad Markle"); 

$mail->WordWrap = 50; 

$mail->IsHTML(true); 

$mail->Subject = "You have received feedback from your website!"; 

$mail->Body = $message; 
$mail->AltBody = $message; 

if(!$mail->Send()) 
{ 
    echo "Message could not be sent. <p>"; 
    echo "Mailer Error: " . $mail->ErrorInfo; 
    exit; 
} 

echo "Message has been sent"; 
?> 

當我運行此代碼時,它顯示「消息已發送」,但它實際上並未發送消息。我的問題是什麼?

+0

如果你使用GMAIL發送郵件,你應該檢查連接的參數。您應該使用SSL連接到SMTP。 –

+0

如果確實沒有要求,那麼嘗試刪除併發送一個簡單的PHP郵件功能的電子郵件。如果可行,那麼Gmail SMTP連接就會出現問題 – Avinash

回答

0

改變這個代碼,您有:

$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = ""; // SMTP password 
$mail->Port = 25; 
$mail->SMTPSecure = ''; 

這樣:

$mail->SMTPAuth = true;     // enable SMTP authentication 

$mail->SMTPSecure = "tls";     // sets the prefix to the servier 

$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 

$mail->Port  = 587;     // set the SMTP port for the GMAIL server 

$mail->Username = "[email protected]"; // GMAIL username 

$mail->Password = "yourpassword";   // GMAIL password 
0

除了什麼Lelio Faieta發佈,enable less secure apps in gmail

,並防止您的郵件將您的垃圾郵件 變更

$mail->From = $email; 

到你的郵箱,你可以在郵件正文中添加$ email