You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lambda返回图片在Windows Outlook无法显示,求代码调整方案

解决Windows版Outlook无法显示Lambda返回图片的问题

Windows版Outlook对嵌入邮件的图片资源有更严格的HTTP响应要求,你需要修改Lambda代码的以下几个部分:

1. 使用标准MIME类型

将mimetype = 'image/jpg'改为mimetype = 'image/jpeg'——虽然image/jpg是常见别名,但Outlook的Windows版本只严格识别标准的image/jpeg类型。

2. 添加必需的响应头

Windows Outlook需要明确的Content-Length和缓存控制头才能正常加载图片,建议放弃send_file的默认返回,改用Flask的Response对象手动控制所有响应头:

  • Content-Length:告诉Outlook图片文件的大小
  • Cache-Control:设置合理的缓存策略,避免Outlook因"无缓存标记"拦截图片

修改后的完整代码

from PIL import Image
from io import BytesIO
from flask import Flask, request, Response

app = Flask(__name__)

@app.route('/test')
def test():
    img = Image.new('RGB', (100, 100))
    imgByteArr = BytesIO()
    format = 'JPEG'
    mimetype = 'image/jpeg'
    img.save(imgByteArr, format=format, quality=90)
    imgByteArr.seek(0)
    
    # 获取图片字节长度
    img_length = imgByteArr.getbuffer().nbytes
    # 构建响应对象,手动设置所有必要头
    return Response(
        imgByteArr.getvalue(),
        mimetype=mimetype,
        headers={
            'Content-Length': str(img_length),
            'Cache-Control': 'public, max-age=3600',
            'Content-Disposition': 'inline; filename="image.jpg"'
        }
    )

额外说明

  • Content-Disposition: inline明确告诉客户端图片是嵌入显示而非下载,适配邮件图片的使用场景
  • 确保Lambda的API Gateway配置中没有修改或移除这些自定义响应头,比如不要开启"缩减响应头"之类的选项

内容的提问来源于stack exchange,提问作者user1322801

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 04:35:29