Getting Mailer Error: SMTP connect() failed error while sending email


#1

I am using Phpmailer to send emails. THe below code works fine in local but it is not in 000webhost.

<?php
require 'PHPMailer/PHPMailerAutoload.php';	
include ("db.php");
$mail = new PHPMailer;


$mail->isSMTP();                                      
$mail->Host = 'smtp.gmail.com;smtp2.gmail.com'; 
$mail->SMTPAuth = true;                               
$mail->Username = 'fromemail@gmail.com';
$mail->Password = 'password';                    
$mail->SMTPSecure = 'tls';                           
$mail->Port = 587;                                    

$mail->setFrom('fromaddress@gmail.com', 'Example');
$mail->addAddress('toaddress@gmail.com', 'Manikandan');
$mail->isHTML(true);
if($mail->IsError()) die($mail->ErrorInfo);
$mail->Subject = 'Email subjects';
$mail->Body    = 'Hello Sir, <br>Pleasse find the attachment...<br><br><br><br>Thanks';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

I remember i have made some changes in php.ini in local but i am not able to find it here… Iam new to this.Any help will be appriciated.


#2

Have a look at this troubleshooting guide.

Since you are using Gmail, you probably need to turn on the “Allow less secure apps” switch in your Google security settings.

Incidentally, please don’t ask for “immediate” help on the web - read this discussion to understand why. A number of folks here are volunteers, and they will answer you at their leisure :slight_smile:


#3

halfer sorry for asking immediate help :slight_smile: and I tried your suggestion still i am facing the same problem.


#4

Are there any errors that are thrown?
If no errors are shown, try this tutorial, maybe some errors aren’t thrown