0

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

Reinaldo Chaves
  • 965
  • 4
  • 16
  • 43

1 Answers1

1

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}}
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thank you very much @philnash I made the above change but got a new error(AttributeError) in the message= block – Reinaldo Chaves Mar 10 '22 at 18:27
  • 1
    Ah, the dynamic template data should be a dict, not a list. I've edited my answer to change your `lista` variable to a dict. – philnash Mar 10 '22 at 22:32
  • Thanks. The email has been sent. But an email was sent with only the empty template, as I created it on the site. Without the data I sent in the Python script. It seems like something too complex to solve, without having to show all my code. Could I send you my code and/or share the dynamic template I created? – Reinaldo Chaves Mar 10 '22 at 23:34
  • 1
    Can you just add the code from the template where you are trying to use the dynamic data? – philnash Mar 10 '22 at 23:52
  • I put it at the beginning of my question: "
    {{lista}}
    " Or is it another part of the template code? It's the first time I use
    – Reinaldo Chaves Mar 11 '22 at 00:12
  • Does it help if I share my Template ID? – Reinaldo Chaves Mar 11 '22 at 00:14
  • 1
    The dynamic content is only called `lista` in the context of the python code sending the email. If you update to using a dict, like in my updated answer, then you can access the dynamic data using the dict's keys. In the case above the key is called "sentences", so your template should look like: `
    {{sentences}}
    `
    – philnash Mar 11 '22 at 00:14
  • Thanks. But the email remains with an empty template, without the "list" data. I edited the code and put some images from Sendgrid to help – Reinaldo Chaves Mar 11 '22 at 00:31
  • 1
    Ok, I think there's two issues here. You were trying to add the dynamic data to one of the `To` addresses, but you should add it to your message instead (`message.dynamic_template_data = lista`). I have updated the code in my answer with this. Secondly, since `sentences` is an array, I think you should likely loop through the array. I've added an example of this at the bottom of my answer. In the SendGrid template editor, you can test your template with example data, which is easier than sending an email each time, I recommend you try that too. – philnash Mar 11 '22 at 00:38
  • Thanks, almost everything worked. The white space started to receive the data it sent, but I had to transform it into Code Module HTML. The only thing that hasn't changed is the subject of the email. It remains fixed as "Together with the next generation". But in the code above I put in message, subject "email subject". Do you know why the subject of the email does not change? – Reinaldo Chaves Mar 11 '22 at 01:36
  • 1
    The dynamic templates take over the subject line, but [you can set the subject in the dynamic data and then use handlebars in the subject line in the template](https://support.sendgrid.com/hc/en-us/articles/1260802535110-Create-a-Dynamic-Email-Subject-Line-with-Mustache-Templates). – philnash Mar 11 '22 at 01:39
  • Thanks. But on the templates site where should I create the "{{{subject}}}"? – Reinaldo Chaves Mar 11 '22 at 01:43
  • Got it here. Thank you very much – Reinaldo Chaves Mar 11 '22 at 02:05
  • Glad we got everything sorted in the end! – philnash Mar 11 '22 at 03:17