Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting Personalization Removes Existing template_data #1037

Open
jr-bespoke opened this issue Jan 24, 2022 · 4 comments
Open

Setting Personalization Removes Existing template_data #1037

jr-bespoke opened this issue Jan 24, 2022 · 4 comments
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@jr-bespoke
Copy link

Issue Summary

I am trying to determine why using add_personalization removes the existing dynamic_template_data I already set on my mail object. If this is desired behavior, I would have expected some kind of note about it in the docs, or preferably a warning in the code.

I tried looking in the knowledge base, on github issues, and on stack overflow. I could not find anything to suggest why this might be happening. I found the workaround by simply resetting dynamic_template_data. I could, of course, simply add the personalization first and then add the dynamic_template_data, but this seemed like a silent gotcha that should be brought to the sendgrid team's awareness.

Steps to Reproduce

  1. Attach dynamic template data to mail object
  2. Have cc recipients, so add personalization to mail object
  3. Dynamic template data is now empty

Code Snippet

def our_simplified_send_func(to_, template_id, template_data, cc_recipients = []):
    mail = Mail(from_email=self._from, to_emails=_to) # self._from is a class var
    mail.dynamic_template_data = template_data
    mail.template_id = template_id

    if len(cc_recipients) > 0:
        sendgrid_personalization = Personalization()

        for cc in cc_recipients:
            sendgrid_personalization.add_cc( To(cc, cc) )

        for to_recipient in to_:
            sendgrid_personalization.add_to( To(to_recipient, to_recipient) )
            mail.add_personalization(sendgrid_personalization)

        # Why do I need to re-add dynamic_template_data after adding a personalization?
        mail.dynamic_template_data = template_data

Exception/Log

It's not an explicit exception or error log so much as running `add_personalization` removes the existing dynamic_template_data.

Technical details:

  • sendgrid-python version: 6.4.8
  • python version: 3.8.12
@beebzz
Copy link

beebzz commented Feb 1, 2022

Hi @jr-bespoke! I tried reproducing the issue using your code snippet, and saw that the dynamic template data still exists. When you create your new personalization (with two recipients & ccs) and add it to the object on line 14, the original top-level data (with the single recipient, template data & id) becomes another personalization (or separate email) under the hood.

Read here to gain a clearer idea of how personalizations work & how you can meet your desired use case. Let me know if you have any other questions!

@beebzz beebzz added type: question question directed at the library status: waiting for feedback waiting for feedback from the submitter labels Feb 2, 2022
@phithor
Copy link

phithor commented Feb 16, 2022

I had the same issue that when adding personalization the dynamic template did not have any of the data. Had to do the same as ts.

@childish-sambino
Copy link
Contributor

childish-sambino commented Mar 11, 2022

I think there's a disconnect between the code snippet provided and what the intended goal is. The issue is that dynamic template data is a part of a personalization which also includes recipients. This allows for using the same dynamic template and generating multiple personalized emails to different recipients with different dynamic template data in a single API call. This also means that a personalization generally should not be added multiple times to a single Mail instance.

Recommend building the personalization(s) to match the goal and then adding it/them to the Mail instance. Or just adding the CCs to the mail object. E.g.,

def our_simplified_send_func(to_, template_id, template_data, cc_recipients = []):
    mail = Mail(from_email=self._from, to_emails=_to) # self._from is a class var
    mail.dynamic_template_data = template_data
    mail.template_id = template_id

    for cc in cc_recipients:
        mail.add_cc(Cc(cc, cc))

@rakatyal
Copy link
Contributor

Closing due to lack of activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

5 participants