本文整理汇总了Python中email.mime.multipart.MIMEBase.set_param方法的典型用法代码示例。如果您正苦于以下问题:Python MIMEBase.set_param方法的具体用法?Python MIMEBase.set_param怎么用?Python MIMEBase.set_param使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.mime.multipart.MIMEBase
的用法示例。
在下文中一共展示了MIMEBase.set_param方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send
# 需要导入模块: from email.mime.multipart import MIMEBase [as 别名]
# 或者: from email.mime.multipart.MIMEBase import set_param [as 别名]
def send(self, emails):
if isinstance(emails, Email):
emails = [emails]
if len([e for e in emails if e.__class__ != Email]):
raise TypeError('emails must be Email or list of Email instances')
smtpclass = SMTP_SSL if self.ssl else SMTP
if self.server == 'localhost':
smtp = smtpclass(self.server)
else:
smtp = smtpclass(self.server, self.port)
if self.tls:
smtp.starttls()
if self.login and self.password:
smtp.login(self.login, self.password)
for email in emails:
c = Charset(email.charset)
c.header_encoding = QP
c.body_encoding = 0
r = Charset(email.charset)
r.header_encoding = 0
r.body_encoding = 0
email.normalize_email_list('rcpt')
email.normalize_email_list('cc')
email.normalize_email_list('bcc')
mime1, mime2 = email.mimetype.split('/')
mainpart = MIMEBase(mime1, mime2)
if not email.force_7bit:
mainpart.set_param('charset', email.charset)
if len(email.attachments):
message = MIMEMultipart('mixed')
message.attach(mainpart)
del mainpart['mime-version']
else:
message = mainpart
message['Date'] = datetime.datetime.now().strftime(
'%a, %d %b %Y %H:%M:%S') + (" +%04d" % (time.timezone/-36,))
h = Header(maxlinelen=1000) # FIXME: what is correct max length?
fromname = self.fromname.encode(email.charset, 'xmlcharrefreplace')
h.append(fromname, r if is7bit(fromname) else c)
h.append('<%s>' % self.email, r)
message['From'] = h
message['To'] = email.get_emails_header('rcpt')
if len(email.cc):
message['CC'] = email.get_emails_header('cc')
if len(email.bcc):
message['BCC'] = email.get_emails_header('bcc')
subject = email.subject.encode(email.charset, 'xmlcharrefreplace')
message['Subject'] = Header(subject, r if is7bit(subject) else c)
if email.reply_to:
message['Reply-To'] = email.get_emails_header('reply_to')
if email.force_7bit:
body = email.body.encode('ascii', 'xmlcharrefreplace')
else:
body = email.body.encode(email.charset, 'xmlcharrefreplace')
mainpart.set_payload(body)
if is7bit(body):
mainpart['Content-Transfer-Encoding'] = '7bit'
else:
encode_quopri(mainpart)
for attachment in email.attachments:
if attachment.__class__ != Attachment:
raise TypeError("invalid attachment")
mimetype = attachment.mimetype
if not mimetype:
mimetype, encoding = guess_type(attachment.filename)
if not mimetype:
mimetype = 'application/octet-stream'
mime1, mime2 = mimetype.split('/')
part = MIMEBase(mime1, mime2)
# using newer rfc2231 (not supported by Outlook):
# part.set_param('name', attachment.filename.encode('utf-8'), charset = 'utf-8')
# hack: using deprecated rfc2047 - supported by Outlook:
part.set_param('name', str(Header(attachment.filename)))
del part['mime-version']
if attachment.id:
part['Content-Disposition'] = 'inline'
else:
part['Content-Disposition'] = 'attachment'
# using newer rfc2231 (not supported by Outlook):
# part.set_param('filename',
# attachment.filename.encode('utf-8'),
# 'Content-Disposition',
# charset = 'utf-8')
#.........这里部分代码省略.........