Reputation: 12538
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 = "[email protected]"; // SMTP username
$mail->Password = "workingpassword"; // SMTP password
$mail->From = "[email protected]";
$mail->FromName = "Mailer";
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->AddAddress("[email protected]"); // name is optional
$mail->AddReplyTo("[email protected]", "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: [email protected]
Upvotes: 2
Views: 38020
Reputation: 1
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,
var $Host = 'smtp.gmail.com';
var $Port = 465;
var $Helo ='domain.net';
public $SMTPSecure = 'ssl';
public $SMTPAutoTLS = true;
public $SMTPAuth = true;
public $SMTPOptions = array();
public $Username = 'gmail-username';//or domain credentials on google mail
public $Password = 'gmail-password';
public $Timeout = 10;
public $SMTPDebug = true;
mail send php
<?php
require('PHPMailer/class.phpmailer.php');
require('PHPMailer/class.smtp.php');
$headers = "Content-Type: text/html; charset=UTF-8";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="SET NAMES UTF8";
$mail->SMTPDebug = 0;
$mail->CharSet="utf8";
$mail->Debugoutput = 'html';
$mail->host = "smtp.gmail.com";
$mail->port = 465;
$mail->smtpauth = true;
$mail->smtpsecure = 'ssl';
$mail->username = "gmail-username"; //or domain credentials on google mail
$mail->password = "gmail-password";
$mail->SetFrom('from-email', 'from-name');
$mail->AddAddress('to-address', 'to-address');
$mail->Body = 'your-text'; //emailbody
if(!$mail->Send())
{
mysql_close();
echo "<script>alert('Error: " . $mail->ErrorInfo."');</script>";
}
else
{
mysql_close();
echo "<script>alert('success');</script>";
}
?>
This is work for me. Hope it helps.
Upvotes: 0
Reputation: 98
ping smtp.gmail.com
For some reason, google redirects SMTP requests to gmail-smtp-msa.l.google.com
PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms
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.
$mail->Host = 'gmail-smtp-msa.l.google.com';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = '';
$mail->Port = 25;
Upvotes: 0
Reputation: 81
<?php
require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "[email protected]"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('[email protected]', 'abc');
$mail->SetFrom('[email protected]', 'def');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Go to google settings and enable 'less secure' applications. It worked for me.
Upvotes: 1
Reputation: 5349
Got the same error and the problem was that i was trying to sent email from "boy333@in**" account pretending as "girl333@in**". I just changed
$mail->From = 'girl333@in**'
to username i was actually connecting. So I changed to:
$mail->From = 'boy333@in**'
The idea is that these two fields is with the same usernames.
$mail->Username = "boy333";
$mail->From = 'boy333@in**';
Upvotes: 1
Reputation: 2143
Are you running PHP on Windows? Then this might help:
http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html
Upvotes: 1
Reputation: 14237
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
Upvotes: 3
Reputation: 3218
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?
$mail->SMTPDebug = 1;
Upvotes: 8