我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用email.mime()。
def _send_system_email(config, subject, recips, body): try: fromaddr = config.get('email', 'fromaddr') smtphost = config.get('email', 'smtphost') except (configparser.NoOptionError, configparser.NoSectionError): raise MissingEmailConfig() msg = email.mime.text.MIMEText(body) msg['Subject'] = subject msg['From'] = fromaddr msg['Date'] = email.utils.formatdate() msg['Message-Id'] = email.utils.make_msgid('concord232') for addr in recips: msg['To'] = addr smtp = smtplib.SMTP(smtphost) smtp.sendmail(fromaddr, recips, msg.as_string()) smtp.quit()
def to_mime(self): """Return a MIME representation of this object - used internally""" e = email.mime.multipart.MIMEMultipart() e.set_charset(self.charset) e['Subject'] = self.subject maintype, subtype = self.mimetype.lower().split('/', 1) m = email.mime.text.MIMEText(self.content, _subtype=subtype, _charset=self.charset) e.attach(m) for h, hv in self.headers: e[h] = hv for a in self.attachments: maintype, subtype = a.mimetype.lower().split('/', 1) if maintype == 'image': aclass = email.mime.image.MIMEImage else: aclass = email.mime.application.MIMEApplication mimea = aclass(a.data, _subtype=subtype) mimea.add_header('Content-Disposition', 'attachment', filename=a.filename) e.attach(mimea) return e