By maxwell


2019-01-11 11:59:54 8 Comments

I'm trying to send emails from a Gmail business account, But it's not working.

Here is the code:

//Including PHPMailer files
require_once('phpmailer/src/phpmailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');

//Emails list
$recipients = array('[email protected]', '[email protected]', '[email protected]');

//Initializing PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer();                              // Passing `true` enables exceptions

try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'tls://smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = '*******';                          // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]n.com', 'Admin');
    $mail->addReplyTo('[email protected]', 'Admin');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Subject';
    $mail->Body    = 'Hello World!';
    $mail->AltBody = 'Hello World';

    foreach ($recipients as $recipient) {
        $mail->addAddress($recipient);
        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $recipient) . ')<br />';
        }
        // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();
    }

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

The result:

2019-01-02 16:19:05 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP h2sm20095348itk.0 - gsmtp
2019-01-02 16:19:05 CLIENT -> SERVER: EHLO mydomain.com
2019-01-02 16:19:05 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2607:1b00:93b2:e42c::914a]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-01-02 16:19:05 CLIENT -> SERVER: STARTTLS
2019-01-02 16:19:05 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-01-02 16:19:05 CLIENT -> SERVER: EHLO mydomain.com
2019-01-02 16:19:05 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2607:1b00:93b2:e42c::914a]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-01-02 16:19:05 CLIENT -> SERVER: AUTH LOGIN
2019-01-02 16:19:05 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-01-02 16:19:05 CLIENT -> SERVER: <credentials hidden>
2019-01-02 16:19:05 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-01-02 16:19:05 CLIENT -> SERVER: <credentials hidden>
2019-01-02 16:19:05 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials h2sm20095348itk.0 - gsmtp
2019-01-02 16:19:05 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials h2sm20095348itk.0 - gsmtp
SMTP Error: Could not authenticate.
2019-01-02 16:19:05 CLIENT -> SERVER: QUIT
2019-01-02 16:19:05 SERVER -> CLIENT: 221 2.0.0 closing connection h2sm20095348itk.0 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error ([email protected]) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I had a similar issue with a personal account, I fixed it by allowing low secured apps, But I don't know how to fix this one.

I tried both ssl and tls, But still not working.

How to fix this issue?

0 comments

Related Questions

Sponsored Content

70 Answered Questions

76 Answered Questions

[SOLVED] How to validate an email address in JavaScript?

13 Answered Questions

[SOLVED] Send email using the GMail SMTP server from a PHP page

  • 2009-04-03 02:47:59
  • skb
  • 737248 View
  • 361 Score
  • 13 Answer
  • Tags:   php email smtp gmail

2 Answered Questions

[SOLVED] PHPMailer ERROR USING GMAIL: Mailer Error: SMTP connect()

21 Answered Questions

[SOLVED] Sending email in .NET through Gmail

1 Answered Questions

Send mail SMTP GMail and PHPMailer

1 Answered Questions

Wordpress site can not send or receive Email

  • 2018-01-30 13:47:01
  • v patel
  • 188 View
  • 0 Score
  • 1 Answer
  • Tags:   wordpress email

1 Answered Questions

Having issues with my email

  • 2017-04-02 17:20:00
  • Imraan Nundloll
  • 190 View
  • -1 Score
  • 1 Answer
  • Tags:   java email javamail

0 Answered Questions

PhpMailer with Gmail on WAMP server

1 Answered Questions

[SOLVED] Get full error message from PHPMailer Exception

  • 2016-09-19 05:11:03
  • US-1234
  • 9076 View
  • 1 Score
  • 1 Answer
  • Tags:   php phpmailer

Sponsored Content