I am using this php code to send email through gmail account . but its showing smtp error . I have googled it . but not found any specific answer . would anyone please help to find the solution ??
I have hosted this code in my webserver
error : "SMTP Error: Could not connect to SMTP host. Message was not sent Mailer Error: SMTP Error: Could not connect to SMTP host.SMTP Error: Could not connect to SMTP host."
code:
<?php
require("class.phpmailer.php");
$email = $_POST['email_address'];
$username = 'username@gmail.com'; //your gmail address
$password = 'password'; //password
send_mail_to_subscriber();
function send_mail_to_subscriber()
{
global $username,$password,$email;
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'ssl://smtp.gmail.com:465';
$mailer->SMTPAuth = TRUE;
$mailer->Username = $username;
$mailer->Password = $password;
$mailer->From = $username;
$mailer->FromName = $username;
$mailer->Body = 'this is a message';
$mailer->Subject = 'this is a subject';
$mailer->AddAddress($email);
if(!$mailer->Send())
{
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
}
else
{
echo "Download link has been sent to your email address";
}
}
?>
Change your Host and add the following:
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->Mailer = "smtp";
$mail->setCharset = "UTF-8";
$mail->isSMTP();
$mail->SMTPDebug = 0; // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; //Specify main and backup SMTPservers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'gmail user name'; // SMTP username
$mail->Password = 'gmail password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 'Your Outgoing server port number';// TCP port to connect to
$mail->From = 'from address ';
$mail->FromName = ' from name';
To Identify the Issues, Change the value $mail->SMTPDebug = 1,2.3.4.