How to Set Up SMTP to Send Emails from Your Website or Application


Sending emails from your website or app — for contact forms, password resets, order confirmations, and more — requires a reliable method. That’s where SMTP (Simple Mail Transfer Protocol) comes in.
In this guide, we’ll cover what SMTP is, why it’s better than using native functions, and how to set it up using Gmail or any other SMTP provider.
🔍 What is SMTP?
SMTP is the industry-standard protocol used to send outgoing emails from a server to an email client (like Gmail, Outlook, or Yahoo). It handles message delivery while ensuring emails don’t get flagged as spam.
🛑 Why Not Use PHP mail()
or Default Email Functions?
While mail()
works in basic PHP scripts, it lacks:
📬 Deliverability — more likely to land in spam
🔐 Security — no encryption or authentication
📊 Analytics — no feedback on delivery or opens
Using SMTP with authentication, TLS/SSL, and a trusted mail server improves email success rates dramatically.
✅ SMTP Setup Requirements
Before configuring, you’ll need:
Item | Example |
SMTP Host | smtp.gmail.com (Gmail), smtp.sendgrid.net |
Port | 465 (SSL), 587 (TLS) |
Email Address | yourname@example.com |
App Password | Required for Gmail with 2FA |
Encryption Type | SSL/TLS |
🧪 Gmail SMTP Example
To use Gmail SMTP, follow these steps:
Step 1: Generate an App Password
Go to your Google Account
Enable 2-Step Verification
Under Security > App Passwords, generate one for "Mail"
Step 2: SMTP Settings
Field | Value |
SMTP Host | smtp.gmail.com |
SMTP Port | 587 (TLS) or 465 (SSL) |
Username | your Gmail address |
Password | Your app password |
Encryption | TLS or SSL |
🧑💻 SMTP Code Example (PHP + PHPMailer)
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourname@gmail.com';
$mail->Password = 'your-app-password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('yourname@gmail.com', 'Your Name');
$mail->addAddress('recipient@example.com');
$mail->Subject = 'SMTP Test Email';
$mail->Body = 'This is a test email sent using Gmail SMTP via PHPMailer.';
$mail->send();
🔁 Alternative SMTP Providers
Provider | SMTP Server | Free Tier |
SendGrid | smtp.sendgrid.net | 100 emails/day |
Mailgun | smtp.mailgun.org | 5,000 emails/month |
Zoho Mail | smtp.zoho.com | Free plan available |
Amazon SES | Varies by region | Low-cost (pay-per-use) |
🧯 Common SMTP Issues & Fixes
Issue | Solution |
Authentication failed | Use correct email and app password |
Connection timeout | Make sure port 465 or 587 is open on server |
Spam folder issues | Set up SPF, DKIM, and DMARC records |
No email received | Check SMTP logs or enable debug mode |
🛡️ Security Best Practices
Use App Passwords or secure API keys
Always use TLS/SSL encryption
Never hardcode credentials in code — use
.env
or secret managersSet up domain authentication (SPF/DKIM/DMARC)
🏁 Final Thoughts
SMTP is the gold standard for sending emails from your web server. Whether you’re working with Gmail, SendGrid, or Amazon SES, a proper SMTP setup ensures your emails land where they should — in the inbox, not the spam folder.
Need help integrating SMTP into your WordPress, Laravel, or Node.js project? Drop your tech stack in the comments, and we’ll guide you!
Subscribe to my newsletter
Read articles from Habiba Meher directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
