附上python邮件发送脚本(对于发送测试邮件不成功的情况可以看下debug中的错误代码提示,如(554, DT:SPM)一般情况是发送的邮件被视为垃圾邮件和病毒邮件,需要调整发件内容):
#!/usr/bin/python # -*- coding: utf-8 -*- #python-version:2.7 from email import encoders from email.header import Header from email.mime.text import MIMEText from email.utils import parseaddr, formataddr import smtplib import sys def send_mail(_to_email,_subject,_message): from_addr = "xxxx@163.com" password = "your_password" smtp_server = "smtp.163.com" msg = MIMEText(_message, ‘plain‘, ‘utf-8‘) msg[‘Subject‘] = _subject server = smtplib.SMTP(smtp_server, 25) # 使用SSL加密传输 #server = smtplib.SMTP_SSL(smtp_host,994) # 对发送过程开启调试模式 server.set_debuglevel(1) server.login(from_addr, password) server.sendmail(from_addr, [_to_email], msg.as_string()) server.quit() if __name__==‘__main__‘: send_mail(sys.argv[1],sys.argv[2],sys.argv[3])
本文出自 “Trying” 博客,请务必保留此出处http://tryingstuff.blog.51cto.com/4603492/1923962
原文地址:http://tryingstuff.blog.51cto.com/4603492/1923962