SMTP小魔法–自动点饭提醒

3,075次阅读
没有评论

共计 888 个字符,预计需要花费 3 分钟才能阅读完成。

经常上班发现周围的同事忘记点饭了,因此萌生了使用邮件的方法给同事点饭提醒,因为产生的这篇博文,二话不说上代码

 

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))
mail_host="smtp.126.com"  
mail_user="**@126.com"    
mail_pass="passwd" 


sender = '**@126.com'
receivers = ['**@126.com','**@163.com']

message = MIMEText('大家快点点饭啊!', 'plain', 'utf-8')
message['From'] = _format_addr('老司机 <%s>' % mail_user)
#message['To'] = _format_addr('小伙伴<%s>' % receivers)
message['To']=','.join([ _format_addr('小伙伴 <%s>' % x) for x in receivers ])
subject = '点饭专用'
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP() 
    #smtpObj.set_debuglevel(1)
    smtpObj.connect(mail_host, 25)  
    smtpObj.login(mail_user,mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()
    print "success"
except smtplib.SMTPException:
    print "Error: can not send mail"
正文完
请博主喝杯咖啡吧!
post-qrcode
 
admin
版权声明:本站原创文章,由 admin 2016-11-10发表,共计888字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码