I am trying to create a contact form on my website using PHPMailer. I am having some trouble setting it up. I am trying to use G-mail as my smtp host. I was wondering if anyone can help troubleshoot this?
This is my mailer code:
<?php
require("class.phpmailer.php");
require("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 467;
$mail->Username = "validmail@gmail.com"; // SMTP username
$mail->Password = "workingpassword"; // SMTP password
$mail->From = "validmail@gmail.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("info@example.com", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
The error message:
Message could not be sent.
Mailer Error: The following From address failed: validmail@gmail.com
smtp.gmail.com requires that you use SSL and port 587 or 465.
See their configuration page: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
Go to google settings and enable 'less secure' applications. It worked for me.
Are you running PHP on Windows? Then this might help:
http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html
For some reason, google redirects SMTP requests to gmail-smtp-msa.l.google.com
So you should put the final destination in your config, as PHPMailer does not follow redirections. Also you may try to send emails without SMTPSecure by keeping that field blank.
Have you looked at and tried the info from this Q?
PHPMailer: SMTP Error: Could not connect to SMTP host
In particular, does this provide any additional info?
If your email in gsuite check two steps: https://support.google.com/a/answer/6260879?hl=en https://accounts.google.com/DisplayUnlockCaptcha
Php Mail Settings; PHPMailer version 5.2.22 Check class.phpmailer.php,
mail send php
This is work for me. Hope it helps.