使用 Flask-Mail 发送图片(正文)邮件,在 macOS 系统 Mail 客户端上,图片出现重复展示

问题描述:
用flask mail发送图片邮件(正文中的图片),在mac系统邮件客户端上,图片出现重复展示问题,即展示了正文图片,同时也把图片作为附件进行了展示,用原生smtp方式发送的图片邮件没有出现该问题;
(只在mac上出现,windows上没有出现)

求助点:
发送的图片邮件,在mac上出现重复展示问题,一张是正文,一张是附件,需要了解flask mail 发送图片邮件,通过什么方式能在mac上不重复展示(mac自带邮箱客户端):

代码:

flask mail发送邮件代码:

from flask import *
from flask_mail import *
import base64

app = Flask(__name__)

app.config["MAIL_SERVER"] = 'smtp.jiguang.cn'
app.config["MAIL_PORT"] = 465
app.config["MAIL_USERNAME"] = 'xiaxx@jiguang.cn'
app.config['MAIL_PASSWORD'] = 'xxxx'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

mail = Mail(app)


@app.route('/')
def send_email():
    # send_email_func('emailTemplate', img=img)
    send_email_func()
    return "sent ok"


def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)


def send_email_func():
    app = current_app._get_current_object()
    msg = Message(subject="hello", body="hello", sender="xiaxx@jiguang.cn", recipients=["xiaxx@jiguang.cn"])
    # msg.body = render_template(template + '.txt', **kwargs)
    # msg.html = render_template(template + '.html', **kwargs)
    msg.html = '<img src="cid:image"> <p>用flask mail发送图片邮件,测试邮件发送内联图片功能</p>'
    with app.open_resource("test.png") as fp: # 打开本地图片即可
        msg.attach("image.png", "image/png", fp.read(), 'inline', headers=[('Content-ID', 'image')])
    send_async_email(app, msg)


if __name__ == '__main__':
    app.run(debug=True)

smtp方式代码(这种方式发送的图片邮件在mac上能正常展示,没有重复):
# Send an HTML email with an embedded image and a plain text message for
# email clients that don’t want to display the HTML.
from email.encoders import encode_base64
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

# Define these once; use them twice!
strFrom = 'xiaxx@jiguang.cn'
strTo = 'xiaxx@jiguang.cn'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = 'xiaxx@jiguang.cn'
msgRoot['To'] = 'xiaxx@jiguang.cn'
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)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('用smtp发送图片邮件,测试邮件发送功能<img src="cid:image1"><br>Nifty!', 'html')
msgAlternative.attach(msgText)

# This example assumes the image is in the current directory
fp = open('test.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.jiguang.cn')
smtp.login('xiaxx@jiguang.cn', 'xxxx')
# smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.sendmail(strFrom, strTo, msgRoot.as_bytes())
smtp.quit()

mac系统邮件客户端截图:

flask mail发送的图片邮件(重复展示,一张是正文,一张是附件)

求大佬帮忙看下,flask mail通过什么属性能让图片邮件在mac上能不重复展示

该问题已解决,整理了解决方法到博客中,感兴趣的可以看下:

1 Like

感谢辉哥的关注

客气啦!最近太忙了,还没来得及研究你的问题 :sweat_smile: