我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用email.mime.base.MIMEBase()。
def test__all__(self): module = __import__('email') # Can't use sorted() here due to Python 2.3 compatibility all = module.__all__[:] all.sort() self.assertEqual(all, [ # Old names 'Charset', 'Encoders', 'Errors', 'Generator', 'Header', 'Iterators', 'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart', 'MIMENonMultipart', 'MIMEText', 'Message', 'Parser', 'Utils', 'base64MIME', # new names 'base64mime', 'charset', 'encoders', 'errors', 'generator', 'header', 'iterators', 'message', 'message_from_file', 'message_from_string', 'mime', 'parser', 'quopriMIME', 'quoprimime', 'utils', ])
def get_mime_message(text, html_text=None, **kwargs): if not html_text: instance = MIMEText(text) else: instance = MIMEMultipart('alternative') instance.attach(MIMEText(text, 'plain')) instance.attach(MIMEText(html_text, 'html')) extra = MIMEBase('application', 'octet-stream') extra.set_payload(b'test content') encoders.encode_base64(extra) extra.add_header('Content-Disposition', 'attachment', filename='report.pdf') instance.attach(extra) instance['X-Accept-Language'] = 'en-us, en' for key, value in kwargs.items(): instance[key] = value return instance
def format(self, parts, events, filename, *args): prefix, ext = os.path.splitext(filename) if ext.lower() == ".zip": zip_name = filename raw_name = prefix else: zip_name = filename + ".zip" raw_name = filename data = self.formatter.format(parts, events, *args) memfile = StringIO() zipped = zipfile.ZipFile(memfile, 'w', zipfile.ZIP_DEFLATED) zipped.writestr(raw_name, data.encode("utf-8")) zipped.close() memfile.flush() memfile.seek(0) part = MIMEBase("application", "zip") part.set_payload(memfile.read()) encode_base64(part) part.add_header("Content-Disposition", "attachment", filename=zip_name) parts.append(part) return u""
def book_msg(to_addr, book): # TODO: Give a subject. subject = '' mobi_workshop_dir = '../../mobi_workshop/' bookname = book+'.mobi' msg = MIMEMultipart() msg['From'] = mail_config['from_addr'] msg['To'] = to_addr msg['Subject'] = Header(subject, 'utf-8').encode() msg.attach(MIMEText('send with file...', 'plain', 'utf-8')) # TODO: check if target is 0kb, if so, send_alert. with open(mobi_workshop_dir+book+'.mobi', 'rb') as f: mime = MIMEBase('*', '*/*', filename=bookname) mime.add_header('Content-Disposition', 'attachment', filename=bookname) mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) encoders.encode_base64(mime) msg.attach(mime) return msg
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.')