Solution skill
SendGrid with Flask: Flask-Mail SMTP config
Complete solution
Configure Flask-Mail to send through SendGrid's SMTP relay: the five MAIL_* settings, the literal apikey username, and keeping the key in the environment. Not the full reference manual.
TL;DR: Configure Flask-Mail to send through SendGrid's SMTP relay: the five MAIL* settings, the literal apikey username, and keeping the key in the environment. MAILDEFAULT_SENDER must be a verified sender.
The fix
app.config.update( MAILSERVER='smtp.sendgrid.net', MAILPORT=587, MAILUSERNAME='apikey', # literal string, not your key MAILUSETLS=True, MAILDEFAULTSENDER='[YOURVERIFIED_SENDER]' # verified sender )
Set MAILPASSWORD from the SENDGRIDAPI_KEY environment variable (for example via os.environ when building config). Do not hardcode it.
Then:
from flaskmail import Mail, Message mail = Mail(app) msg = Message('subject here', recipients=['[RECIPIENTEMAIL]']) msg.body = 'plain body' mail.send(msg)
Checklist:
- MAIL_USERNAME is exactly 'apikey'.
- MAILUSETLS True with port 587. Port 465 wants MAILUSESSL instead.
- MAILDEFAULTSENDER must be a verified sender.
- In tests, suppress sending (MAILSUPPRESSSEND) or point at sandbox mode so CI stays quiet.
When to use this
- This covers exactly what the title says: SendGrid with Flask.
- You are setting this up for the first time, or auditing an existing setup.
- You want the key gotchas in one place before you start.
When not to use this
- You are doing a different workflow with SendGrid; these steps are specific to the title above.
- You need the full reference docs; this is the short path, not the manual.
Compatibility
- Not pinned to a specific version; follows current SendGrid behavior.
Evidence
Flask-Mail reads MAIL_* config and speaks SMTP; the SendGrid-specific facts are the official SMTP settings: host smtp.sendgrid.net, literal apikey username, port 587 with TLS. Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/integrating-with-the-smtp-api.md
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_7KO_oiA7fB74K_EjHjO2VQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">SendGrid with Flask: Flask-Mail SMTP config <span>— Vectle</span></a>