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

Habiba MeherHabiba Meher
3 min read

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:

ItemExample
SMTP Hostsmtp.gmail.com (Gmail), smtp.sendgrid.net
Port465 (SSL), 587 (TLS)
Email Addressyourname@example.com
App PasswordRequired for Gmail with 2FA
Encryption TypeSSL/TLS

🧪 Gmail SMTP Example

To use Gmail SMTP, follow these steps:

Step 1: Generate an App Password

  1. Go to your Google Account

  2. Enable 2-Step Verification

  3. Under Security > App Passwords, generate one for "Mail"

Step 2: SMTP Settings

FieldValue
SMTP Hostsmtp.gmail.com
SMTP Port587 (TLS) or 465 (SSL)
Usernameyour Gmail address
PasswordYour app password
EncryptionTLS 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

ProviderSMTP ServerFree Tier
SendGridsmtp.sendgrid.net100 emails/day
Mailgunsmtp.mailgun.org5,000 emails/month
Zoho Mailsmtp.zoho.comFree plan available
Amazon SESVaries by regionLow-cost (pay-per-use)

🧯 Common SMTP Issues & Fixes

IssueSolution
Authentication failedUse correct email and app password
Connection timeoutMake sure port 465 or 587 is open on server
Spam folder issuesSet up SPF, DKIM, and DMARC records
No email receivedCheck 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 managers

  • Set 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!

0
Subscribe to my newsletter

Read articles from Habiba Meher directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Habiba Meher
Habiba Meher