使用gmail发送邮件的Python脚本

import smtplib
from email.Message import Message
from time import sleep

smtpserver = 'smtp.gmail.com'
username = '[email protected]'
password = '******'
from_addr = '[email protected]'
to_addr = '[email protected]'
cc_addr = '[email protected]'


message = Message()
message['Subject'] = 'Mail Subject'    #邮件标题
message['From'] = from_addr 
message['To'] = to_addr 
message['Cc'] = cc_addr 
message.set_payload('mail content')    #邮件正文
msg = message.as_string()


sm = smtplib.SMTP(smtpserver, port=587, timeout=20)
sm.set_debuglevel(1)                   #开启debug模式
sm.ehlo()
sm.starttls()                          #使用安全连接
sm.ehlo()
sm.login(username, password)
sm.sendmail(from_addr, to_addr, msg)
sleep(5)                               #避免邮件没有发送完成就调用了quit()
sm.quit()

你可能感兴趣的:(Linux,Python)