【Python】一键式发邮件小脚本

 

     工作中涉及到给客户定时发邮件,这里提供一个Python小脚本,单独放进一个py小文件,每次用时import一下非常简单方便。

#!/usr/bin/env python
# -*- coding: utf-8 -*-"""
create_author: 蛙鳜鸡鹳狸猿
create_time  : 2016-10-10
program      : *_*send email*_*
"""import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipartclass Smtp(object):def __init__(self, host, user, password, port=25):"""SMTP server init. See alsohttps://docs.python.org/2.7/library/smtplib.html:param host: SMTP server host.:param user: SMTP server user.:param password: SMTP server password.:param port: SMTP server port."""self.host = hostself.port = portself.user = userself.password = passwordself.smtp = smtplib.SMTP()def __enter__(self):self.smtp.connect(host=self.host, port=self.port)self.smtp.login(user=self.user, password=self.password)return self.smtpdef __exit__(self, exc_type, exc_val, exc_tb):self.smtp.quit()class Mail(object):def __init__(self, _from, _to, _cc=None, _bcc=None, _subject='', _body='', _attach=None):"""Mail object init. See alsohttps://docs.python.org/2.7/https://tools.ietf.org/html/rfc4021There is a BUG remained that `cc` and `bcc` accounts display in recipients list and can not be hidden. See alsohttps://stackoverflow.com/questions/1546367/python-how-to-send-mail-with-to-cc-and-bcc:param _from: mail sender.:param _to: mail recipients list.:param _cc: mail cc recipients list.:param _bcc: mail bcc recipients list.:param _subject: mail subject.:param _body: mail body.:param _attach: mail attachments list."""self._from = _fromself._to = _toself._cc = _ccself._bcc = _bccself._subject = _subjectself._body = _bodyself._attach = _attachdef construct_mail(self):msg = MIMEMultipart()msg['From'] = self._frommsg['To'] = ','.join(self._to)if self._cc:msg["Cc"] = ','.join(self._cc)if self._bcc:msg["Bcc"] = ','.join(self._bcc)msg['subject'] = self._subjectmsg.attach(MIMEText(self._body, _charset="utf-8"))if self._attach:for _ in self._attach:msg_attach = MIMEText(open(_[0] + _[1], "rb").read(), _subtype="base64", _charset="utf-8")msg_attach.add_header("Content-Disposition", "attachment", filename=_[1])msg_attach["Content-Type"] = "application/octet-stream"msg.attach(msg_attach)return msgdef send_mail(self, smtp):tos = self._toif self._cc:tos += self._ccif self._bcc:tos += self._bccsmtp.sendmail(self._from, tos, self.construct_mail().as_string())if __name__ == "__main__":SMTP = {"host": "localhost","port": 587,"user": "zoo","password": "1024",}MAIL = {"_from": "noreply@mail.com","_to": ["to_a@mail.com", "to_b@mail.com"],"_cc": ["cc_a@mail.com", "cc_b@mail.com"],"_bcc": ["bcc_a@mail.com", "bcc_b@mail.com"],"_subject": "test mail","_body": "test mail\n\t(generate by Python2.7.17)","_attach": [["/home/zoo/", "attach_a.xlsx", ], ],}with Smtp(**SMTP) as s:Mail(**MAIL).send_mail(s)

     也就是把所有的参数放进一个大字典,直接取用就可以了,非常一目了然。在Linux服务器上,当然也可以非常简单地进行Shell发邮件,参考:http://blog.csdn.net/sweeper_freedoman/article/details/52782345。

 




本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部