python 发送邮件 图片显示问题_Python - 发送带图片邮件

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import smtplib

import os

import logging

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart

logger = logging.getLogger("django")

class EmailHandler(object):

def __init__(self, smtpserver, user, pwd):

self.smtp = smtplib.SMTP()

self.smtpserver = smtpserver

self.smtpuser = user

self.smtppwd = pwd

def generateAlternativeEmailMsgRoot(self, strFrom, listTo, listCc, strSubJect, strMsgText, strMsgHtml, listImagePath):

# Create the root message and fill in the from, to, and subject headers

msgRoot = MIMEMultipart('related')

msgRoot['Subject'] = strSubJect

msgRoot['From'] = strFrom

msgRoot['To'] = ",".join(listTo)

if listCc:

msgRoot['Cc'] = ",".join(listCc)

msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an

# 'alternative' part, so message agents can decide which they want to display.

msgAlternative = MIMEMultipart('alternative')

msgRoot.attach(msgAlternative)

msgContent = strMsgText.replace("\n","
") if strMsgText else ""

msgContent += "
" + strMsgHtml if strMsgHtml else ""

# We reference the image in the IMG SRC attribute by the ID we give it below

if listImagePath and len(listImagePath)>0:

msgHtmlImg = msgContent + "
"

for imgcount in range(0, len(listImagePath)):

msgHtmlImg += '
'.format(count=imgcount)

msgText = MIMEText(msgHtmlImg, 'html')

msgAlternative.attach(msgText)

# print(msgHtmlImg)

# This example assumes the image is in the current directory

for i,imgpath in enumerate(listImagePath):

fp = open(imgpath, 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

# Define the image's ID as referenced above

msgImage.add_header('Content-ID', ''.format(count=i))

msgRoot.attach(msgImage)

else:

msgText = MIMEText(msgContent, 'html')

msgAlternative.attach(msgText)

return msgRoot

# Send the email (this example assumes SMTP authentication is required)

def sendemail(self, strFrom, listTo, strSubJect, strMsgText, strMsgHtml=None, listImagePath=None, listCc=None):

msgRoot = self.generateAlternativeEmailMsgRoot(strFrom, listTo, listCc, strSubJect, strMsgText, strMsgHtml, listImagePath)

try:

self.smtp = smtplib.SMTP()

self.smtp.connect(self.smtpserver)

self.smtp.login(self.smtpuser, self.smtppwd)

if listCc:

listTo = listTo + listCc

self.smtp.sendmail(strFrom, listTo, msgRoot.as_string())

self.smtp.quit()

logger.info("Send mail success {0}".format(strSubJect))

except Exception as e:

logger.error("ERROR:Send mail failed {0} with {1}".format(strSubJect, str(e)))

# if __name__ == "__main__":

# smtpserver = 'smtp.exmail.qq.com'

# smtpport = 465

# username = 'max.bai@test.com'

# password = '123456'

# strFrom = 'max.bai@test.com'

# strTo = ['max.bai@test.com','max.bai@test.com']

# strCc = ['max.bai@test.com']

# strSubJect = 'test email - text with image'

# eh = EmailHandler(smtpserver,username,password)

# imgpath = "D:\images\cropper.jpg"

# imgpath2 = "D:\images\picture.jpg"

# # eh.sendemail(strFrom,strTo,"text mail","Hi it's Max, this is a test maill-----1","

test html content

")

# eh.sendemail(strFrom,strTo,"image mail","Hi it's Max,\n this is a test maill-----2","

test html content

", [imgpath,imgpath2], listCc=strCc)

# # eh.sendemail(strFrom,strTo,"image mail","Hi it's Max, this is a test maill-----2",listImagePath=[imgpath])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值