-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
41 lines (33 loc) · 1.51 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#encoding:utf-8
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import argparse
def send_email(**kw):
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
msg = MIMEText(kw['message'], 'plain', 'utf-8')
msg['From'] = _format_addr('%s <%s>' % (kw['name'], kw['efrom']))
msg['To'] = _format_addr('%s <%s>' % (kw['recipient_name'], kw['to']))
msg['Subject'] = Header(kw['title'], 'utf-8').encode()
server = smtplib.SMTP(kw['server'], 25)
server.login(kw['efrom'], kw['password'])
server.sendmail(kw['efrom'], [kw['to']], msg.as_string())
server.quit()
def main():
parser = argparse.ArgumentParser(description='Simple email sender.')
parser.add_argument('-f', '--from', default='liukunxin1@163.com', dest='efrom')
parser.add_argument('-p', '--password', default='liukunxin1')
parser.add_argument('-t', '--to', default='liukunxin1@163.com')
parser.add_argument('-s', '--server', help='smtp server', default='smtp.163.com')
parser.add_argument('-m', '--message', default='hello smtp')
parser.add_argument('-ti', '--title', default='title')
parser.add_argument('-n', '--name', default='Anonymous')
parser.add_argument('-rn', '--recipient_name', default='Dear')
args = parser.parse_args()
send_email(**vars(args))
if __name__ == "__main__":
main()