Error Google SMTP Not Accepted (EGSNA)

I get the following error when trying to use my GMail account to send email from my PHP application with SwiftMailer.

535-5.7.8 Username and Password not accepted

Here is my SwiftMailer code:

$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
    ->setUsername('ayrshireminiscontact@gmail.com')
    ->setPassword('~password-in-here~');

$mailer  = Swift_Mailer::newInstance($transporter);
$message = Swift_Message::newInstance('Portfolio Enquiry')
    ->setFrom(array('ayrshireminiscontact@gmail.com' => 'CRMPicco Portfolio Enquiry'))
    ->setTo(array('picco@crmpicco.co.uk' => 'A name'))
    ->setBody($email_body);

$result = $mailer->send($message);

This is the entry in my Apache error log and stack trace.

[Wed Aug 18 22:06:10.284728 2015] [:error] [pid 9298] [client 10.0.0.1:64806] PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at\r\n535 5.7.8  https://support.google.com/mail/answer/14257  gj13sm2191931wic.22 - gsmtp\r\n"' in /var/www/portfolio/swiftmailer/lib/ classes/Swift/Transport/AbstractSmtpTransport.php:383\nStack trace:\n#0 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('535-5.7.8 Usern...', Array)\n#1 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(243): Swift_Transport_AbstractSmtpTransport->executeCommand('RSET\r\n', Array, Array)\n#2 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php(54): Swift_Transport_EsmtpTransport->executeCommand('RSET\r\n', Array)\n#3 /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php(176): Swift_Transport_Esmtp_Auth_XOAuth2Authent in /var/www/portfolio/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 383, referer:  https://crmpicco.dev/

I have tried  http://www.google.com/accounts/DisplayUnlockCaptcha  and I can log in to my account via a web browser without any issues. This happens on my local development machine and on a publicly accessible site. Is this an issue with my GMail account or my implementation?

Completion

You need to allow  access from less secure apps , as IMAP over passwords is fundamentally insecure. (Cannot be reversed)

Better yet, switch to OAuth authentication.

Free Send Email Using Google SMTP

Not everyone knows that they can take advantage of the free Google SMTP server to send emails. This can be a major solution for those, who cannot use the SMTP server provided by their ISP or hosting, as well as those who are experiencing various email delivery issues. In this tutorial, you will learn how to use the free Google SMTP server. You will also find detailed instructions on how to use it to send emails via PHP mailer and WordPress plugins.

Simple Mail Transfer Protocol (SMTP) is an Internet standard for transmitting electronic mail (email). First defined by RFC 821 in 1982, it was updated in 2008 with additional technology by RFC 5321, which is the protocol in widespread use today.

Although e-mail servers and other mail transfer agents use SMTP to send and receive e-mail messages, user-level client e-mail applications typically use SMTP only to send messages to e-mail servers. To retrieve messages, client applications typically use IMAP or POP3.

SMTP communication uses TCP port 25. Clients on the other hand, often send email to the server using port 587 or 465. Previously these ports were rarely used, but since the security enhancements in the RFC 8314 standard, their use is now highly recommended. SMTP connections on port 25 or 587 can be secured using an extra command (STARTTLS).

Although proprietary systems (such as Microsoft Exchange and IBM Notes) and webmail systems (such as Outlook.com, Gmail and Yahoo Mail) use their own non-standard protocols to access mailbox accounts on their own email servers, all use SMTP when sending or receiving email from outside their own systems.

Benefits of Using an External SMTP Service

Although, most hosting (e.g. Hostinger) and ISPs offer SMTP support, there are some benefits to using an external SMTP service:

  • They can ensure better email delivery.
  • You don't need to set up your own server (if you use VPS).
  • Their servers are less likely to be blacklisted, which means your emails are less likely to be labeled as SPAM.

Google SMTP Free Sending Limits

Google limits outgoing emails to 100 per day. Once the limit is reached, you will not be able to send emails for the next 24 hours until it is reset. More information about Google email limits can be found  here .

Before starting this guide, you will need the following things:

  • Gmail or G Suite account
  • Access to your hosting control panel

Step 1 - Getting Google SMTP Server Settings

First of all, to use the free SMTP server from Google, you need to allow access for less secure applications. Google has started blocking applications and devices that, in their opinion, do not use modern security standards. However, access for less secure applications can be easily enabled  here .

IMPORTANT: If you use 2-factor authentication to access your Google account, access for less secure apps cannot be enabled. In this case, you must sign in using your App password.

You can find Google SMTP details below:

  • SMTP Server: smtp.gmail.com
  • SMTP Username: Your full Gmail username (email address), for example  youremail@gmail.com
  • SMTP Password: Your Gmail password.
  • SMTP Port: 587
  • TLS/SSL: Required.

If you want to take advantage of the IMAP protocol and save all outgoing emails to your Gmail Sent folder, follow these steps:

1. Log in to your Gmail account and access the Settings section.

2. Jump to the Forwarding and POP/IMAP tab, then enable IMAP.

3. Press the Save Changes button at the bottom of the page when you are done.

Step 2 - Using Google SMTP

In this step, you will learn how to use Google's free SMTP to send emails using the PHP mailer plugin and WordPress. Knowing how to send emails via PHP is useful whenever you are learning to code or just want to create a simple contact form for your website.

Option 2.1 - Using PHPMailer

There are many PHP email scripts available. In this case, we will use PHPMailer:

1. Head over to the  PHPMailer repository  on GitHub and download the script by hitting the Clone or download -> Download ZIP button. Save it to your computer.

2. Upload the archive to your hosting account. You can use an FTP client or the file manager provided by your hosting for this. If you are not sure how to upload files to your hosting account, check out this tutorial (coming soon).

PHPMailer already has an example configuration file called gmail.phps for use with Google's SMTP server. This is located in the PHPMailer-master/examples folder.

<?php
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
Contact GitHub API Training Shop Blog About

In order for this script to work with Google SMTP server, you will need to edit it and change settings such as username, password and email recipient address. You will also need to rename this file from gmail.phps to gmail.php so that it can be accessed via browser. You can use the same file manager or FTP client to rename and edit the file with your details.

Once you are done editing, simply open the script with your browser. If you have uploaded the PHPMailer-master folder to the public_html folder or in the www folder on your local wamp server or htdoc on your local xampp, it should be accessible via  http://yourdomain.com/PHPMailer-master/examples . If everything is configured correctly, you should see a success message indicating that the email message has been successfully sent.

Option 2.2 - Using Wordpress Mail SMTP Plugin

The easiest way to use external SMTP in WordPress is to use the Mail SMTP plugin.

First of all access your WordPress admin dashboard and go to Plugins > Add New section. Install and activate the Mail SMTP plugin.

Now access the SMTP settings page, go to Plugins -> Installed Plugins and click Settings.

You have to fill all the required details of your SMTP server.

  1. SMTP Host - Enter smtp.gmail.com.
  2. SMTP Port - The default Gmail SMTP server ports are 465 for SSL and 587 for TSL.
  3. Encryption - It is always recommended to use encryption. Select according to the port you have specified.
  4. Authentication - Select Yes as SMTP authentication is required.
  5. Username - your Gmail address.
  6. Password - your Gmail password.

To save your changes, click the Save Changes button at the bottom. To make sure your settings are working properly, you can test them by sending a test email to your personal mail account. Enter your email address and click Send Test.

If you receive the email you sent then email sending is working properly. However, if you get an error message, check your email settings or contact your hosting support team for assistance.

Conclusion

After completing this tutorial, you have learned how to configure your Google account and use the free Google SMTP server. You have also learned how to send emails via PHPMailer.


Post a Comment

Previous Next

نموذج الاتصال