Home > Backend Development > Python Tutorial > Python sending email example - using smtplib module

Python sending email example - using smtplib module

高洛峰
Release: 2016-10-18 11:52:13
Original
1403 people have browsed it

# 导入 smtplib 和 MIMEText
import smtplib
from email.mime.text import MIMEText
    
# 定义发送列表
mailto_list=["root@pythontab.com","10118157@qq.com"]
    
# 设置服务器名称、用户名、密码以及邮件后缀
mail_host = "smtp.163.com"
mail_user = "xx@163.com"
mail_pass = "xx"
mail_postfix="163.com"
    
# 发送邮件函数
def send_mail(to_list, sub, context):
    '''''
    to_list: 发送给谁
    sub: 主题
    context: 内容
    send_mail("xxx@126.com","sub","context")
    '''
    me = mail_user + "<"+mail_user+"@"+mail_postfix+">"    msg = MIMEText(context)     msg[&#39;Subject&#39;] =&#39;python email test&#39;    msg[&#39;From&#39;] = sub    msg[&#39;To&#39;] = ";".join(to_list)     try:         send_smtp =smtplib.SMTP()         send_smtp.connect(mail_host)         send_smtp.login(mail_user, mail_pass)         send_smtp.sendmail(me, to_list, msg.as_string())         send_smtp.close()        print &#39;success&#39;        return True    except (Exception, e):         print(str(e))         print&#39;false&#39;  send_mail(mailto_list,"test mail","你好")
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template