I am learning to use the sendgrid api with django python. For now, I can successfully send emails but I would like to make the email more appealing and apply some css styles. I looked online for some help but couldn't find any. Can someone please suggest me how i should go about it ?
This is what i tried so far
def sendemail(sender,recipient,subject,content,url):
sg = sendgrid.SendGridAPIClient(apikey="*****")
from_email = Email(sender)
to_email = Email(recipient)
subject = subject
content = Content("text", content+url)
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
Related
I am sending a dynamic email template through SendGrid using python. The correct HTML email template is displayed in inbox when I test the email through Sendgrid UI, but a plaintext version is displayed when the email is sent via python . Here's the code:
import sendgrid
import os
from sendgrid.helpers.mail import *
from sendgrid import SendGridAPIClient
sg = sendgrid.SendGridAPIClient('MY_KEY_HERE')
from_email = Email("example#email.com")
to_email = To("example#email.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
mail.template_id = "d-9e2812d070bb458e8d6cbd6c9b8922f6"
mail.dynamic_template_data = {
"first_name": "John",
"last_name": "Blacutt",
}
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
Here's the plaintext email:
If you are using a stored template and you want your dynamic content to render as HTML, you need to add an extra set of handlebars in the template itself else it will render as plain text.
EX:
<p>
{{{MailBody}}}
</p>
Notice the extra handlebars around {{MailBody}}
I am sending email from Azure functions using python and sendgrid. I am able to form the other part of the email but when I am trying to insert logo in signature of the email is not displaying that. Rest HTML is rendering fine.(till href)
Following is my code :
import sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
message = Mail(
from_email=email_from,
to_emails=email_to,
subject=email_subject,
html_content= '<br>Hi '+name+ ','+
'<br>body of the email. Working fine.' +
'<br><a href='+no+'>No'</a>\n' +
'<br><br>LOGO--><img src="doc.png" alt="W3Schools.com"/>'+
'</html>'
)
response = sg.send(message)
print(response.status_code)
I tried pointing out URL in src too and it did not help.
There are 3 options to achieve this:
CID Embedded Images (Inline Images)
Inline Embedding (Base64 Encoding)
Linked Images
Check out below links for implementation details:
https://sendgrid.com/blog/embedding-images-emails-facts/
https://github.com/sendgrid/sendgrid-python/issues/736
email = 'user#example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)
So I'm using this code from https://firebase.google.com/docs/auth/admin/email-action-links#python in django
and I'm getting this error:
NameError: name 'send_custom_email' is not defined
And nothing is mentioned in the documentation. So if anyone knows the solutions it would be great.
sg = SendGridAPIClient(os.environ["SENDGRID_API_KEY"])
html_content = f"""
<p>Hello,</p>
<p>Follow this link to reset password</p>
<p>{link}</p>
<p>If you didn't ask to reset the password, you can ignore this
email.</p>
"""
message = Mail(
to_emails=xyz#gmail.com,
from_email=Email('abc#gmail.com', "ABC System")
subject="Reset your password for ABC System",
html_content=html_content
)
try:
response = sg.send(message)
print(response.status_code)
except HTTPError as e:
print(e.message)
The above script is used for sending an email using Sendgrid.
I am trying to send email using sendgrid using python django. For some reason, the email is being delivered but without the html content in it. What could I be doing wrong ?
def sendemail(sender,recipient,subject,content,url):
sg = sendgrid.SendGridAPIClient(apikey="")
from_email = Email(sender)
to_email = Email(recipient)
subject = subject
objects = {
"message":content,
"domain":url
}
html_content = get_template('sendemail.html').render(objects)
print ('htmlcontent',html_content)
content = Content("text/html",html_content)
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
I think that you have to set text/plain content as well.
You can try something like this :
mail = Mail(from_email, subject, to_email)
mail.add_content(Content("text/plain", "plain message"))
mail.add_content(Content("text/html", html_content ))
I'm sending out an email to users with a generated link, and I wanna write a test that verifies whether the link is correct, but I can't find a way to get the content of the email inside the test.
Is there a way to do that?
If it helps at all, this is how I'm sending the email:
content = template.render(Context({'my_link': my_link}))
subject = _('Email with link')
msg = EmailMultiAlternatives(subject=subject,
from_email='MyWebsite Team <mywebsite#mywebsite.com>',
to=[user.email, ])
msg.attach_alternative(content, 'text/html')
msg.send()
The docs have an entire section on testing emails.
self.assertEqual(mail.outbox[0].subject, 'Email with link')