sklarsa / django-sendgrid-v5

An implementation of Django's EmailBackend compatible with sendgrid-python v5+
MIT License
318 stars 54 forks source link

Handle "to" resolution between EmailMessage attr and personalizations #92

Closed sklarsa closed 3 years ago

sklarsa commented 3 years ago

Hi @sklarsa sorry I forgot to mention something important The .personalizations attribute obviously contains the "to" address for each personalization.

This may make it seem like setting the "to" attr on the EmailMessage is unnecessary. It is unnecessary in most cases, eg when using mail.get_connection().send_messages() ; however, the implementation of EmailMessage.send() does a check for self.recipients(), which means an arbitrary "to" must be set - even if it will not be used, if the given function is to be used

Originally posted by @alichass in https://github.com/sklarsa/django-sendgrid-v5/issues/90#issuecomment-892874816

sklarsa commented 3 years ago

@alichass Are you attaching Personalization objects to the EmailMessage when constructing it, or dicts? I'm wondering if we need to support both..

alichass commented 3 years ago

so currently the personalizations attribute needs to be set as such in order to be used (basically correctly formatted EmailMessage Attributes):

email = EmailMessage(from_email="hello@github.com", to=["goodbye@github.com"])
email.personalizations=[
    {"to": ["_to_email_addr_1"], "bcc":[...]  "dynamic_template_data": {...}, ...},
    {"to": ["_to_email_addr_2", "_to_email_addr_3", ...], "bcc":[...]  "dynamic_template_data": {...}, ...},
...
]

If you use EmailMessage.send() be sure to construct the EmailMessage with an arbitrary to - don't worry it wont be used

sklarsa commented 3 years ago

Closed by #93