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

Direct access to sendgrid Mail object #45

Open
davegaeddert opened this issue Dec 13, 2016 · 9 comments
Open

Direct access to sendgrid Mail object #45

davegaeddert opened this issue Dec 13, 2016 · 9 comments
Assignees

Comments

@davegaeddert
Copy link

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
Copy link
Collaborator

andriisoldatenko commented Dec 13, 2016

@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 <[email protected]>",
  to=["[email protected]"],
  headers={"Reply-To": "[email protected]"}
)
# tracking_settings
mail.tracking_settings = 'tracking_settings' # which should put to python sendgrid

@elbuo8 How do you think?
@thinkingserious

@elbuo8
Copy link
Owner

elbuo8 commented Dec 13, 2016

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
Copy link
Author

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
Copy link
Owner

elbuo8 commented Dec 13, 2016

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
Copy link
Author

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 <[email protected]>",
  to=["[email protected]"],
  headers={"Reply-To": "[email protected]"}
)
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
Copy link
Collaborator

andriisoldatenko commented Dec 14, 2016

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
Copy link
Author

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

@estianross
Copy link

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

@davegaeddert
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants