Send an Email via Gmail SMTP Server using PHP
In this Blog, you will learn how to send email in PHP using PHPMailer library via Gmail SMTP. Like PHPMailer, there are few more good email sending libraries in PHP e.g. PEAR::Mail interface, Swiftmailer etc which can help you easily send mail in PHP using Gmail SMTP.
PrerequisitesBefore starting with steps on how to send mail using SMTP in PHP example, lets first see what are few limits with Gmail SMTP servers and how to overcome some of these:
- 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 limit, then you need to integrate with third-party email delivery platform like Pepipost Sendgrid etc.
- On reaching threshold limits, you won’t be able to send messages for the next 24 hours. Once this temporary suspension period is over, the counter gets reset automatically, and the user can resume sending emails.
- By default, any third-party apps/codes are not allowed to send emails using your Gmail account. And, hence there are few settings which need to be done at your end:
How To Enable Email Sending In Gmail?
- Before sending emails using the Gmail’s SMTP Server. you to make some of the security and permission level settings under your Google Account Security Setting.
- Make sure that 2-Step-Verification is disabled.
- Turn ON the “Less Secure App” access.
- If 2-step-verification is enabled, then you will have to create app password for your application or device.
- For security measures, Google may require you to complete this additional step while signing-in. Click here to allow access to your Google account using the new device/app.
Writing the PHP Code to Send Email using Gmail SMTPStep 1: Download PHPMailer library from this github. Unzip the master.zip in your application directory and run 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 host “smtp.gmail.com”