def send_mail(self, subject, message, files=None):
if files is None:
files = []
msg = MIMEMultipart()
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(message))
# TODO files attachment max size
if files is not None:
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(f, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
msg.attach(part)
self.logger.debug('Sending mail to {0} {1}'.format(self.to_address, ' about {0}'.format(subject)))
self.server.sendmail(self.from_username, self.to_address, msg.as_string())
self.logger.debug('Mail was sent.')
评论列表
文章目录