本文整理匯總了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')
#.........這裏部分代碼省略.........