最近python用cron进行邮件推送的任务:
1、crontab e 进行添加
* 7 * * * root export LC_ALL=”zh_CN.UTF-8″ && /root/anaconda/bin/python /var/www/html/stock/send_mail_qq.py >> /var/log/stock/send_mail_qq.log
2、/var/log/cron 查看执行日志
3、注意要写python完整的路径,不然执行会有问题
代码如下:
#-*- encoding: utf-8 -*-
import os, sys
import smtplib
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText
mailInfo = {
“from”: “***”,
“to”: “***”,
“hostname”: “smtp.qq.com”,
“username”: “***”,
“password”: “***”,
“mailsubject”: “this is test”,
“mailhtml”: “<html><h1>你好,python发送邮件测试</h1></html>”,
“mailencoding”: “utf-8”
}
if __name__ == ‘__main__’:
smtp = SMTP_SSL(mailInfo[“hostname”])
smtp.set_debuglevel(1)
smtp.ehlo(mailInfo[“hostname”])
smtp.login(mailInfo[“username”],mailInfo[“password”])
msg = MIMEText(mailInfo[“mailhtml”],”html”,mailInfo[“mailencoding”])
msg[“Subject”] = Header(mailInfo[“mailsubject”],mailInfo[“mailencoding”])
msg[“from”] = mailInfo[“from”]
msg[“to”] = mailInfo[“to”]
smtp.sendmail(mailInfo[“from”], mailInfo[“to”], msg.as_string())
smtp.quit()
注意:要开通qq邮箱设置里面的 stmp开关,才有效