Windows上的Outlook调整电子邮件图像的大小

问题描述

我具有以下发送电子邮件功能

import logging
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

import boto3


def send_email(body,subject,recipients,region_name='us-east-1',sender='default@sender.com',attachments=False):
    logging.info('Generating email from {} to {} about {}'.format(sender,str(recipients),subject))

    message = MIMEMultipart()
    message['Subject'] = subject
    message['From'] = sender
    message['To'] = ','.join(recipients)

    logging.info('Adding attachments...')

    if attachments:
        for attachment in attachments:
            logging.info('Adding ' + attachment)

            f = open(os.path.normpath(attachment),'rb')
            a = MIMEApplication(f.read())
            f.close()
            a.add_header('Content-disposition','attachment',filename=os.path.basename(attachment))
            message.attach(a)

            logging.info('Attachment added!')

    else:
        logging.info('No attachments found!')

    logging.info('Adding body...')

    part = MIMEText(body,'html')
    message.attach(part)

    client = boto3.client('ses',region_name=region_name)

    logging.info('Sending...')
    client.send_raw_email(
        Source=message['From'],Destinations=recipients,RawMessage={
            'Data': message.as_string()
        }
    )

    logging.info('Sent!')

这就像一种魅力,我在各种用例中使用它。我的用例之一是在电子邮件正文中发送图像。发生这种情况时,我会执行以下操作:

send_email('''<img src="cid:{}" width={} height={}>'''.format(final_image_name,width_of_new_image,height_of_new_image),'Email with body image',attachments=['/tmp/{}'.format(final_image_name),])

它有效。在iPad,iPhone,Mail应用程序和Outlook for Mac中打开电子邮件时,一切都完美无缺。但是...当我的同事在Windows中的Outlook中打开它们时,一切都变得一团糟。有些人看到很小的图像,有些人看到扭曲的图像,有些人必须制作250%的图像才能看到可读的电子邮件。由于除了Windows上的Outlook之外,这对其他所有功能都有效,因此我必须假定问题出在那。我有办法在代码中更正此错误吗?有防止这种情况的前景设置吗?我的大多数同事都使用Windows,所以我需要一点帮助。

解决方法

我没有Outlook,因此无法重现该问题。

尝试添加引号和带有宽度和高度属性的px,如下所示:

send_email('''<img src="cid:{}" width="{}px" height="{}px">'''.format(final_image_name,width_of_new_image,height_of_new_image),'Email with body image',recipients,attachments=['/tmp/{}'.format(final_image_name),])

也许Outlook正在缩小图像以使其适合窗口?

尝试将img标签包裹在div中,并强制最小宽度/高度。

html_str = f'''<div style="width: 100%; min-width: {width_of_new_image}px; height: 100%; min-height: {height_of_new_image}px;"><img src="cid:{final_image_name}" width="{width_of_new_image}px" height="{height_of_new_image}px"></div>'''
send_email(html_str,])

上面的代码应产生如下内容:

<div style="width: 100%; min-width: 800px; height: 100%; min-height: 600px;"><img src="cid:imagename" width="800px" height="600px"></div>