-1

I'm a beginner in php so I had written code for testing which will send an email using php. I'm using "PHPMailer-5.2.27" to sent email with SMTP on php, but when I run the code the email not sent. I don't know what is the problem. Can anyone help me out!

<?php

require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "example@gmail.com";
$mail->Password = "xxxxxxxx";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Subject = "test mail";
$mail->Body = "just a test";
$mail->setFrom('example@gmail.com','aaaa');
$mail->addAddress('example@gmail.com');

if ($mail->send())
    echo "mail is sent";
else
    echo "something wrong ";
?>
vikram sahu
  • 161
  • 12
robert
  • 1
  • 3
  • 1
    Please set **$mail->SMTPDebug = true**, used to debug the error. Please use **$mail->ErrorInfo** to print the error – Vamsi Dec 18 '18 at 07:18
  • Enable SMTP debug mode and check what error you are getting. – Kamal Paliwal Dec 18 '18 at 07:18
  • ok, just a moment! – robert Dec 18 '18 at 07:20
  • thats whart appear : ................................................................................................................................... SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting somethong wrong – robert Dec 18 '18 at 07:22
  • Check this out for the solution: https://stackoverflow.com/questions/22927634/smtp-connect-failed-phpmailer-php – rs007 Dec 18 '18 at 07:46
  • Check out that link to the troubleshooting docs - there's lots about gmail in there. – Synchro Dec 18 '18 at 12:34

1 Answers1

0

Assuming you have allowed your Gmail account to authorize the server from where you are sending an email.

use $mail->ErrorInfo; in order to debug your issue, this will clarify what is the actual failure reason for sending an email you can refer the below code I have shared.

<?php
require "PHPMailer/PHPMailerAutoload.php";
echo !extension_loaded('openssl')?"Not Available":"Available"; //check openssl is available or not
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "mygmailaccount@gmail.com";
$mail->Password = "**********";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("toaddress@gmail.com");

if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
} else {
        echo "Message has been sent";
}
?>
vikram sahu
  • 161
  • 12