2015-05-29 137 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