首页 > 解决方案 > 如何使用 Python 使用 SendGrid 发送超链接

问题描述

我正在尝试使用必须包含一个超链接的 SendGrid 发送一封简单的邮件。

我没有做任何花哨的事情,只是按照文档示例进行了一些更改

import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("test@example.com")
subject = "Sending with SendGrid is Fun"

content = Content("text/html", '<html><a href="https://www.google.com">google</a></html>')

mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())

对我来说它看起来不错,但是一旦我运行脚本并发送了邮件,它就会像纯文本一样显示,我无法单击。

我还尝试了许多其他删除<html>标签的组合,使用带反斜杠的单引号和双引号,但没有任何效果。我什至尝试在没有Mail Helper Class的情况下做同样的事情,但它没有用。

非常感谢您的帮助。

标签: pythonemailhyperlinksendgrid

解决方案


content = Content(

    "text/html", "Hi User, \n This is a test email.\n This is to also check if hyperlinks work <a href='https://www.google./com'> Google </a> Regards Karthik")

这对我有帮助。我相信你不需要提及html标签


推荐阅读