SMTP stands for "Simple Mail Transfer Protocol." As the name suggests, it is a protocol for sending and delivering emails to the recipient.
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:
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:
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.
25
for sending emails, the likelihood of the message being marked as spam or blocked by the recipient's provider is reduced.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.google.com
465
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.
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.
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.
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.