Sign In
Sign In

How to Use Google SMTP Server

How to Use Google SMTP Server
Hostman Team
Technical writer
Mail
18.10.2024
Reading time: 7 min

SMTP stands for "Simple Mail Transfer Protocol." As the name suggests, it is a protocol for sending and delivering emails to the recipient.

What is an SMTP server?

An SMTP server is a server responsible for ensuring the proper functioning of the SMTP protocol. Its main role is to act as a relay between the sender and the recipient. The SMTP server performs two essential tasks:

  1. Verifies the configuration of the device attempting to send a message and permits it to do so.
  2. Sends the message to the specified address and receives a response code.

An SMTP server's responsibility ends here — it only handles sending emails. Receiving emails on the recipient's side is managed by other protocols, such as POP3 and IMAP.

Basic steps of sending an email:

  1. The sender's server gathers the necessary information — such as the sender's and recipient's addresses, along with the message itself containing the required fields.
  2. The sender's server identifies the recipient's email provider by analyzing the recipient's email address and requests the IP address of the recipient's mail server.
  3. The sender's server receives a response from the recipient's server.
  4. If there is no response from the recipient's server, the sender's server will attempt to establish a connection multiple times. If there is still no response, an error code is returned.

The standard port for SMTP is 25, but other ports like 465 and 587 are also used for secure SSL connections and mandatory authentication. It's worth noting that some providers block port 25 to prevent spam, so it's a good idea to check this with your provider.

For SMTP, you can use cloud servers in almost any configuration. However, if you plan to send large volumes of emails or need to ensure that your emails are not marked as spam, using Google's SMTP server is recommended.

Advantages of Using Google's SMTP Server

  1. Cost: One of the most obvious advantages is that Google SMTP is entirely free — you only need a Google account to use it.
  2. Pre-configured: Setting up and managing a mail server is quite complex and requires theoretical knowledge of network protocols and practical experience with server configuration. Using an external solution like Google's saves a lot of time configuring the server.
  3. Backup: You don't need to worry about the server's uptime — if something goes wrong in the middle of the night, Google's team will handle it. Google also takes care of backing up both sent and received emails, saving you the trouble of ensuring the security of valuable or confidential information.
  4. Indexing: Another advantage of storing emails on Google's servers is that indexing and searching through emails are powered by Google's computational resources. If you use the same SMTP for Gmail, emails will automatically appear in the "Sent" and "Inbox" folders, keeping everything organized in one place.
  5. Spam Protection: One of the biggest challenges with managing your own mail server is preventing emails from being marked as spam. When sending through Google's SMTP server, you can be confident that the email will arrive at the recipient's inbox just like any other Gmail message. Since Google doesn't use the standard port 25 for sending emails, the likelihood of the message being marked as spam or blocked by the recipient's provider is reduced.

Disadvantages of Using a Third-Party SMTP Server

  1. Data storage on a remote server: One common concern with third-party SMTP servers is that all your communication is stored under Google's control. However, privacy concerns about keeping emails on your own servers are still valid, especially if you are communicating with average users who are unlikely to use their own SMTP servers.
  2. Email limits: Google limits the number of emails sent per day to 100. This limit is generally sufficient if you're testing the SMTP sending mechanism or your project doesn't require large volumes of outgoing emails.

Setting Up Google SMTP

You'll need access to a Google account to set up the Google SMTP service. In most cases, a simple login and password are sufficient. Still, if you have enabled two-factor authentication (which is highly recommended), you must generate an app-specific password.

Here are the settings you'll need to configure Google's SMTP server:

  • SMTP Server (Outgoing Mail Server): smtp.google.com
  • SMTP Username: Your full email address
  • SMTP Password: Your Google account password or the app password you generated
  • SMTP Port: 465
  • Requires TLS/SSL?: Yes

Note that Google will automatically overwrite the From header of any email you send via the SMTP server if it doesn't match your default email address. For instance, if you try to send an email from a non-existent address, Google will replace it with your real one. This is standard behavior, but you can adjust this in your email settings.

Email Clients

Besides sending automated emails using Google's SMTP server, you can also use these settings to connect with email clients like Thunderbird or Outlook. This way, you can send emails without using a browser or Google's standard client.

However, to receive emails from your Google account in another client, you'll need to use POP3 or IMAP protocols. These settings are available in the same place as other Gmail mail settings, under the "Forwarding and POP/IMAP" section.

Testing Email Sending

We'll write a simple PHP script to test the configuration provided above. We'll send the email using the PHPMailer package, which we can install via the Composer dependency manager:

composer require phpmailer/phpmailer

Next, create a file index.php where we will specify the SMTP server settings and attempt to send a test email.

<?php
error_reporting(E_ALL); // Show all errors
// Include PHPMailer
require dirname(__FILE__) . '/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
// Specify that we are using SMTP
$mail->isSMTP();
// Enable debugging output for testing purposes
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
// Provide the SMTP credentials
$mail->Host = 'smtp.gmail.com'; // SMTP host
$mail->Port = 587; // SMTP port
$mail->SMTPSecure = 'tls'; // Encryption
$mail->SMTPAuth = true; // Enable authentication
$mail->Username = "[email protected]"; // Your Google account email
$mail->Password = "62584jattjjtmxnpwf124"; // App-specific password
// Specify sender and recipient information
$mail->setFrom('[email protected]', 'Test Sender Hostman); // Sender
$mail->addReplyTo('[email protected]', 'First Last'); // Reply-To address
$mail->addAddress('[email protected]', 'James Smith'); // Recipient
// Subject and content
$mail->Subject = 'Hostman: Google SMTP Test'; // Subject line
$mail->msgHTML('<h1>Hello, Hostman</h1>'); // HTML content
$mail->AltBody = 'This is a plain-text message body'; // Plain-text fallback
// Output the result
if (!$mail->send()) {
echo "Mailer Error:". $mail->ErrorInfo;
} else {
echo "Message sent!";
}

You can use the same script by replacing the credentials and recipients with your own information, including the Reply-To address.

Now, execute the PHP script through the browser by loading the page. If everything is set up correctly, you'll see the output of the email being sent. If any credentials are incorrect, PHPMailer will display an error message.

Next, open your email client and check if the email has arrived. Everything should work as expected, and you'll also see the email in the Sent folder in your Gmail account.

Conclusion

In this article, we explored the advantages of using Google's SMTP server, including the free setup and maintenance, reliable backup, and reduced likelihood of emails being marked as spam.

Additionally, we wrote a simple PHP script to demonstrate how to send emails via Google SMTP.

We also discussed some limitations and drawbacks of using third-party email services. If you decide to set up your own mail server, you can use Hostman's cloud servers

Mail
18.10.2024
Reading time: 7 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us