python通过163邮箱发送邮件

2023-01-31 01:01:14 python 邮箱 发送邮件
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import sys
import os
import datetime
import time
import Mysqldb
import subprocess




reload(sys)
sys.setdefaultencoding("utf-8")




mailto_list = ["xxx@163.com"]  #目标邮箱
mail_host = "smtp.163.com"    
mail_user = "xxx@163.com"  
mail_pass = "lecpuedugk"  #163邮箱smtp生成的密码



def send_mail(to_list, sub, content):
    me = "LogServer"+"<"+mail_user+">"
    msg = MIMEText(content, _subtype='plain', _charset='utf-8')
    msg['Subject'] = sub    
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user, mail_pass)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False


if __name__ == '__main__':
        send_mail(mailto_list, 'submit', 'content')

相关文章