How to Send an Email via Gmail SMTP Server using PHP

the author voluntarily contributed this tutorial as part of the pepipost write-to-contribute program.

intro

In this tutorial you will learn how to send email in php using phpmailer library via gmail smtp. like phpmailer there are few more good send email libraries in php e.g. pear::mail interface, swiftmailer etc which can help you to easily send mail in php using gmail smtp.

requirements

Before we start with the steps on how to send mail using smtp in php example, let’s first see what are some limits with gmail smtp servers and how to overcome some of them:

  • gmail limits the number of recipients in a single email and the number of emails that can be sent per day. the current limit is 500 emails in a day or 500 recipients in a single email. you can’t really increase this limit. if you want to send above these limits, you need to integrate with a third party email delivery platform, like pepipost sendgrid, etc.
  • upon reaching the threshold limits, you will not be able to send messages for the next 24 hours. once this temporary suspension period ends, the counter is automatically reset and the user can resume sending emails.
  • By default, third-party apps/code are not allowed to send emails using their gmail account. and so there are some configurations that need to be done on your end:

how to enable sending email in gmail?

  1. Before sending emails using the gmail smtp server, you need to make some permission and security level settings in your google account security settings.
  2. make sure that two-step-verification is disabled.
  3. turn on turn on “less secure application” access or click here.
  4. if 2-step verification is enabled, then you will need to create an app password for your app or device.
  5. for security measures, google may require you to complete this additional step on startup session. click here to allow access to your google account using the new device/application.
See Also:  Mail is not working correctly on your Mac? Heres how to fix it

note: security changes may take an hour or more to reflect

writing php code to send email using gmail smtp

step 1: download the phpmailer library from this github link. to directly download the .zip file, use this link.

unzip master.zip to your application directory and run the following command from your application directory.

composer is the recommended way to install phpmailer.

step 2: writing the php code to make an smtp connection

  • Using your gmail credentials, connect to the host “smtp.gmail.com”
  • click here for more phpmailer examples and tutorials

step 3: include packages and files for the phpmailer and smtp protocol:

step 4: initialize php mailer and set smtp as mail protocol:

Step 5: Set the parameters required to make an smtp connection, such as server, port, and account credentials. SSL and TLS are cryptographic protocols that provide authentication and data encryption between servers, machines, and applications operating on a network. ssl is the predecessor of tls.

step 6: set the required parameters for the email header and body:

Step 7: Send the email and catch any required exceptions:

working php code to send email using smtp server

click here to download full working php code to send email using gmail smtp server. you just need to change some values ​​and it should work.

See Also:  100 Free Mail-Order Catalogs for Delivery (2023)

list of possible errors and exceptions

error 1: password command failed: 534-5.7.9 application specific password required🔗

This mainly happens when the smtp mail server credentials were correct but the application specific password was not provided.

error 2: smtp error: password command failed: 535-5.7.8 username and password not accepted🔗

If you’ve encountered this error, it’s mainly because the application-specific password is incorrect.

error 3: invalid address: (to): recipient-email🔗

If you’ve encountered this error, it’s mainly because the recipient’s email is invalid.

error 4: smtp: smtp server does not support authentication🔗

If you’ve encountered this error, it’s mainly because your code doesn’t seem to be using tls/ssl, which is required to send mail to google, it should also be using port 587 or 465.

reconfirm once if the value of $host is set as follows:

error 5: stream_socket_client(): could not connect to smtp.gmail.com🔗

If you have encountered this error, this is mainly because your server is unable to connect to smtp.gmail.com on port 587. This is mainly because your hosting provider has a strict firewall rule that blocks your server to connect to any other external server via smtp port 587.

error 6: smtp server error: 5.5.1 authentication required. more information in 530 5.5.1🔗

If you have encountered this error, it is mainly because you have enabled 2fa on your gmail account or have not enabled access to a less secure app. Please read the prerequisites above to address this issue.

See Also:  December 2015 - Best Free Baby Stuff

error 7: message: fsockopen(): could not connect to ssl://smtp.gmail.com🔗

If you have encountered this error, it is mainly due to an ssl issue. you need to enable ssl in the php.ini file of your server configuration. in case you are using a xampp server, check if it is enabled or not. you’ll get that in the php info.

error 8: emails with php code being sent as spam

This is definitely not a bug, but it is definitely a cause for concern. One of the most common reasons emails end up in spam is blacklisting. you should check if your domain or ip address is blacklisted.

step-by-step debugger output after sending an email successfully

conclusion

I hope the steps explained above were helpful and you were able to successfully send mail from your gmail smtp server using php. feel free to contribute, should you run into any issues not listed as part of this tutorial. use the comment section below to ask/share any feedback.

<? happy coding?>

Leave a Reply

Your email address will not be published. Required fields are marked *