Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to send an html/text email using sendgrid & python?

I'm trying to use sendgrid to send email from my Python application.

It's sending fine using the mail helper and the following syntax:

sgmessage = mail.Mail(from_email, message.subject, to_email, content)
sg.client.mail.send.post(request_body=sgmessage.get())

But I can't find the way to send an email which has a multipart/alternative content type with text & html versions?

I've tried things like this:

sgmessage = mail.Mail(from_email, message.subject, to_email)
sgmessage.add_content(content)
sgmessage.add_content(htmlcontent)

But that gives me Bad Request when I try to send.

Could someone point me to the documentation or other tip for achieving this?

Also attachments -- I can't find docs for that handy mail helper which would help me add attachments ability to my code.

like image 595
Jeremy Jones Avatar asked Jan 04 '23 05:01

Jeremy Jones


1 Answers

Mail Helper Source https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py

mail_html = Content(type_='text/html', value='<h1>Test Mail</h1><p>This is a test email message.</p>')
mail_txt = Content(type_='text/plain', value='This is a test email message.')
mail = Mail(mail_from, mail_subject, mail_to, mail_html)
mail.add_content(mail_txt)
response = sg.client.mail.send.post(request_body=mail.get())

The sample links are still 404's.

like image 144
Sachin Avatar answered Jan 16 '23 22:01

Sachin