PHP Mailer works on localhost (MAMP), but not on live site
I'm learning to make websites and I'm putting together a simple website for my parents business. I made a contact form and the message is just emailed to my parents' email. I just looked up a sample GMail PHP code to send mail and it works when I test it with MAMP, but when I upload it to the live site, I get this error:
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.
Why doesn't it work on the live site, but it works when I test it on MAMP? Here's my code:
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 = "parentswebsit开发者_StackOverflow社区e@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!";
}
try adding the following :
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
and changing the Host and Port to
$mail->Host='smtp.gmail.com';
$mail->Port='465';
if this didn't work , check that your servers firewall does not block outbound connections to gmail.com or through port 465 .
check you have the functions like fopen and/or fsockopen enabled
check you have php curl enabled (not sure about that)
精彩评论