Python Outlook on windows可调整电子邮件中图像的大小
我有以下发送电子邮件的功能:Python Outlook on windows可调整电子邮件中图像的大小,python,python-3.x,email,outlook,amazon-ses,Python,Python 3.x,Email,Outlook,Amazon Ses,我有以下发送电子邮件的功能: 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-e
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', recipients,
attachments=['/tmp/{}'.format(final_image_name), ])
<div style="width: 100%; min-width: 800px; height: 100%; min-height: 600px;"><img src="cid:imagename" width="800px" height="600px"></div>
发送电子邮件(格式)(最终图像名称、,
新图像的宽度,
高度(新图像的高度),
“带有身体图像的电子邮件”,收件人,
附件=['/tmp/{}.格式(最终图像名称),])
它是有效的。在iPad、iPhone、Mail应用程序和Outlook for Mac中打开电子邮件时,一切都非常完美。然而。。。当我的同事在Windows中的Outlook中打开它们时,一切都变得一团糟。有些人看到非常微小的图像,有些人得到扭曲的图像,有些人必须制作250%的图像,有些人才能看到可读大小的电子邮件。因为除了Windows上的Outlook之外,其他所有功能都可以使用,所以我不得不假设问题就在那里。我有没有办法在代码中更正这个问题?是否有outlook设置来防止这种情况?我的大多数同事都使用Windows,因此我需要一些帮助。我没有Outlook,因此无法重现问题 尝试使用宽度和高度属性添加引号和
px
,如下所示:
发送电子邮件(格式)(最终图像名称、,
新图像的宽度,
高度(新图像的高度),
“带有身体图像的电子邮件”,收件人,
附件=['/tmp/{}.格式(最终图像名称),])
也许Outlook正在缩小图像以使其适合窗口
尝试将img
标记包装在div
中,并强制设置最小宽度/高度
html\u str=f''
发送电子邮件(html_str,“带正文图像的电子邮件”,收件人,附件=['/tmp/{}'。格式(最终图像名称),])
上述代码应生成如下内容:
send_email('''<img src="cid:{}" width={} height={}>'''.format(final_image_name,
width_of_new_image,
height_of_new_image),
'Email with body image', recipients,
attachments=['/tmp/{}'.format(final_image_name), ])
<div style="width: 100%; min-width: 800px; height: 100%; min-height: 600px;"><img src="cid:imagename" width="800px" height="600px"></div>