python学习:python 发送邮件实例源码 |

  • A+
所属分类:Seay信息安全博客

显示不全请点击全屏阅读

不断学习,不断钻研,才能不断进步

# 导入 smtplib 和 MIMEText 
import smtplib 
from email.mime.text import MIMEText 

# 定义发送列表 
mailto_list=["[email protected]","[email protected]"] 

# 设置服务器名称、用户名、密码以及邮件后缀 
mail_host = "smtp.163.com" 
mail_user = "[email protected]" 
mail_pass = "xx" 
mail_postfix="163.com" 

# 发送邮件函数 
def send_mail(to_list, sub, context): 
    '''''
    to_list: 发送给谁
    sub: 主题
    context: 内容
    send_mail("[email protected]","sub","context")
    ''' 
    me = mail_user + "<"+mail_user+"@"+mail_postfix+">" 
    msg = MIMEText(context) 
    msg['Subject'] = 'python email test' 
    msg['From'] = sub
    msg['To'] = ";".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 'success'
        return True 
    except (Exception, e): 
        print(str(e)) 
        print 'false'

send_mail(mailto_list,"test mail","你好")

Tags:

python资料,

如果您喜欢我的博客,欢迎点击图片定订阅到邮箱填写您的邮件地址,订阅我们的精彩内容: 也可以点击链接【订阅到鲜果】

如果我的想法或工具帮助到了你,也可微信扫下方二维码打赏本人一杯咖啡
python学习:python 发送邮件实例源码 |