If you’ve tried to send email with plain mail() in PHP, you probably hit limits fast: no SMTP authentication, no encryption, and Gmail often just ignores you.
In this guide we’ll use PHPMailer with the Gmail SMTP server so your messages are authenticated, encrypted, and much more likely to land in the inbox.
You’ll see the full code, line by line, plus some deployment tips for real-world PHP hosting and production servers.
Let’s keep it simple.
mail() in PHP does not speak authenticated SMTP.
Gmail requires authentication and encryption for @gmail.com addresses.
PHPMailer wraps all the SMTP details in a friendly PHP class.
So instead of fighting with raw headers and sockets, you configure a few properties and let PHPMailer do the dirty work. You control the SMTP host, the port, the encryption, and you get proper error messages when something goes wrong.
For a PHP developer, this means:
More reliable delivery
Easier debugging
Less time guessing why “nothing happens”
Before PHP enters the scene, your Gmail account needs to accept SMTP connections from your app.
Sign in to your Google Account.
Go to the Security section.
Make sure 2‑Step Verification / App Passwords or the equivalent secure option is set up for SMTP access.
Older tutorials talk about “Allow less secure apps.” Google has changed this, but the idea stays the same: you must explicitly allow your script to sign in via SMTP. Whatever Google is calling it this year, follow the official instructions and get a password/token that PHPMailer can use.
Also:
Use a strong password or app password.
Keep it out of your code repository (use environment variables or a config file that isn’t committed).
You have two usual options.
If you use Composer in your project, install PHPMailer like this:
bash
composer require phpmailer/phpmailer
Then, in your PHP script:
php
require DIR . '/vendor/autoload.php';
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
If you’re not using Composer, download PHPMailer from its official GitHub repository, extract it into a PHPMailer folder inside your project, and include the main files:
php
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
Nothing fancy here: you just make sure PHP knows where the PHPMailer classes live.
Here is a full, working example using the Gmail SMTP server:
php
<?php
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
try {
// Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL/TLS
$mail->Port = 465;
// Enable SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'seuemail@gmail.com'; // your Gmail address
$mail->Password = 'senha'; // your Gmail password or app password
// Sender and recipient
$mail->setFrom('seuemail@gmail.com', 'Nome do Remetente');
$mail->addAddress('destinatario@exemplo.com.br');
// Email content
$mail->isHTML(true);
$mail->Subject = 'Assunto Teste';
$mail->Body = 'Corpo da mensagem em <b>HTML</b>';
// Send it
$mail->send();
echo 'Mensagem enviada com sucesso.';
} catch (Exception $e) {
echo "A mensagem não pôde ser enviada. Erro do PHPMailer: {$mail->ErrorInfo}";
}
You replace:
seuemail@gmail.com → your Gmail account
senha → your password or app password
Recipient, subject, and body → whatever fits your app
Run this script from your web server or CLI, and you should see either a success message or a clear error.
Instead of treating this as magic, let’s walk through it part by part. This will save you time when something breaks at 2 a.m.
These lines bring the PHPMailer classes into your script:
php
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
You:
Load the core PHPMailer code.
Load SMTP support.
Load exception handling.
Import the classes into your namespace so you can write new PHPMailer() instead of new PHPMailer\PHPMailer\PHPMailer().
php
$mail = new PHPMailer(true);
Passing true tells PHPMailer to throw exceptions on error. That’s why we wrap everything in try { ... } catch (Exception $e) { ... }. If sending fails, we get a clear message from $mail->ErrorInfo.
php
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
You say:
“Use SMTP” (isSMTP()).
“Connect to smtp.gmail.com.”
“Use encrypted SMTP (SSL/TLS).”
“Use port 465.”
This makes sure the connection between your PHP script and Gmail is encrypted, which is required by Gmail and just the right thing to do anyway.
php
$mail->SMTPAuth = true;
$mail->Username = 'seuemail@gmail.com';
$mail->Password = 'senha';
Here you log in to the Gmail SMTP server.
If authentication fails, PHPMailer will complain loudly. Double-check:
The username: full email address
The password: correct app password or account password
That Gmail is configured to allow SMTP access from this app
php
$mail->setFrom('seuemail@gmail.com', 'Nome do Remetente');
$mail->addAddress('destinatario@exemplo.com.br');
This is who sends and who receives.
You can call addAddress() multiple times to send the same email to several recipients. There are also methods like addCC() and addBCC() if you need them.
php
$mail->isHTML(true);
$mail->Subject = 'Assunto Teste';
$mail->Body = 'Corpo da mensagem em HTML';
isHTML(true) tells PHPMailer to send an HTML email.
Subject is your subject line.
Body is the HTML content.
You can also set $mail->AltBody if you want a plain-text alternative for clients that don’t like HTML.
php
try {
// ... all your config here ...
$mail->send();
echo 'Mensagem enviada com sucesso.';
} catch (Exception $e) {
echo "A mensagem não pôde ser enviada. Erro do PHPMailer: {$mail->ErrorInfo}";
}
PHPMailer tries to send the email. If something goes wrong (wrong password, blocked port, DNS issue, whatever), it throws an exception and you see the error in $mail->ErrorInfo.
This is where PHPMailer really helps: you get meaningful error messages instead of silence.
Sending email from localhost is one thing. Putting this into production is another story.
When you move to a real server or cloud hosting provider, you have to think about:
Are outbound SMTP ports (465 or 587) open?
Does the firewall allow connections to smtp.gmail.com?
Does the hosting provider block SMTP to prevent spam?
If you’re doing serious PHP email work or need to test bulk notifications, you often want your own environment where you control the ports and can tweak everything.
That’s where a dedicated or VPS-style setup shines. You spin up a machine, deploy your PHP code, and configure SMTP exactly how you want. If you want that without waiting days for provisioning or fighting random port blocks, it’s worth looking at hosting that is friendly to developers and email workloads.
With that, your local test script becomes a real production tool: forms, notifications, password resets, whatever your app needs.
You’ve seen, step by step, how to send email with PHP using PHPMailer and the Gmail SMTP server: prepare your Gmail account, install PHPMailer, configure SMTP, and handle errors cleanly. This gives you more stable, more secure, and easier‑to‑debug email delivery than the basic mail() function.
When you’re ready to move from localhost to a real server, you need hosting that doesn’t fight your SMTP traffic, and that’s 👉 why GTHost is suitable for PHP email and SMTP testing scenarios: you get fast deployment, control over your environment, and a place where your PHPMailer + Gmail setup can behave exactly the way you expect.