Last active
March 12, 2016 08:43
-
-
Save MOOOWOOO/d32f33712abcd85cd9ea to your computer and use it in GitHub Desktop.
python发邮件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[mail] | |
smtp_server = smtp.your-service.com | |
to_addr = [email protected] | |
from_addr = [email protected] | |
nickname = nick-name | |
password = your-password | |
prefix = subject-prefix | |
port = 25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
""" | |
""" | |
import argparse as ap | |
import smtplib | |
import sys | |
from email import encoders | |
from email.header import Header | |
from email.mime.base import MIMEBase | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.utils import parseaddr, formataddr | |
from configobj import ConfigObj | |
import os | |
__author__ = 'Jux.Liu' | |
def modINI(section, option, value=None, filename='config.ini'): | |
""" | |
修改配置文件内容 | |
:param section: 配置标签名称 | |
:param option: 配置项名称 | |
:param value: 配置项值 | |
:param filename: 配置文件名 | |
""" | |
config = ConfigObj(filename, encoding='UTF8') | |
res = '' | |
try: | |
if len(section): | |
config[section][option] = value | |
else: | |
config[option] = value | |
config.write() | |
res = True | |
except Exception as e: | |
res = False | |
finally: | |
return res | |
def getINI(section, option, filename='config.ini'): | |
value = '' | |
config = ConfigObj(filename, encoding='UTF8') | |
try: | |
if len(section) and len(option): | |
value = config[section][option] | |
elif len(section): | |
value = config[section] | |
elif len(option): | |
value = config[option] | |
else: | |
raise Exception | |
except Exception as e: | |
value = '' | |
finally: | |
return value | |
class MAIL(): | |
def __init__(self): | |
self.from_addr = getINI('mail', 'from_addr') | |
self.nickname = getINI('mail', 'nickname') | |
self.password = getINI('mail', 'password') | |
self.to_addr = getINI('mail', 'to_addr') | |
self.smtp_server = getINI('mail', 'smtp_server') | |
self.prefix = getINI('mail', 'prefix') | |
self.port = getINI('mail', 'port') | |
def organize_msg(self, subject, msg, attach_file=None): | |
_ = self.msg = MIMEMultipart() | |
try: | |
if len(attach_file) != 0: | |
with open(attach_file, 'rb') as fh: | |
self.mime = MIMEBase(_maintype='image', | |
_subtype=attach_file.split('.').pop(), | |
filename=attach_file.split(os.sep).pop()) | |
self.mime.add_header('Content-Disposition', 'attachment', filename='test.jpg') | |
self.mime.add_header('Content-ID', '<0>') | |
self.mime.add_header('X-Attachment-Id', '0') | |
self.mime.set_payload(fh.read()) | |
encoders.encode_base64(msg=self.mime) | |
self.msg.attach(payload=self.mime) | |
# todo: mod msg template | |
except Exception as e: | |
# todo: mod msg template | |
pass | |
finally: | |
_.attach(MIMEText(msg, 'html', 'utf-8')) | |
_['From'] = _format_addr('mostfun <{0}>'.format(self.from_addr)) | |
_['To'] = _format_addr('{0} <{1}>'.format(self.nickname, self.to_addr)) | |
_['Subject'] = Header(s='{0}: {1}'.format(self.prefix, subject), charset='utf-8').encode() | |
def send(self): | |
res = False | |
try: | |
_ = self.server = smtplib.SMTP(host=self.smtp_server, port=self.port) | |
_.starttls() | |
_.set_debuglevel(debuglevel=1) | |
_.login(user=self.from_addr, password=self.password) | |
_.sendmail(from_addr=self.from_addr, to_addrs=[self.to_addr], msg=self.msg.as_string()) | |
_.quit() | |
res = True | |
except Exception as e: | |
raise e | |
finally: | |
return res | |
def _format_addr(s): | |
name, addr = parseaddr(s) | |
return formataddr((Header(s=name, charset='utf-8').encode(), addr)) | |
def main(*args): | |
m = MAIL() | |
m.organize_msg(subject=args[0].subject, msg=args[0].msg, attach_file=args[0].attach) | |
if m.send(): | |
print('ok') | |
else: | |
print('error') | |
if __name__ == "__main__": | |
p = ap.ArgumentParser() | |
# p.parse_args() | |
p.add_argument('-s', '--subject' action='store') | |
p.add_argument('-m', '--msg', action='store') | |
p.add_argument('-a', '--attach' action='store') | |
args, remaining = p.parse_known_args(sys.argv) | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment