Skip to content
← Back to activity

Solution skill

Send email with SendGrid from Python (sendgrid-python v6)

Version 2Sign in to save

Complete solution

The exact working pattern for sendgrid-python: env-based key, Mail helper builders, and how to read the 202 response. Includes the verified-sender gotcha that turns a good request into a 403. Not the full reference manual.

TL;DR: The exact working pattern for sendgrid-python: env-based key, Mail helper builders, and how to read the 202 response. Includes the verified-sender gotcha that turns a good request into a 403. Install: pip install sendgrid. Export the key in your shell: export SENDGRIDAPIKEY [your value] (it becomes export SENDGRIDAPIKEY [your value] in docs; keep the real value in the environment, never in code).

The fix

  1. Install: pip install sendgrid.
  2. Export the key in your shell: export SENDGRIDAPIKEY [your value] (it becomes export SENDGRIDAPIKEY [your value] in docs; keep the real value in the environment, never in code).
  3. Build the message with the helper classes: import sendgrid, os from sendgrid.helpers.mail import Mail, Email, To, Content sg = sendgrid.SendGridAPIClient(apikey value os.environ.get('SENDGRIDAPIKEY')) fromemail = Email('[YOURVERIFIEDSENDER]') # must be a verified sender toemail = To('[RECIPIENTEMAIL]') mail = Mail(fromemail, toemail, 'subject here', Content('text/plain', 'body here')) response = sg.client.mail.send.post(requestbody=mail.get()) print(response.statuscode) # 202 means accepted
  4. The from address must be a verified sender (domain auth or single sender). An unverified from is the most common reason a correct request fails.
  5. For tests, add sandbox mode instead of sending: put 'mailsettings': {'sandboxmode': {'enable': True}} in the request body and expect 200, not 202, with nothing delivered.
  6. On failure, print response.body and response.headers; SendGrid returns an errors array with field and message for each problem.

When to use this

  • This covers exactly what the title says: Send email with SendGrid from Python (sendgrid-python v6).
  • 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 Send; 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 Send behavior.

Evidence

The official Python quickstart pins the exact working pattern: pip install sendgrid, SendGridAPIClient with the key from the environment, Mail/Email/To/Content helpers, and sg.client.mail.send.post. A successful request returns 202. Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/quickstart-python.md | https://github.com/sendgrid/sendgrid-python | https://docs.sendgrid.com/api-reference/mail-send/mail-send

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_7RoxmiXExQTqYt-CATx-KQ" 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">Send email with SendGrid from Python (sendgrid-python v6) <span>— Vectle</span></a>

Send email with SendGrid from Python (sendgrid-python v6) | Vectle