python官方示例发送带附件邮件,python官方示例附件,下面的示例代码发送带图片


下面的示例代码发送带图片附件的邮件

# 导入smtplib包import smtplib# Here are the email package modules we'll needfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartCOMMASPACE = ', '# Create the container (outer) email message.msg = MIMEMultipart()msg['Subject'] = 'Our family reunion'# me == the sender's email address# family = the list of all recipients' email addressesmsg['From'] = memsg['To'] = COMMASPACE.join(family)msg.preamble = 'Our family reunion'# Assume we know that the image files are all in PNG formatfor file in pngfiles:    # Open the files in binary mode.  Let the MIMEImage class automatically    # guess the specific image type.    fp = open(file, 'rb')    #初始化附件对象    img = MIMEImage(fp.read())    fp.close()    #添加附件到msg    msg.attach(img)# 使用SMTP server发送邮件s = smtplib.SMTP('localhost')s.sendmail(me, family, msg.as_string())s.quit()

评论关闭