python/flask 联系表未向 zoho 帐户发送电子邮件

问题描述

我非常需要你的帮助,我已经尝试解决这个问题一个星期了,但没有成功。

这是我的烧瓶/python 应用程序[![在此处输入图像描述][1]][1]

@app.route('/',methods=["GET","POST"])
@app.route('/home',"POST"])
def home():
    return render_template("landing.html")

def contact():
    if request.method == "POST":
        data = request.form
        data = request.form
        send_email(data["name"],data["email"],data["subject"],data["message"])
        return render_template("landing.html",msg_sent=True)
    return render_template("landing.html",msg_sent=False)

def send_email(name,email,subject,message):
    email_message = f"Subject: {subject}\n\nName: {name}\nEmail: {email}\nMessage:{message}"
    with smtplib.SMTP("smtp.zoho.eu") as connection:
        connection.starttls()
        connection.login(OWN_EMAIL,OWN_PASSWORD)
        connection.sendmail(OWN_EMAIL,OWN_EMAIL,email_message)

这是联系表单的landing.html部分:

<div class="col-lg-6">
            <form name="sentMessage" id="contactForm" action="{{ url_for('home') }}" method="post" novalidate>
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label>Name</label>
                  <input type="text" name="name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                  <p class="help-block text-danger"></p>
                </div>
              </div>
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label>Email Address</label>
                  <input type="email" name="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                  <p class="help-block text-danger"></p>
                </div>
              </div>
              <div class="control-group">
                <div class="form-group col-xs-12 floating-label-form-group controls">
                  <label>Subject</label>
                  <input type="text" name="subject" class="form-control" placeholder="Subject" id="subject" required data-validation-required-message="Please enter the subject.">
                  <p class="help-block text-danger"></p>
                </div>
              </div>
              <div class="control-group">
                <div class="form-group floating-label-form-group controls">
                  <label>Message</label>
                  <textarea rows="5" name="message" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                  <p class="help-block text-danger"></p>
                </div>
              </div>
              <br>
              <div id="success"></div>
              <button type="submit" class="btn btn-primary" id="sendMessageButton">Send</button>
            </form>

我有 2 个不同的错误,具体取决于我是从移动设备还是从 Mac 发送,如果我从移动设备发送,应用程序日志显示 status=200,如果从 Mac 发送,则显示 status=404

2021-03-02T22:20:44.499516+00:00 heroku[router]: at=info method=POST path="/home" host=www.redwhisker.com request_id=ff810eb4-138f-4c80-a605-785b130c1ddf fwd="86.141.236.165" dyno=web.1 connect=0ms service=2ms status=200 bytes=10907 protocol=http
2021-03-02T22:22:08.721016+00:00 heroku[router]: at=info method=POST path="/home" host=www.redwhisker.com request_id=cbe2cfc0-d197-4eef-9e97-7d021ac34b32 fwd="86.141.236.165" dyno=web.1 connect=1ms service=3ms status=200 bytes=10907 protocol=http
2021-03-02T22:22:08.719985+00:00 app[web.1]: 10.8.36.208 - - [02/Mar/2021:22:22:08 +0000] "POST /home HTTP/1.1" 200 10744 "http://www.redwhisker.com/home" "Mozilla/5.0 (Linux; Android 10; SM-G965F) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36"
2021-03-02T22:28:47.733308+00:00 heroku[router]: at=info method=POST path="/home" host=www.redwhisker.com request_id=a2bead33-e8d5-4eed-879f-90ce41e52e7d fwd="86.141.236.165" dyno=web.1 connect=0ms service=2ms status=200 bytes=10907 protocol=http
2021-03-02T22:28:47.730454+00:00 app[web.1]: 10.39.168.78 - - [02/Mar/2021:22:28:47 +0000] "POST /home HTTP/1.1" 200 10744 "http://www.redwhisker.com/home" "Mozilla/5.0 (iPhone; cpu iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML,like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
2021-03-02T22:28:48.050319+00:00 heroku[router]: at=info method=GET path="/static/lib/bootstrap/css/bootstrap.min.css.map" host=www.redwhisker.com request_id=8330d006-3ae4-46ed-a803-d2e93f6b92d6 fwd="86.141.236.165" dyno=web.1 connect=2ms service=4ms status=404 bytes=400 protocol=http
2021-03-02T22:28:48.047736+00:00 app[web.1]: 10.39.168.78 - - [02/Mar/2021:22:28:48 +0000] "GET /static/lib/bootstrap/css/bootstrap.min.css.map HTTP/1.1" 404 232 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.96 Safari/537.36"
2021-03-02T22:28:48.235576+00:00 app[web.1]: 10.39.168.78 - - [02/Mar/2021:22:28:48 +0000] "GET /static/lib/bootstrap/js/bootstrap.bundle.min.js.map HTTP/1.1" 404 232 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.96 Safari/537.36"
2021-03-02T22:28:48.238169+00:00 heroku[router]: at=info method=GET path="/static/lib/bootstrap/js/bootstrap.bundle.min.js.map" host=www.redwhisker.com request_id=6b290b88-8cbf-4b64-8185-24eae023afdb fwd="86.141.236.165" dyno=web.1 connect=5ms service=4ms status=404 bytes=400 protocol=http

我对 Flask/python 还很陌生,我被困在这个问题上,有人可以帮我吗?谢谢!

解决方法

由于您收到电子邮件状态代码,因此您的 Flask 代码非常接近工作状态。问题可能只是一两个配置更改。

以下是一些调试建议:

  1. 如果 Flask/Python 的电子邮件代码中有调试选项,请打开它,看看是否能获得有关失败的更详细信息。

  2. 在 Stackoverflow 中搜索 [zoho][smtp] 并通读问题。这是一个常见的问题。问题将出现在不同的编码语言中,但它们大多都通过 Zoho 的 smtp 服务器发送电子邮件。

  3. 尝试通过 python 发送一封电子邮件,中间没有烧瓶,只是为了降低复杂性,看看你是否可以从那里解决问题。此 Stackoverflow 链接讨论了一些 Python 邮件选项:Sending HTML email using Python