-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend_email.py
70 lines (61 loc) · 2.17 KB
/
send_email.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import smtplib
import os
import mimetypes
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
mail_poster = {}
mail_poster['163'] = {}
mail_poster['163']['host'] = "smtp.163.com"
mail_poster['163']['user'] = "pedestal_peter"
mail_poster['163']['pass'] = "pedestaldlut"
mail_poster['163']['postfix'] = "163.com"
mail_poster['qq'] = {}
mail_poster['qq']['host'] = "smtp.qq.com"
mail_poster['qq']['user'] = "pedestal_scott"
mail_poster['qq']['pass'] = "pedestaldlut"
mail_poster['qq']['postfix'] = "qq.com"
def send_mail(to_email, sub, context):
#if re.findall(re_email, to_email)[0][1] == 'qq':
# poster = 'qq'
#else:
# poster = '163'
poster = '163'
me = mail_poster[poster]['user'] + "<" + mail_poster[poster]['user'] + "@" + mail_poster[poster]['postfix'] + ">"
msg = MIMEMultipart()
msg['Subject'] = sub
msg['From'] = me
msg['To'] = to_email
msg.attach(MIMEText('test'))
ctype, encoding = mimetypes.guess_type('email_input.txt.enc')
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
attachment = MIMEImage((lambda f: (f.read(), f.close()))(open('email_input.txt.enc', 'rb'))[0], _subtype = subtype)
attachment.add_header('Content-Disposition', 'attachment', filename = 'email_input.txt.enc')
msg.attach(attachment)
#print msg
print mail_poster[poster]
#try:
send_smtp = smtplib.SMTP()
send_smtp.connect(mail_poster[poster]['host'])
send_smtp.login(mail_poster[poster]['user'], mail_poster[poster]['pass'])
send_smtp.sendmail(me, to_email, msg.as_string())
send_smtp.close()
return True
#except (Exception, e):
# print(str(e))
# return False
if __name__ == '__main__':
to_email = raw_input('Input email:')
sub = raw_input('Input sub:')
context_path = raw_input('Input context path:')
with open(context_path) as inputfile:
context = inputfile.read()
print to_email
print sub
print context
send_mail(to_email, sub, context)