pythonemailtemplatessendgridbcc

How to send email in python with sendgrid and dynamic template, with BCC addresses?


In python 3 and sendgrid I need to send BCC type emails and use a dynamic template, which I built here

In the dynamic template I put a blank space to receive data that I will send. I created the variable {{lista}} in the blank space. It looked like this in the HTML of the whitespace template:

<div style="font-family: inherit; text-align: inherit">{{sentences}}</div>

My python code looked like this:

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Attachment, Mail 
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException, Personalization, Bcc
import os

API_KEY = "real id"

lista = ["Sentence 1 <br>", "Sentence 2 <br>"]
lista = {"sentences": lista}

recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']

to_emails = [
        To(email= 'one_valid_email@gmail.com',
           dynamic_template_data = lista)] 

personalization = Personalization()
personalization.add_to(To('one_valid_email@gmail.com'))
    
for bcc_addr in recips:
        personalization.add_bcc(Bcc(bcc_addr))
   
message = Mail(
        from_email=('emailsender@gmail.com'),
        subject="email subject", 
        to_emails=to_emails,
        is_multiple=True)

message.add_personalization(personalization)

message.template_id = 'real id'

try:
        sendgrid_client = SendGridAPIClient(api_sendgrid)
        response = sendgrid_client.send(message)
        print(response.status_code)
        #print(response.body)
        #print(response.headers)
except Exception as e:
        print(e.message)


return

The email is sent, but with empty templates, without "list" data

Please, does anyone know what could be wrong?

Here is the image of my template, the place I created the blank to receive the data:

enter image description here

And here the HTML code of the place, Edit Module HTML:

enter image description here


Solution

  • That is because you are trying to call the method add_personalization on the to_emails object, which is a list that you defined a few lines above:

    to_emails = [To(email= 'one_valid_email@gmail.com'] 
    

    You need to call add_personalization to the message object that you created, like this:

    message.add_personalization(personalization)
    

    Here's the full code with the fix:

    from sendgrid import SendGridAPIClient
    from sendgrid.helpers.mail import Attachment, Mail 
    from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException, Personalization, Bcc
    import os
    
    API_KEY = "real id"
    
    lista = { "sentences": ["Sentence 1 <br>", "Sentence 2 <br>"] }
    
    recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']
    
    to_emails = [To(email= 'one_valid_email@gmail.com'] 
    
    personalization = Personalization()
    personalization.add_to(To('one_valid_email@gmail.com'))
        
    for bcc_addr in recips:
            personalization.add_bcc(Bcc(bcc_addr))
    
    message = Mail(
            from_email=('emailsender@gmail.com'),
            subject="email subject", 
            to_emails=to_emails,
            is_multiple=True)
    
    message.add_personalization(personalization)
    
    message.dynamic_template_data = lista
    
    message.template_id = 'real id'
    
    try:
            sendgrid_client = SendGridAPIClient(api_sendgrid)
            response = sendgrid_client.send(message)
            print(response.status_code)
            #print(response.body)
            #print(response.headers)
    except Exception as e:
            print(e.message)
    
    return
    

    Since your "sentences" dynamic template data is an array, you should likely loop through it too, so you can print each sentence. Try this in your template:

    {{#each sentences}}
      <div style="font-family: inherit; text-align: inherit">{{this}}</div>
    {{/each}}