• flask 邮件发送


    falsk实现邮件发送

    flask 发邮件:

    #coding:utf-8
    from flask import Flask,render_template,session,url_for,redirect,flash
    from flask.ext.mail import Mail, Message
    from flask.ext.moment import Moment
    from flask.ext.wtf import Form
    from flask.ext.bootstrap import Bootstrap
    from wtforms import StringField,SubmitField, TextAreaField
    from wtforms.validators import Required, Email
     
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')#设置系统默认编码为utf-8
     
    '''
    这个类描述了网页上的结构
    '''
    class MailForm(Form):
        receiver = StringField('收件人:',validators=[Required(),Email()])
        style = StringField('主题:',validators=[Required()])
        body = TextAreaField('正文:',validators=[Required()])
        submit = SubmitField('发送')
     
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'qiyeboy'
    #下面是SMTP服务器配置
    app.config['MAIL_SERVER'] = 'smtp.163.com' #电子邮件服务器的主机名或IP地址
    app.config['MAIL_PORT'] = '25' #电子邮件服务器的端口
    app.config['MAIL_USE_TLS'] = True #启用传输层安全
    app.config['MAIL_USERNAME'] ='xxxxxx@163.com' #os.environ.get('MAIL_USERNAME') #邮件账户用户名
    app.config['MAIL_PASSWORD'] = 'your password'#os.environ.get('MAIL_PASSWORD') #邮件账户的密码
     
    mail = Mail(app)
    bootstrap = Bootstrap(app)#进行网页渲染
    moment = Moment(app)#时间
     
    @app.route('/',methods=['GET','POST'])
    def index():
        '''
           flask中的路由
        :return:
        '''
        mailForm = MailForm()#表单
        if mailForm.validate_on_submit():#表单提交成功的判断
            try:
                receiverName = mailForm.receiver.data #收件人文本框的内容
                styledata = mailForm.style.data#主题文本框的内容
                bodydata  = mailForm.body.data#正文文本框的内容
                msg = Message(styledata,sender='xxxxxx@163.com',recipients=[receiverName])#发件人,收件人
                msg.body = bodydata
                mail.send(msg)
                flash('邮件发送成功!')#提示信息
            except:
                flash('邮件发送失败!')
     
        return render_template('index.html',form=mailForm,name ='xxxxxx@163.com' )#渲染网页
     
    if __name__ == '__main__':
        app.run(debug=True)
    

    base.html:

    temlates 文件下:
    {% extends "bootstrap/base.html" %}
     
    {% block title %}Flasky {% endblock %}
     
    {% block navbar %}
    <div class="navbar navbar-inverse" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="/">七夜</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a href="http://blog.csdn.net/qiye_/">CSDN博客</a></li>
                    <li><a href="http://www.cnblogs.com/qiyeboy/">博客园</a></li>
                    <li><a href="/">公众号:qiye_python</a></li>
                </ul>
            </div>
        </div>
    </div>
    {% endblock %}
     
    {% block content %}
     
    <div class="container">
    {% for message in get_flashed_messages() %}
    <div class="alert alert-warning">
    <button type="button" class="close" data-dismiss="alert">×</button>
    {{ message }}
    </div>
    {% endfor %}
     
    {% block page_content %}{% endblock %}
    </div>
     
    {% endblock %}
    

    index.html:

    index.html:
    {% extends "base.html" %}
    {% import "bootstrap/wtf.html" as wtf %}
     
     
    {% block title %}首页 {% endblock %}
     
    {% block page_content %}
     
    <div class="page-header">
    {% if name %}
    <h1> Hello,{{ name }}! </h1>
    {% else %}
    <h1>Hello,Stranger!</h1>
    {% endif %}
    </div>
     
    {{ wtf.quick_form(form) }}
     
    {% endblock %}
    
    --> python发送邮件经过的是smtp.163.com(网易的电子邮件服务器)
    

    Smtp 服务配置:

    SMTP服务器配置
    
    app.config['MAIL_SERVER'] = 'smtp.163.com' #电子邮件服务器的主机名或IP地址
    app.config['MAIL_PORT'] = '25' #电子邮件服务器的端口
    app.config['MAIL_USE_TLS'] = True #启用传输层安全
    app.config['MAIL_USERNAME'] ='xxxxxx@163.com' #os.environ.get('MAIL_USERNAME') #邮件账户用户名
    app.config['MAIL_PASSWORD'] = 'xxxxxx'#os.environ.get('MAIL_PASSWORD') #邮件账户的密码
    

    发邮件:

    msg = Message(styledata,sender='xxxxxx@163.com',recipients=[receiverName])#发件人,收件人
    msg.body = bodydata
    mail.send(msg)
    
  • 相关阅读:
    hdu2574 Hdu Girls' Day (分解质因数)
    python------logging模块
    python之异常
    python之反射
    python面向对象之封装
    python之面向对象2
    pyhton之路---面向对象
    python之路模块与包
    python常用模块
    匿名函数
  • 原文地址:https://www.cnblogs.com/shaozheng/p/12791859.html
Copyright © 2020-2023  润新知