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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 20 16:08:17 2019
@author: litao
发送有附件的 邮件
"""
import os
import smtplib
# For guessing MIME type based on file name extension
import mimetypes
from email import encoders
#from email.message import Message
#from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
#from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#COMMASPACE = ', '
def send_file_email(file_path, data_str, email_group=[],
email_msg_body_str=None,
title_str=None,
cc_group=["litao@igengmei.com"], sender=None,file=None):
directory = file_path
# Create the enclosing (outer) message
if email_msg_body_str == None:
email_msg_body = '''{0} '''.format(data_str=data_str)
else:
email_msg_body = email_msg_body_str
outer = MIMEMultipart()
if title_str == None:
title = '-' + data_str
else:
title = title_str
outer['Subject'] = title
outer['To'] = ','.join(email_group)
outer['Cc'] = ','.join(cc_group)
if not sender:
outer['From'] = 'litao@igengmei.com'
else:
outer['From'] = sender
mail_service = 'smtp.exmail.qq.com'
outer.attach(MIMEText(email_msg_body))
if file:
if not os.path.isfile(file):
pass
file_path,file_name = os.path.split(file)
# Guess the content type based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files.
ctype, encoding = mimetypes.guess_type(file)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
# if maintype == 'text':
## with open(path,encoding='utf-8') as fp:
### Note: we should handle calculating the charset
## msg = MIMEText(fp.read(), _subtype=subtype)
# continue
if maintype == 'image':
# with open(path, 'rb') as fp:
# msg = MIMEImage(fp.read(), _subtype=subtype)
pass
elif maintype == 'audio':
# with open(path, 'rb') as fp:
# msg = MIMEAudio(fp.read(), _subtype=subtype)
pass
else:
with open(file, 'rb') as fp:
msg = MIMEBase(maintype, subtype, charset='gb18030')
msg.set_payload(fp.read())
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=file_name)
outer.attach(msg)
elif directory:
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if not os.path.isfile(path):
continue
# Guess the content type based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files.
ctype, encoding = mimetypes.guess_type(path)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
# if maintype == 'text':
## with open(path,encoding='utf-8') as fp:
### Note: we should handle calculating the charset
## msg = MIMEText(fp.read(), _subtype=subtype)
# continue
if maintype == 'image':
# with open(path, 'rb') as fp:
# msg = MIMEImage(fp.read(), _subtype=subtype)
continue
elif maintype == 'audio':
# with open(path, 'rb') as fp:
# msg = MIMEAudio(fp.read(), _subtype=subtype)
continue
else:
with open(path, 'rb') as fp:
msg = MIMEBase(maintype, subtype, charset='gb18030')
msg.set_payload(fp.read())
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
# Now send or store the message
# composed = outer.as_string()
server = smtplib.SMTP_SSL(mail_service,465)
server.login("litao@igengmei.com","Lemo1995")
server.send_message(outer)
server.quit()
if __name__ == '__main__':
send_file_email("",'',sender="litao@igengmei.com",email_group=["litao@igengmei.com"],email_msg_body_str="test",title_str="test",cc_group=["litao@igengmei.com"],file="/srv/apps/readelf/shield.text")