A sample program to send email to multiple users using Send Grid is attached below. The user emails can be provided in the list.


import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email=('amal@gmail.com', 'Amal G Jose'),
to_emails=[('receiver01@mail.com', 'Receiver 02'), ('receiver02@mail.com', 'Receiver 02')],
subject='Sample Email',
html_content='My test email')
sg = SendGridAPIClient('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
response = sg.send(message)
print(response.status_code, response.body, response.headers)

view raw

send_email.py

hosted with ❤ by GitHub

 

The to_email specifies the recipients. The from_email specifies the sender. You can provide the recipient details either as a list of emails addresses or a list of tuples containing email address and the label.

That means

to_emails = ['receiver01@mail.com', 'receiver02@mail.com', 'receiver03@mail.com']

or

to_emails = [('receiver01@mail.com', 'Receiver 02'), ('receiver02@mail.com', 'Receiver 02'), ('receiver03@mail.com', 'Receiver 03')]

 

Also in the from_email if you are simply passing the email address, the recepient will receive an email with the sender name as the name in the email address. If you want proper labels in the email, provide the details in a tuple.

from_email=('amal@gmail.com', 'Amal G Jose')

You have to grab the token from the SendGridto get this email service enabled.

Advertisement