简体   繁体   English

Python Flask电子邮件表单示例

[英]Python Flask e-mail form example

I wanted to make a contact form on my website. 我想在我的网站上填写联系表格。 I can find PHP e-mail forms everywhere, but simply there are no Flask examples. 我到处都可以找到PHP电子邮件表单,但是根本没有Flask示例。 I have no idea how to do it myself, so I'm asking if there is anyone who could tell me from scratch - how to make an e-mail contact form on website using Flask? 我自己也不知道该怎么做,所以我问是否有人可以从头开始告诉我-如何使用Flask在网站上创建电子邮件联系表?

Assuming: 假设:

  1. You have an html form sending the relevant information to /process_email 您有一个HTML表单,将相关信息发送到/process_email
  2. You have a Gmail account set up to send emails from (called testaccount in the below) 您已经设置了一个Gmail帐户来发送电子邮件(以下称为testaccount)

You would be looking for something like: 您将寻找类似的东西:

from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)

app.config.update(dict(
    MAIL_SERVER = 'smtp.googlemail.com',
    MAIL_PORT = 465,
    MAIL_USE_TLS = False,
    MAIL_USE_SSL = True,
    MAIL_USERNAME = 'testaccount',
    MAIL_PASSWORD = '[password]'
))

mail = Mail(app)

@app.route('/process_email', methods=['POST'])
def process_email():
    msg = Message('Test', sender='testaccount@gmail.com', recipients=['your@email.com'])
    msg.body = 'This is a test email' #Customize based on user input
    mail.send(msg)

    return 'done'

See https://code.tutsplus.com/tutorials/intro-to-flask-adding-a-contact-page--net-28982 and https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support for examples and more information 参见https://code.tutsplus.com/tutorials/intro-to-flask-adding-a-contact-page--net-28982https://blog.miguelgrinberg.com/post/the-flask-mega- tutorial-part-xi-email-support的示例和更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM