Consider having three email IDs [email protected]
, [email protected]
and [email protected]
and I have three files unique for each user user1.xlsx
, user2.xlsx
, and user3.xlsx
. I have to send each user an email with their file as an attachment.
Currently, I'm sending the emails in a loop which I think is not the best solution. Here is the simplified version of the email-sending snippet.
for user, attachment_file in zip(users, attachment_files):
message = Mail(from_email=from_email, to_emails=user)
attachment = Attachment(attachment_file)
message.attachment = [attachment]
send_grid_client.send(message)
Is there a way to send these emails with attachments as a single sendgrid API call?
To send multiple emails using a single API call, you can use personalizations on the Mail Send API. Unfortunately, the personalization objects do not support attachments, so the attachment have to be shared across all personalizations.
This means you'll have to call the Mail Send API for each email in your use case, like you are already doing. Additionally, you could send multiple API requests in parallel.