2

I have just started coding in PHP. I'm trying to send a mail but it throws an error Mailer Error: SMTP connect() failed.

Below is the code i am using :

    date_default_timezone_set('Etc/UTC');

    require 'PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP(); 
    $mail->Host = gethostbyname('ssl://smtp.gmail.com'); 

    $mail->SMTPAuth = true; 
    $mail->AuthType = 'LOGIN';  
    $mail->Username = 'abc@gmail.com';
    $mail->Password = '********';   
    $mail->SMTPSecure = 'tls';  
    $mail->Port = 587;  

    $mail->setFrom('abc@gmail.com','Abc') ;
    $mail->addReplyTo('abc@gmail.com');
    $mail->addAddress("abc@gmail.com",'No REPLY');
    $mail->isHTML(true);

    $mail->Body = "12345";
    $mail->Subject = "<b>OTP for password reset<b>";
    $mail->Body = "Hi! Your OTP is : ";

    if(!$mail->Send()) {
       echo 'Message was not sent.';
       echo "Mailer Error: " . $mail->ErrorInfo;
       } 
    else {
       echo 'Message has been sent.';
    }  

Any help would be appreciated.

1 Answers1

1

Change from

$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->SMTPSecure = 'tls';  
$mail->Port = 587; 

to

$mail->Host = "smtp.gmail.com"; 
$mail->SMTPSecure = 'ssl';  
$mail->Port = 465; 

And check for open_ssl enabled

Open your php.ini and search for this line ;extension=php_openssl.dll and remove the semi colon before it , save the file and restart your webserver.

dhruv jadia
  • 1,684
  • 2
  • 15
  • 28