在 gmail api 中添加内嵌图片会导致图片为空

Adding inline image in gmail api results in empty image

我正在尝试使用以下功能在电子邮件中发送内联图像:

def create_message_without_attachment (sender, to, subject, message_text_html, message_text_plain,image):

message = MIMEMultipart()
message['Subject'] = subject
message['From'] = sender
message['To'] = to

img = MIMEImage(image.read())
img.add_header('Content-Id', '<image1>')
message.attach(img)

message.attach(MIMEText('<p><img src="cid:image1" /></p>'+message_text_html, 'html'))

raw_message_no_attachment = base64.urlsafe_b64encode(message.as_bytes())
raw_message_no_attachment = raw_message_no_attachment.decode()
body  = {'raw': raw_message_no_attachment}
return body

但是当我收到电子邮件时,我就得到了

<p> <img> </p>

感谢任何帮助。

def create_message_without_attachment (sender, to, subject, message_text_html, message_text_plain,image):

message = MIMEMultipart()
message['Subject'] = subject
message['From'] = sender
message['To'] = to

message.attach(MIMEText('<p><img src="cid:image1" /></p>'+message_text_html, 'html'))

image.seek(0)
img = MIMEImage(image.read(), 'png')
img.add_header('Content-Id', '<image1>')
img.add_header("Content-Disposition", "inline", filename="image1")
message.attach(img)

raw_message_no_attachment = base64.urlsafe_b64encode(message.as_bytes())
raw_message_no_attachment = raw_message_no_attachment.decode()
body  = {'raw': raw_message_no_attachment}
return body

另外这个函数在另一个文件中。 所以,当我导入并调用这个函数时 "Spyder", 它没有用,因为 Spyder 使用的是导入文件的 old/cached 版本...