January 25, 2017
3 min read
How to set up Gmail SMTP is quite easy to do through a few simple steps. Not everyone knows that they can benefit from Google’s free SMTP servers that are available for sending email.
This can be a perfect solution for users who cannot use the SMTP server of internet and hosting service providers, and have various problems sending email.
In this tutorial, we will cover how to use Gmail’s SMTP server which is available for free. You can also find detailed instructions for sending email via PHP mail.
Benefits of Using External SMTP Service
Here are some of the benefits of using an external SMTP service.
- The email can be confirmed to be delivered to the destination.
- You do not need to setup a server (if you are using a VPS).
- Servers are rarely blacklisted, so your email is less likely to come in as SPAM.
Email Sending Limit with Gmail SMTP
Email sending limit provided by Gmail SMTP is 100 emails per day. If you have reached the limit, then you cannot send emails for the next 24 hours until it is automatically reset.
Before proceeding to the Instructions for Setting Gmail SMTP, here are the things you will need:
- Gmail or G Suite account.
- Access to your hosting control panel.
Also Read: Rabbani Case Study: When Should You Switch to a VPS?
Step 1: Get Google SMTP Server Settings
To use the SMTP server from Google, you first need to open access Less secure apps. Google began blocking applications and devices that were deemed not to use modern security standards. To avoid this, please activate your Less secure apps account first on the link following.
IMPORTANT: If 2-step verification your Google account is active, then less secure apps cannot be activated. In this case, you need login using app password.
Here are Google’s SMTP details:
- SMTP Server: smtp.gmail.com
- SMTP Username: your email address, for example email@example.com
- SMTP Password: Your Gmail Password
- SMTP Port: 465
- TLS/SSL: Required
If you want to take advantage of the IMAP protocol and save all outgoing email in the sent email folder in Gmail, please follow these steps.
1. Login to your Gmail account and click on the menu Settings.
2. Select tab Forwarding and POP/IMAP and click Enable IMAP as seen in the image below.
3. Please click the button Save Changes at the very bottom.
Step 2: Using Google SMTP with PHP Mail
In this step, you will learn how to set up Gmail smtp for sending emails using PHP mail. Knowing how to send emails using PHP will come in handy when you are learning to code or just want to build a contact form for a website.
There are many php mail scripts available, in the example below we will use PHPMailer:
1. Please open PHPMailer repository on GitHub, then download the script via the menu Clone or download > Download ZIP. Please save it on your computer.
2. Please upload the file into your hosting. To upload it, you can use an FTP client such as FileZilla or a file manager that is already available on your hosting panel.
PHPMailer already has an example configuration file with the name gmail.phps for use with Google SMTP servers. The file is in the folder PHPMailer-master/examples. Please edit the script, according to your email data, as shown in the script below which is marked in bold:
<?php date_default_timezone_set('Etc/UTC'); require '../PHPMailerAutoload.php'; //Membuat instance PHPMailer baru $mail = new PHPMailer; //Memberi tahu PHPMailer untuk menggunakan SMTP $mail->isSMTP(); //Mengaktifkan SMTP debugging // 0 = off (digunakan untuk production) // 1 = pesan client // 2 = pesan client dan server $mail->SMTPDebug = 2; //HTML-friendly debug output $mail->Debugoutput="html"; //hostname dari mail server $mail->Host="smtp.gmail.com"; // gunakan // $mail->Host = gethostbyname('smtp.gmail.com'); // jika jaringan Anda tidak mendukung SMTP melalui IPv6 //Atur SMTP port - 587 untuk dikonfirmasi TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set sistem enkripsi untuk menggunakan - ssl (deprecated) atau tls $mail->SMTPSecure="tls"; //SMTP authentication $mail->SMTPAuth = true; //Username yang digunakan untuk SMTP authentication - gunakan email gmail $mail->Username = "firstname.lastname@example.org"; //Password yang digunakan untuk SMTP authentication $mail->Password = "passwordanda"; //Email pengirim $mail->setFrom('email@example.com', 'First Last'); //Alamat email alternatif balasan $mail->addReplyTo('firstname.lastname@example.org', 'First Last'); //Email tujuan $mail->addAddress('email@example.com', 'John Doe'); //Subject email $mail->Subject="PHPMailer GMail SMTP test"; //Membaca isi pesan HTML dari file eksternal, mengkonversi gambar yang di embed, //Mengubah HTML menjadi basic plain-text $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); //Replace plain text body dengan cara manual $mail->AltBody = 'This is a plain-text message body'; //Attach file gambar $mail->addAttachment('images/phpmailer_mini.png'); //mengirim pesan, mengecek error if (!$mail->send()) echo "Email Error: " . $mail->ErrorInfo; else echo "Pesan Terkirim!"; Contact GitHub API Training Shop Blog About
In order for the script to work with Google’s SMTP server, you will need to edit it and change some settings like username, password, sender email and destination email. You also need to rename the file gmail.phps Becomes gmail.php so that it can be accessed via a browser. You can use the same file manager or FTP client to rename it.
After you have finished editing, please access the script via a browser. If you upload the PHPMailer-master folder into the public_htmlthe link that will be accessed will be like this: http://yourdomain.com/PHPMailer-master/examples. If all the configurations are done correctly, you will see a success message which means the email message has been sent successfully.
Also Read: How to Setup CloudFlare in WordPress with Easy Steps
Now you can do how to setup Gmail SMTP and configure Google account. Apart from that, you can also send emails via PHP mail.
If you’ve tried doing it yourself before, or have questions about how to set up Gmail SMTP, don’t hesitate to write them in the comments column below?