我使用一个模板使用Django发送电子邮件。电子邮件被发送,但图像不显示。
在模板html文件中,我有:
{% load static %}
<img class="margin-bottom" height="52px" width="140px" src="{% static 'images/image.png' %}" />
在settings.py文件中,我有以下内容:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_URL = env('STATIC_URL', cast=str, default='/static/')
STATIC_ROOT = env(
'STATIC_ROOT', cast=str, default=os.path.join(BASE_DIR, "collected_static")
)
我把我的图像放在一个叫做静态文件夹的文件夹中。
发布于 2021-04-25 18:50:38
您必须使用MultiPart和cid:。发送带有图像的html邮件几乎总是一个坏主意。它将垃圾邮件指向您的邮件和smtp服务器。
尝尝这个
from email.mime.image import MIMEImage
from django.core.mail import EmailMultiAlternatives
subject = 'Django sending email'
body_html = '''
<html>
<body>
<img src="cid:logo.png" />
<img src="https://www.google.com/logos/doodles/2015/googles-new-logo-5078286822539264.3-hp2x.gif" />
</body>
</html>
'''
from_email = 'hello@localhost.com'
to_email = 'hi@localhost.com'
msg = EmailMultiAlternatives(
subject,
body_html,
from_email=from_email,
to=[to_email]
)
msg.mixed_subtype = 'related'
msg.attach_alternative(body_html, "text/html")
img_dir = 'static'
image = 'logo.png
file_path = os.path.join(img_dir, image)
with open(file_path, 'r') as f:
img = MIMEImage(f.read())
img.add_header('Content-ID', '<{name}>'.format(name=image))
img.add_header('Content-Disposition', 'inline', filename=image)
msg.attach(img)
发布于 2022-08-08 15:32:28
所发生的是gmail有它最大的电子邮件大小,或者让我说,如果您试图加载的图像非常高,它不会显示它在ios邮件应用程序,但它不会显示在其他一些,所以你必须使图像大小低,但我会建议你使用https://imgur.com/,你上传你的图像,然后你可以把它加载到html。例如,您可以检查这一点,它可以工作,我目前也使用540 it映像。
<img style="width: 150px;object-fit: contain;color: #ff3838;border: 0;-ms-interpolation-mode: bicubic;"
alt="Gimsap"
src="https://i.imgur.com/xVWEwWg.png">
https://stackoverflow.com/questions/67256407
复制相似问题