(Original) Send email in python and send email in original python

Source: Internet
Author: User
Tags email account mail account

(Original) Send email in python and send email in original python

I have been learning the flask framework for this time. I see a mail plug-in the flask extension. So today I will show you how to send emails.

First, I registered a 163 mailbox and needed to enable the smtp function, because we sent an email through smtp.163.com (Netease's email server) through python ).

Register the 163 mailbox and enable the smtp function, as shown in:

 

You need to bind a mobile phone when enabling the Service.

The final implementation is as follows:

Use flask to build a web server, create a webpage, fill in the recipient, subject, and body, and click send. The sending result is displayed.

 

The structure of the entire project is as follows:

Templates stores two html files. pyMail implements all functions. Next, I will list the source code and explain the core code sent.

 

Base.html:{% Extends "bootstrap/base.html" % }{% block title %} Flasky {% endblock % }{% block navbar %} <div class = "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 = "/ "> seven nights </a> </div> <div class =" navbar-collapse "> <ul class =" nav navbar-nav "> <li> <a href = "http://blog.csdn.net/qiye_/"> CSDN blog </a> </li> <a href = "http://www.cnblogs.com/qiyeboy/"> blog </a> </li> 

  

Index.html:

{% Extends "base.html" % }{% import "bootstrap/wtf.html" as wtf % }{% block title %} homepage {% endblock % }{% block page_content %} <div class = "page-header" >{% if name %}
PyMail. py: # coding: utf-8from flask import Flask, render_template, session, url_for, redirect, flashfrom flask. ext. mail import Mail, Messagefrom flask. ext. moment import Momentfrom flask. ext. wtf import Formfrom flask. ext. bootstrap import Bootstrapfrom wtforms import StringField, SubmitField, TextAreaFieldfrom wtforms. validators import Required, Emailimport sysreload (sys) sys. setdefaultencoding ('utf8') # set the system default value The UTF-8 encoding ''' class describes the structure of the webpage ''' class MailForm (Form): receiver ER = StringField ('recipient: ', validators = [Required (), email ()]) style = StringField ('topic: ', validators = [Required ()]) body = TextAreaField ('body:', validators = [Required ()]) submit = SubmitField ('send') app = Flask (_ name _) app. config ['secret _ key'] = 'qiyeboys' # below is the SMTP server configuration app. config ['mail _ Server'] = 'smtp .163.com '# The email SERVER's host name or IP address app. config ['mail _ port'] = '25' # Port app of the email server. config ['mail _ USE_TLS '] = True # enable the Transport Layer Security app. config ['mail _ username'] = 'xxxxxx @ 163.com '# OS. environ. get ('mail _ username') # email account USERNAME app. config ['mail _ password'] = 'your password' # OS. environ. get ('mail _ password') # MAIL account PASSWORD mail = Mail (app) bootstrap = Bootstrap (app) # rendering webpage moment = Moment (app) # Time @ app. route ('/', methods = ['get', 'post']) def index (): ''' route in flask: return: ''' mailForm = MailForm () # Table Single if mailForm. validate_on_submit (): # judgment on successful form submission try: receiverName = mailForm. aggreger. data # Content of the recipient text box styledata = mailForm. style. data # content of the topic text box bodydata = mailForm. body. data # body text box content msg = Message (styledata, sender = 'xxxxxx @ 163.com ', recipients = [receiverName]) # sender, recipient msg. body = bodydata mail. send (msg) flash ('email sent successfully! ') # The message "failed T: flash" ('email failed to be sent! ') Return render_template('index.html', form = mailForm, name = 'xxxxxx @ 163.com ') # render the webpage if _ name _ =' _ main _ ': app. run (debug = True)

  

I have hidden both my mailbox and password in the Code. If you want to test it, replace it with your own mailbox and password. Remember that this password is not a logon password, the management password entered when smtp is enabled.
Mail core code:
1. Configure the smtp server first:
# Below is the SMTP server configuration

App. config ['mail _ Server'] = 'smtp .163.com '# host name or IP address of the email SERVER
App. config ['mail _ port'] = '25' # PORT of the email server
App. config ['mail _ USE_TLS '] = True # enable Transport Layer Security
App. config ['mail _ username'] = 'xxxxxx @ 163.com '# OS. environ. get ('mail _ username') # email account USERNAME
App. config ['mail _ password'] = 'xxxxxx' # OS. environ. get ('mail _ password') # email account PASSWORD

2. send an email:

Msg = Message (styledata, sender = 'xxxxxx @ 163.com ', recipients = [receiverName]) # sender, recipient
Msg. body = bodydata
Mail. send (msg)

Fill in the sender, recipient, subject, body, and then send it.

Finally, let's take a look at the demo image:

At this time, my QQ mailbox has received an email:

Today's sharing is here. The development of the Seven-night music platform is in progress. Please stay tuned and remember to enjoy it.

 

Welcome to support my public account:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.