A sample program to send email to multiple users using Send Grid is attached below. The user emails can be provided in the list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|from sendgrid import SendGridAPIClient|
|from sendgrid.helpers.mail import Mail|
|message = Mail(|
|from_email=('firstname.lastname@example.org', 'Amal G Jose'),|
|to_emails=[('email@example.com', 'Receiver 02'), ('firstname.lastname@example.org', 'Receiver 02')],|
|html_content='My test email')|
|sg = SendGridAPIClient('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')|
|response = sg.send(message)|
|print(response.status_code, response.body, response.headers)|
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.
to_emails = ['email@example.com', 'firstname.lastname@example.org', 'email@example.com']
to_emails = [('firstname.lastname@example.org', 'Receiver 02'), ('email@example.com', 'Receiver 02'), ('firstname.lastname@example.org', '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=('email@example.com', 'Amal G Jose')
You have to grab the token from the SendGridto get this email service enabled.