本文整理汇总了Python中email.MIMEImage.MIMEImage.read方法的典型用法代码示例。如果您正苦于以下问题:Python MIMEImage.read方法的具体用法?Python MIMEImage.read怎么用?Python MIMEImage.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.MIMEImage.MIMEImage
的用法示例。
在下文中一共展示了MIMEImage.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_email
# 需要导入模块: from email.MIMEImage import MIMEImage [as 别名]
# 或者: from email.MIMEImage.MIMEImage import read [as 别名]
def send_email(_to="[email protected]",
_from="[email protected]"):
# declare self as host
HOST = 'localhost'
# create intermediate base class for MIME message
msg = MIMEMultipart('mixed', 'multi_outer')
# form headers of letter(from, to, subject, date)
msg["From"] = _from
msg["To"] = _to
msg["Subject"] = u"Резюме на должность веб-разработчика."
msg['Date'] = formatdate(localtime=True)
# attach a text to the letter
mail_text = MIMEBase('text', 'plain', charset='utf-8')
mail_text.set_payload('Резюме: Дыкин Сергей Александрович')
Encoders.encode_base64(mail_text)
msg.attach(mail_text)
# create intermediate base class for MIME message inside another one
msg_inside = MIMEMultipart('alternative', 'multi_inner')
msg.attach(msg_inside)
# attach embedded a image(photo) to the letter
attached_image = open(image_path, "rb")
attached_image = MIMEImage(attached_image.read())
attached_image.add_header('Content-Disposition', 'inline;" +\
" filename="%s"' % os.path.basename(image_path))
attached_image.add_header('Content-ID', '<photo>')
msg_inside.attach(attached_image)
# attach a html-file to the letter
mail_html = MIMEBase('text', 'html', charset='utf-8')
mail_html.set_payload(open(html_path, "rb").read())
Encoders.encode_base64(mail_html)
msg_inside.attach(mail_html)
# attach a text-file(with program code) to the letter
part = MIMEBase('application', 'octet-stream')
attached_file = open(file_path, "rb")
part.set_payload(attached_file.read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(file_path))
msg.attach(part)
# create SMTP connection
server = smtplib.SMTP(HOST)
# send letter, if successful then print confirm message
try:
failed = server.sendmail(_from, _to, msg.as_string())
server.close()
print "Letter was sent."
# if something wrong, print error message
except Exception, e:
error_msg = "Unable to send letter.\nError: {0}".format(str(e))
print error_msg