Using Python 2.7. Taken your example code which works OK. I now want to tell the email to use a transnational template. I am using Mail.template_id="6a7d6c49-4b23-4ad7-b3af-aaee938d9ca8" but I just get a standard text email.
Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?
Hey @hanhaa
Sorry to hear you are having issues. Since you did not provide the code you are using I am going to guess as to what is happening. Since you are only getting a text/plain email I am guessing you are passing a text/plain section in your API call. Something like this:
from_email = Email("[email protected]")
subject = "I'm replacing the subject tag"
to_email = Email("[email protected]")
content = Content("text/plain", "Text")
mail = Mail(from_email, subject, to_email, content)
mail.template_id = "6a7d6c49-4b23-4ad7-b3af-aaee938d9ca8"
When using templates you want to do one of two options. The first is to not pass any content at all or, the second is to pass both html and plain text content. I hope this helps resolve your issue.
Hi,
Thanks for the reply. I have the following test code:
import sendgrid
import os
from sendgrid.helpers.mail import *
sg =
sendgrid.SendGridAPIClient(apikey='REDACTED')
from_email = Email("[email protected]")
to_email = Email("[email protected]")
subject = "Sending with SendGrid is Fun"
Python")
content=""
mail = Mail(from_email, subject, to_email,content)
Mail.template_id="6a7d6c49-4b23-4ad7-b3af-aaee938d9ca8"
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
When I run this I get:
Traceback (most recent call last):
File "/Users/ianmaciver/Dropbox/Parceline/HH Mobile/Portal
Code/testSendGrid.py", line 18, in
response = sg.client.mail.send.post(request_body=mail.get())
File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_http_client/client.py",
line 227, in http_request
return Response(self._make_request(opener, request))
File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_http_client/client.py",
line 161, in _make_request
raise exc
BadRequestsError
I have tried a number of combinations with no content or content="" but
still get the same error.
Just trying to get a basic piece of code working.
Thanks
Ian MacIver
CEO Hanhaa Mobile
Tel: +44 (0) 7778 266547
On 25 October 2017 at 22:51, Kyle Roberts notifications@github.com wrote:
Hey @hanhaa https://github.com/hanhaa
Sorry to hear you are having issues. Since you did not provide the code
you are using I am going to guess as to what is happening. Since you are
only getting a text/plain email I am guessing you are passing a text/plain
section in your API call. Something like this:from_email = Email("[email protected]")
subject = "I'm replacing the subject tag"
to_email = Email("[email protected]")
content = Content("text/plain", "Text")
mail = Mail(from_email, subject, to_email, content)
mail.template_id = "6a7d6c49-4b23-4ad7-b3af-aaee938d9ca8"When using templates you want to do one of two options. The first is to
not pass any content at all or, the second is to pass both html and plain
text content. I hope this helps resolve your issue.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-python/issues/440#issuecomment-339482654,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AfkJBos7kCIhMChKuGcdjKMaSKrBLBXcks5sv61FgaJpZM4QGQtr
.
--
Here is an example that should work for you:
import sendgrid
import os
from sendgrid.helpers.mail import Email, Substitution, Mail, Personalization
from python_http_client import exceptions
sg = sendgrid.SendGridAPIClient(apikey='APIKEY')
personalization = Personalization()
personalization.add_to(Email("[email protected]"))
mail = Mail()
mail.from_email = Email("[email protected]")
mail.subject = "I'm replacing the subject tag"
mail.add_personalization(personalization)
mail.template_id = "51e49da0-76af-4056-8c3e-ae369ce554a6"
personalization.add_substitution(Substitution("-name-", "Example User"))
personalization.add_substitution(Substitution("-city-", "Denver"))
try:
response = sg.client.mail.send.post(request_body=mail.get())
except exceptions.BadRequestsError as e:
print(e.body)
exit()
print(response.status_code)
print(response.body)
print(response.headers)
@hanhaa to add to this, I think that your empty string is causing content to default to text/plain.
In @kylearoberts' example, he is only passing the template ID with no content object.
You have another option, where you can pass BOTH text/plain and text/html to the Mail object along with the template ID.
We hope this helps!
Thank you @kylearoberts for the help and code sample!
Most helpful comment
Here is an example that should work for you: