如何将Senggrid链接到Flask中的联系人表单以转发电子邮件 [英] How to Link Sengrid to a Contact Form in Flask to Forward Email

查看:106
本文介绍了如何将Senggrid链接到Flask中的联系人表单以转发电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Web应用程序,并尝试使用SendGrid处理电子邮件传递服务。

我正在用Flask语言编写应用程序。

现在我有一个联系人表单,我的问题是,只有当我使用SendGrid从预先批准的电子邮件地址发送电子邮件时,才会发送电子邮件。显然,这并不好,因为其他所有填写电子邮件表单的人都不会让它通过。

以下是我的代码:

路由

app.config['MAIL_SERVER'] = 'smtp.sendgrid.net'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'apikey'
app.config['MAIL_PASSWORD'] = os.environ.get('SENDGRID_API_KEY')
app.config['MAIL_DEFAULT_SENDER'] = os.environ.get('MAIL_DEFAULT_SENDER')


@app.route('/contact', methods=['GET', 'POST'])
def contactpage():
    if request.method == 'POST':
        print("Message sent")
        print(request.form.to_dict())
        m = message(request.form.to_dict())
        m.send()
    title = "Jonathan Bechtel contact form"
    description = "Contact Jonathan Bechtel with questions about teaching or working with him"
    return render_template("contact.html",
                            title=title,
                            description=description)

以下是我实际发送电子邮件的代码:

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
class message():

    def __init__(self, message_data):
        for key in message_data:
            print(key, message_data[key])
            setattr(self, key, message_data[key])

    def send(self):
        message      = Mail(
        from_email   = self.email,
        to_emails    = 'jonathanbechtel@gmail.com',
        subject      = 'Sample Email Message',
        html_content = f'<strong>From: {self.email}</strong><br><strong>Reason: {self.reason}</strong><br><strong>Message:</strong>{self.message}')
        try:
            sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
            response = sg.send(message)
        except Exception as e:
            print(e)

如果我将联系人表单中的from地址设置为我自己的地址,电子邮件就可以正常工作。但是,如果我使用任何其他代码,消息不会通过并生成403状态代码。

我认为这意味着我没有使用正确的API部分,但不确定从哪里开始。

谢谢。

推荐答案

Twilio SendGrid Developer布道者点击此处。

SendGrid不允许您从任何电子邮件地址发送电子邮件。我可以看到,在这种情况下,您只是试图创建一个只向您的电子邮件地址发送电子邮件的联系人表单,因此能够从任何电子邮件地址发送可能会很有用。但考虑一下允许用户设置收件人和发件人地址的表单,您就可以看到这可能会被滥用。

您可以read more about sender identity and SendGrid here

同时,以下是我对您的用例的建议。

将发件人电子邮件设置为您预先批准的电子邮件地址,并将用户的电子邮件地址包括在电子邮件正文中,就像您已经做的那样。然后将用户的电子邮件添加为回复电子邮件,这样您就可以回复电子邮件,它将被直接发送给用户。

我相信您可以用mail对象的reply_to方法设置回复:

    def send(self):
        message      = Mail(
        from_email   = APPROVED_SENDGRID_EMAIL,
        to_emails    = 'jonathanbechtel@gmail.com',
        subject      = 'Sample Email Message',
        html_content = f'<strong>From: {self.email}</strong><br><strong>Reason: {self.reason}</strong><br><strong>Message:</strong>{self.message}')

        # Set the reply-to email
        message.reply_to(self.email)

        try:
            sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
            response = sg.send(message)
        except Exception as e:
            print(e)

有关详细信息,请参阅the examples in the helper library

这篇关于如何将Senggrid链接到Flask中的联系人表单以转发电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆