SendGrid SMTP vs API — Choosing the Right Email Delivery Method
In this tutorial, you will learn about SendGrid SMTP vs API. We cover key concepts, practical examples, and best practices to help you master this topic.
SendGrid offers two methods for sending email: SMTP (Simple Mail Transfer Protocol) and the REST API. SMTP is protocol-based and simpler for existing email clients, while the REST API offers richer features like categories, templates, and tracking.
What You'll Learn
- The differences between SendGrid SMTP and API
- When to use SMTP vs the REST API
- How to configure both methods
Why It Matters
Choosing the wrong method adds complexity or limits functionality. SMTP is universal but lacks SendGrid's advanced features. The REST API unlocks the full SendGrid platform but requires more integration work. Your choice depends on your existing infrastructure and feature requirements.
Real-World Use
DodaTech uses both: the REST API for transactional emails (welcome, password reset, invoices) where templates and categories are needed, and SMTP for legacy internal notification scripts that already use standard email libraries.
flowchart LR
A["Your Application"] --> B{"Which method?"}
B -->|"SMTP"| C["Standard email\nlibraries (smtplib)"]
B -->|"REST API"| D["SendGrid SDK\nor HTTP client"]
C --> E["SendGrid SMTP\nServer"]
D --> F["SendGrid v3 API"]
E --> G["Email Delivered\n(fewer features)"]
F --> H["Email Delivered\n(templates, tracking,\ncategories)"]
style E fill:#fef3c7,stroke:#d97706
style F fill:#dbeafe,stroke:#2563eb
SMTP Configuration
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_via_smtp(to_email, subject, html_content):
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = 'noreply@dodatech.com'
msg['To'] = to_email
part = MIMEText(html_content, 'html')
msg.attach(part)
server = smtplib.SMTP('smtp.sendgrid.net', 587)
server.starttls()
server.login('apikey', 'YOUR_SENDGRID_API_KEY')
server.send_message(msg)
server.quit()
print(f"Email sent to {to_email} via SMTP")
REST API Method
import requests
def send_via_api(to_email, subject, html_content):
response = requests.post(
"https://api.sendgrid.com/v3/mail/send",
headers={
"Authorization": "Bearer YOUR_SENDGRID_API_KEY",
"Content-Type": "application/json"
},
json={
"personalizations": [{"to": [{"email": to_email}]}],
"from": {"email": "noreply@dodatech.com"},
"subject": subject,
"content": [{
"type": "text/html",
"value": html_content
}],
"categories": ["transactional"],
"tracking_settings": {
"open_tracking": {"enable": True},
"click_tracking": {"enable": True}
}
}
)
if response.status_code == 202:
print(f"Email sent to {to_email} via API")
else:
print(f"Error: {response.status_code}, {response.text}")
Feature Comparison
| Feature | SMTP | REST API |
|---|---|---|
| Categories | No | Yes |
| Dynamic Templates | No | Yes |
| Attachments | Yes (MIME) | Yes (base64) |
| Open Tracking | Limited | Full |
| Click Tracking | Limited | Full |
| Sandbox Mode | No | Yes |
| Batch Processing | No | Yes (mail/send) |
| Custom Headers | Yes | Yes |
| Legacy Compatibility | Excellent | Good |
Common Mistakes
1. Using SMTP When You Need Dynamic Templates
SMTP cannot use SendGrid dynamic templates. If you need Handlebars-based template personalization, use the REST API.
2. Sending API Keys in Plaintext via SMTP Login
SMTP authentication sends the API key as a password. Ensure TLS is enabled and avoid logging the SMTP credentials.
3. Not Enabling Tracking Settings via SMTP
SMTP has limited tracking. If you need open/click tracking, switch to the REST API and configure tracking_settings.
4. Using SMTP for High-Volume Sending
SMTP connections are persistent and reuse is complex. The REST API handles connection pooling better for high-volume sending.
5. Mixing Methods Without Consistent Headers
If you use both SMTP and API, ensure consistent List-Unsubscribe headers and suppression handling across both methods.
Practice Questions
- What port does SendGrid SMTP use?
- What status code indicates a successful API send?
- Can SMTP use SendGrid dynamic templates?
- Which method supports categories?
- When would you choose SMTP over the API?
Answers
- 587 with STARTTLS. 2. 202 Accepted. 3. No, SMTP cannot use dynamic templates. 4. The REST API supports categories; SMTP does not. 5. When migrating existing email infrastructure that already uses standard SMTP libraries.
Challenge
Build an email router that checks a configuration flag and sends email via either SMTP or REST API based on the feature requirements. The router should use SMTP for simple notifications and API for personalized template emails.
FAQ
Mini Project
Build a dual-mode email sending service that can send via both SMTP and REST API, with a feature router that decides which method to use based on template requirements, tracking needs, and category support. Include logging and performance comparison between the two methods.
What's Next
- Learn about SendGrid dynamic templates with Handlebars
- Explore template versioning for managing template changes
- Continue to email personalization with to, cc, and bcc
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro