0

My log shows as follows $mail->SMTPSecure = 'tls';
$mail->Port = 587;

017-03-14 09:53:25 SERVER -> CLIENT: 220 smtp.gmail.com CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service
CLIENT -> SERVER: STARTTLS
CLIENT -> SERVER: This is the body in plain text for non-HTML mail clients CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii 2017-03-14 09:53:27
CLIENT -> SERVER: This body in bold! 2017-03-14 09:53:27
SERVER -> CLIENT: 221 2.0.0 closing connection 63sm14765080wmg.22 - gsmtp Message has been sent

previous code looks to be as follows . I have changed the port as 465 and SMTPSecure as ssl. I had different error

2017-03-14 07:40:31 SMTP ERROR: Failed to connect to server: (0)
2017-03-14 07:40:31 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I have made changes in php.ini by adding send email password and name.

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->SMTPDebug = 2;       // Enable verbose debug output

$mail->isSMTP();          // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP s
$mail->SMTPAuth = true;
$mail->Username = 'sender@gmail.com';                 // SMTP user
$mail->Password = 'password';                           // SMTP pas
$mail->SMTPSecure = 'ssl';          // Enable TLS  'tls'encryption, 
$mail->Port = 465;                  // TCP port to 

$mail->setFrom('sender@gmail.com', 'Eiman');
$mail->addAddress('sender1@gmail.com', 'user1');
$mail->addAddress('sender2@gmail.com');    // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//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 = 'Registration Form';
$mail->Body = 'This body <b>in bold!</b>';
$mail->AltBody = 'This is the body in ';

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

Finally i have changed $mail->SMTPDebug = 0; `now there is no error . I didn't receive any mail.

iman
  • 19
  • 8
  • Which version of PHPmailer are you using? – Martin Mar 14 '17 at 09:16
  • What hosting service are you using? Digitalocean, for example, blocks smtp for default. I struggled a lot before finding this out, and turned out that this wasn't my fault haha. – Lucas Meine Mar 14 '17 at 09:24
  • Did you follow the advice in the links shown? *This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).* – Álvaro González Mar 14 '17 at 09:29

2 Answers2

0

Have you done this in your gmail account?

Allow less secure app turn it ON

GYaN
  • 2,327
  • 4
  • 19
  • 39
0

017-03-14 09:53:25 SERVER -> CLIENT: 220 smtp.gmail.com CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service
CLIENT -> SERVER: STARTTLS
CLIENT -> SERVER: This is the body in plain text for non-HTML mail clients
CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii 2017-03-14 09:53:27
CLIENT -> SERVER: This body in bold! 2017-03-14 09:53:27
SERVER -> CLIENT: 221 2.0.0 closing connection 63sm14765080wmg.22 - gsmtp Message has been sent

This is not an error, see the text Message has been sent

What you're seeing here is the output of the SMTPDebug log.

So simply comment out your debug log:

// $mail->SMTPDebug = 2; 
Martin
  • 22,212
  • 11
  • 70
  • 132
  • SMTPSecure = 'tls' and Port = 587 even after this it shows the above error . – iman Mar 14 '17 at 10:44
  • Can you update your question to reflect that you've tried these changes please – Martin Mar 14 '17 at 11:20
  • @iman I have updated my answer. Please review. – Martin Mar 14 '17 at 11:23
  • This is a chameleon question. First versions showed a debug log with SMTP connection errors. The OP has been editing it until the code finally worked. – Álvaro González Mar 14 '17 at 15:29
  • 1
    @ÁlvaroGonzález I noticed that after updating my answer, but I didn't have the stamina to pull the OP up about it, and to be honest wasn't certain myself on the best way of correcting this. – Martin Mar 14 '17 at 15:34