elbuo8 / sendgrid-django

Simple django backend to send email through SendGrid
MIT License
207 stars 78 forks source link

Direct access to sendgrid Mail object #45

Open davegaeddert opened 7 years ago

davegaeddert commented 7 years ago

I'm wanting to change some additional settings on the sendgrid Mail object before it's sent (specifically click tracking - https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py#L23).

I was wondering if there is or should be a more generic way to access the underlying object that comes out of _build_sg_email? Suggestions for how to accomplish that (happy to help do it)?

Thanks for your work on this!

andriisoldatenko commented 7 years ago

@davegaeddert Thanks for your message. If I understand correctly, you want to add feature, to use tracking_settings in mail object when you send email? I think it's possible. E.g.

mail = EmailMultiAlternatives(
  subject="Your Subject",
  body="This is a simple text email body.",
  from_email="Yamil Asusta <hello@yamilasusta.com>",
  to=["yamil@sendgrid.com"],
  headers={"Reply-To": "support@sendgrid.com"}
)
# tracking_settings
mail.tracking_settings = 'tracking_settings' # which should put to python sendgrid

@elbuo8 How do you think? @thinkingserious

elbuo8 commented 7 years ago

We would have to add a section to handle it on _build_sg_email. As it stands right now it won't copy it.

davegaeddert commented 7 years ago

Right, I guess what I'm wondering is if there's a more generic way to do it, so that you don't have to add lines like these for each property that just gets passed on to the sendgrid object:

The ones you have already seem handy, but it would be nice if there was a way to simply access the Mail object and set any advanced settings directly on it myself. Does that make sense? Then people could also mess with any of the other properties available here without you having to specifically add support for it: https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/helpers/mail/mail.py#L8

I'm not sure what the best way to do it would be though, since it looks like the email backend code doesn't get called until send?

elbuo8 commented 7 years ago

Only thing that comes to mind is patching the send_messages which is not generic.

We could make a whitelist and copy properties to the mail object too.

davegaeddert commented 7 years ago

Maybe a whitelist of properties that can all be copied is as good as anything. The other thing that came to mind for me, which might not be a good pattern, would be to pass a function that could get called. Haven't tried any of this but something like:

mail = EmailMultiAlternatives(
  subject="Your Subject",
  body="This is a simple text email body.",
  from_email="Yamil Asusta <hello@yamilasusta.com>",
  to=["yamil@sendgrid.com"],
  headers={"Reply-To": "support@sendgrid.com"}
)
def sendgrid_mail_func(mail):
  mail.tracking_settings = 'anything you need'
  # etc.

mail.modify_sendgrid_mail_func = sendgrid_mail_func

Then in your backend

if hasattr(email, 'modify_sendgrid_mail_func'):
  email.modify_sendgrid_mail_func(mail)
andriisoldatenko commented 7 years ago

We could make a whitelist and copy properties to the mail object too.

+1 I think this approach more safety and guarantee that everything will work.

davegaeddert commented 7 years ago

@andriisoldatenko Sounds fine to me. Is that something that you guys are open to tackling relatively soon?

estianross commented 5 years ago

Is there any update on this being tackled? Specifically the handling of mail.tracking_settings?

davegaeddert commented 2 years ago

Personally I don't need this anymore (trying to close some old issues...). Feel free to reopen if somebody actually cares about this still. Thanks!