sklarsa / django-sendgrid-v5

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

Unable to replace keys in html template with values. #64

Closed Shubham-S-Yadav closed 4 years ago

Shubham-S-Yadav commented 4 years ago

Hi, I'm using this library and I'm able to send the template email successfully to the recepient, but the problem is I'm unable to replace the keys in my html template generated in sendgrid with my values as shown in below code snippet. Please let me know what I'm doing wrong or what is the way to do it. Version Installed: django-sendgrid-v5==0.8.1

Thanks

reset_url = "Some link" mail = EmailMultiAlternatives( subject="Demo", body="This is a simple text email body.", from_email="", to=[email], ) #Trying to replace the key in template here mail.subscriptions = {"%reset_token%": reset_url} mail.attach_alternative( "

This is a simple HTML email body

", "text/html" )

Add template

            mail.template_id = 'template_id here'
            mail.send()
sklarsa commented 4 years ago

I think it's mail.substitutions, not mail.subscriptions

On Fri, Apr 10, 2020 at 3:02 PM Shubham Yadav notifications@github.com wrote:

Hi, I'm using this library and I'm able to send the template email successfully to the recepient, but the problem is I'm unable to replace the keys in my html template generated in sendgrid with my values as shown in below code snippet. Please let me know what I'm doing wrong or what is the way to do it. Version Installed: django-sendgrid-v5==0.8.1

Thanks

reset_url = "Some link" mail = EmailMultiAlternatives( subject="Demo", body="This is a simple text email body.", from_email="", to=[email], ) #Trying to replace the key in template here mail.subscriptions = {"%reset_token%": reset_url} mail.attach_alternative( "

This is a simple HTML email body ", "text/html" )

Add template

mail.template_id = 'template_id here' mail.send()

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sklarsa/django-sendgrid-v5/issues/64, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOXCRJLIXFA2IYXOTSKGFDRL5ULVANCNFSM4MFUR2EQ .

Shubham-S-Yadav commented 4 years ago

No tried it out in that case not able to send mail, it gives error

File "C:\Users\MB-User\AppData\Local\Programs\Python\Python36\lib\site-packages\python_http_client\client.py", line 178, in _make_request raise exc python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

sklarsa commented 4 years ago

This seems like an issue that the people over at https://github.com/sendgrid/sendgrid-python can help with (better than me) since that "BadRequestsError" is a response from their server. This middleware only provides a thin abstraction layer on top of that functionality. My guess is that you're missing an additional email setting...

On Fri, Apr 10, 2020 at 4:52 PM Shubham Yadav notifications@github.com wrote:

No tried it out in that case not able to send mail, it gives error

File "C:\Users\MB-User\AppData\Local\Programs\Python\Python36\lib\site-packages\python_http_client\client.py", line 178, in _make_request raise exc python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sklarsa/django-sendgrid-v5/issues/64#issuecomment-612211522, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOXCRKJ7CF7QRILF7NIKXLRL6BHPANCNFSM4MFUR2EQ .

Shubham-S-Yadav commented 4 years ago

Yes I've already checked over there and they have asked to connect with there support and closed the issue, I guessed will get help over here if possible.

Thanks 😊

backtrackbaba commented 4 years ago

Any update on this? @Shubham-S-Yadav

Shubham-S-Yadav commented 4 years ago

Any update on this? @Shubham-S-Yadav

I didn't use django-sendgrid-v5 library instead implemented using just sendgrid library as I didn't found my required functionality.

sklarsa commented 4 years ago

Can you post your solution? I can try to integrate it into this library for the benefit of other users

Shubham-S-Yadav commented 4 years ago

Can you post your solution? I can try to integrate it into this library for the benefit of other users

Ok, here is the demo script you can try.

`import sendgrid from sendgrid.helpers.mail import Email, Substitution, Mail, Personalization from python_http_client import exceptions

sg = sendgrid.SendGridAPIClient("YOUR_SENDGRID_API_KEY") mail = Mail() mail.template_id = "PUT_YOUR_TEMPLATE_ID_HERE"

mail.from_email = Email("SENDER_EMAIL") personalization = Personalization() personalization.add_to(Email("RECEIVER_EMAIL")) mail.subject = "YOUR MAIL SUBJECT"

// you can add dictionary with key value pair here which you need to replace personalization.dynamic_template_data = {"user_name": "Shubham"}

mail.add_personalization(personalization)

try: response = sg.client.mail.send.post(request_body=mail.get()) except exceptions.BadRequestsError as e: print(e.body) exit()`