似乎遵循gmail电子邮件模板有效:
@H_301_3@* multipart/alternative - text/plain - multipart/related + text/html <img src="cid:msgid"/> + image/png Content-ID: <msgid>
基于email
模块文档中的示例:
@H_301_3@#!/usr/bin/env python3 import html import mimetypes from email.headerregistry import Address from email.message import EmailMessage from email.utils import make_msgid from pathlib import Path title = 'Picture report…' path = Path('TESTING.png') me = Address("Pepé Le Pew", *gmail_user.rsplit('@', 1)) msg = EmailMessage() msg['Subject'] = 'Report…' msg['From'] = me msg['To'] = [me] msg.set_content('[image: {title}]'.format(title=title)) # text/plain cid = make_msgid()[1:-1] # strip <> msg.add_alternative( # text/html '<img src="cid:{cid}" alt="{alt}"/>' .format(cid=cid, alt=html.escape(title, quote=True)), subtype='html') maintype, subtype = mimetypes.guess_type(str(path))[0].split('/', 1) msg.get_payload()[1].add_related( # image/png path.read_bytes(), maintype, subtype, cid="<{cid}>".format(cid=cid)) # save to disk a local copy of the message Path('outgoing.msg').write_bytes(bytes(msg))
@H_301_3@msg通过gmail发送:
@H_301_3@import smtplib import ssl with smtplib.SMTP('smtp.gmail.com', timeout=10) as s: s.starttls(context=ssl.create_default_context()) s.login(gmail_user, gmail_password) s.send_message(msg)