使用sendgrid&;发送html/文本电子邮件的简单方法;python

使用sendgrid&;发送html/文本电子邮件的简单方法;python,python,email,sendgrid,Python,Email,Sendgrid,我正在尝试使用sendgrid从我的Python应用程序发送电子邮件 使用mail helper和以下语法可以正常发送邮件: sgmessage = mail.Mail(from_email, message.subject, to_email, content) sg.client.mail.send.post(request_body=sgmessage.get()) 但是我找不到发送包含文本和html版本的多部分/可选内容类型的电子邮件的方法 我试过这样的方法: sgmessage =

我正在尝试使用sendgrid从我的Python应用程序发送电子邮件

使用mail helper和以下语法可以正常发送邮件:

sgmessage = mail.Mail(from_email, message.subject, to_email, content)
sg.client.mail.send.post(request_body=sgmessage.get())
但是我找不到发送包含文本和html版本的多部分/可选内容类型的电子邮件的方法

我试过这样的方法:

sgmessage = mail.Mail(from_email, message.subject, to_email)
sgmessage.add_content(content)
sgmessage.add_content(htmlcontent)
但当我尝试发送时,这给了我不好的请求

有人能告诉我实现这一点的文档或其他提示吗

还有附件--我找不到那个方便的邮件助手的文档,它可以帮助我将附件功能添加到代码中。

你看过了吗?仅显示一种内容类型,但示例显示设置两种内容。

邮件帮助程序源

mail\u html=Content(键入\'text/html',value='testmail这是一封测试电子邮件。

) mail_txt=Content(键入\'text/plain',value='这是一封测试电子邮件。')) 邮件=邮件(邮件发件人、邮件主题、邮件收件人、邮件html) mail.add_内容(mail_txt) response=sg.client.mail.send.post(request\u body=mail.get())
示例链接仍然是404。

导入操作系统
import os

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail


def send_email(emails, notification_text, subject):
    """Send Emails

    Send email to the mail ids

    :param emails: emails of the user
    :type emails: list
    :param notification_text: notification text
    :type notification_text: str
    :param request_link: link to the requet
    :type request_link: str
    :param subject: subject of the email
    :type subject: str

    :rtype: str
    """
    html_content = '<strong>' + notification_text + '</strong>'
    emails = emails
    message = Mail(
        from_email='yourSendGridVerifiedEmail',
        to_emails=emails,
        subject=subject,
        html_content=html_content)
    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        sg.send(message)
        return "Email Sent"
    except Exception as e:
        return e.message
从sendgrid导入SendGridApicClient 从sendgrid.helpers.mail导入邮件 def发送电子邮件(电子邮件、通知文本、主题): “发送电子邮件 向邮件ID发送电子邮件 :param emails:用户的电子邮件 :键入电子邮件:列表 :param notification_text:通知文本 :type notification_text:str :param request_link:链接到请求 :输入请求链接:str :param subject:电子邮件的主题 :类型主题:str :rtype:str """ html_content=''+通知_text+'' 电子邮件=电子邮件 信息=邮件( 从_email='yourSendGridVerifiedEmail', 发送电子邮件=电子邮件, 主语, html\u内容=html\u内容) 尝试: sg=SendGridAPIClient(os.environ.get('SENDGRID\u API\u KEY')) sg.发送(信息) 返回“已发送电子邮件” 例外情况除外,如e: 返回电子邮件
看看这个。在撰写本文时,您需要查看
type
方法中的第31行。这是我在未登录时获得的链接,希望能够保持良好状态:
import os

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail


def send_email(emails, notification_text, subject):
    """Send Emails

    Send email to the mail ids

    :param emails: emails of the user
    :type emails: list
    :param notification_text: notification text
    :type notification_text: str
    :param request_link: link to the requet
    :type request_link: str
    :param subject: subject of the email
    :type subject: str

    :rtype: str
    """
    html_content = '<strong>' + notification_text + '</strong>'
    emails = emails
    message = Mail(
        from_email='yourSendGridVerifiedEmail',
        to_emails=emails,
        subject=subject,
        html_content=html_content)
    try:
        sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
        sg.send(message)
        return "Email Sent"
    except Exception as e:
        return e.message