PHP Mailer适用于localhost(MAMP),但不适用于实时站点

时间:2010-10-13 22:42:05

标签: gmail phpmailer

我正在学习制作网站,而且我正在为我父母的业务整理一个简单的网站。我做了一个联系表格,邮件只是通过电子邮件发送给我父母的电子邮件。我只是查了一个示例GMail PHP代码来发送邮件,当我用MAMP测试它时它可以正常工作,但是当我将它上传到实际网站时,我收到了这个错误:

  

SMTP错误:无法连接到SMTP主机。邮件程序错误:SMTP错误:无法连接到SMTP主机。

为什么它不能在实时网站上运行,但是当我在MAMP上测试时它可以工作?这是我的代码:

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host       = "myparentswebsite.com"; 
$mail->Mailer = "smtp";      
$mail->Host = "ssl://smtp.gmail.com";  
$mail->Port = 465;

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "parentswebsite@gmail.com"; // SMTP username
$mail->Password = "paswsword"; // SMTP password

$webmaster_email = "parentswebsite@gmail.com"; //Reply to this email ID
$email= $_POST['email']; // Recipients email ID
$name= $_POST['name']; // Recipient's name 
$mail->From = $webmaster_email;
$mail->FromName = "parentswebsite";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap

$mail->IsHTML(true); // send as HTML
$mail->Subject = "Feedback from parentswebsite.com";
$mail->Body = $_POST['name']. " " .$_POST['email']. " ". $_POST['phone']. " ". $_POST['message'] ;//HTML Body

echo "it works"; 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "We have received your message!";
}

1 个答案:

答案 0 :(得分:1)

尝试添加以下内容:

$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true; 

并将主机和端口更改为

$mail->Host='smtp.gmail.com';
$mail->Port='465';  

如果这不起作用,请检查您的服务器防火墙是否阻止到gmail.com或通过端口465的出站连接。

检查你是否具有fopen和/或fsockopen启用的功能

检查你是否启用了php curl(不确定)