我正try 使用Win32库通过Django发送邮箱,但没有应用HTML样式.头部的风格是零星的,身体的风格也是零星的

    pythoncom.CoInitialize()
   
    dynamic_link='prueba'

    try:
        # Obtén todos los ingenieros con sus nombres concatenados
        outlook = win32.Dispatch('Outlook.Application')
        mail = outlook.CreateItem(0)

        # Configurar el remitente
        mail.SentOnBehalfOfName = 'example@outlook.com'

        mail.To = adminEmail
        mail.Subject = 'NUEVA SOLICITUD DE PRUEBAS DE LABORATORIO'
        html_body = f"""
            <html>
            <head>
                <style>
                    body {{
                        font-family: Arial, sans-serif;
                        padding: 20px;
                    }}
                    h1, p {{
                        color: #333;
                    }}
                    .background-red {{
                        background-color: red;
                    }}
                    #button {{
                        display: inline-block;
                        padding: 10px 20px;
                        background-color: #4CAF50;
                        color: #fff;
                        text-decoration: none;
                        border-radius: 5px;
                    }}
                </style>
            </head>
            <body class="background-red">
                <h1>Solicitud de pruebas de laboratorio</h1>
                <p>Nueva solicitud pruebas de laboratorio del usuario {FullName}</p>

                <div style="width: 130px; height:130px; background-color:white;">
                    <p>El usuario {FullName} ha creado una nueva solicitud de pruebas de laboratorio para el cliente {customer} con una fecha requerida para el {require_date}</p>
                </div>
                <a href="{dynamic_link}" id="button">Ir a la página</a>
            </body>
            </html>
            """
        mail.HTMLBody = html_body
        mail.Send()
        return Response({"message": "Correo enviado correctamente"}, status=status.HTTP_200_OK)
    except Exception as e:
         print(f"Error: {e}")

    finally:
        # Liberar recursos
        pythoncom.CoUninitialize()

推荐答案

由于不同的平台或邮箱应用程序呈现邮箱模板的方式不同,邮箱模板的处理可能会相当棘手.您遇到的问题是在HTML邮箱模板中非常常见的问题,在这些模板中,标签中定义的样式没有得到一致的apply.许多邮箱客户端,包括Outlook,对<head><style>标签中定义的样式的支持有限或不一致.

设置HTML邮箱样式的最可靠方法将是尽可能使用内联css,或者如果特定部分需要自定义按钮或一些现代样式,则使用图像.

内联样式具有最高的优先级,通常在大多数邮箱客户端上都得到很好的支持.通过使用内联CSS,您可以显著增加您的邮箱在各种平台上按预期呈现的可能性.你可以通过访问"The Ultimate Guide to CSS for Email clients"来查看支持哪些HTML标签和CSS样式

在您的情况下,您可以像这样修改html_body,其中我应用了所有必要的CSS作为内联样式(根据需要进行更改):

html_body = f"""
    <html>
    <body style="font-family: Arial, sans-serif; padding: 20px; background-color: red;">
        <h1 style="color: #333;">Solicitud de pruebas de laboratorio</h1>
        <p style="color: #333;">Nueva solicitud pruebas de laboratorio del usuario {FullName}</p>

        <div style="width: 130px; height: 130px; background-color: white; color: #333;">
            <p>El usuario {FullName} ha creado una nueva solicitud de pruebas de laboratorio para el cliente {customer} con una fecha requerida para el {require_date}</p>
        </div>
        <a href="{dynamic_link}" style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: #fff; text-decoration: none; border-radius: 5px;">Ir a la página</a>
    </body>
    </html>
"""

Python相关问答推荐

想要使用Polars groupby_Dynamic来缩减时间序列收件箱(包括空垃圾箱)

PyQt5如何将pyuic 5生成的Python类添加到QStackedWidget中?

创建带有二维码的Flask应用程序,可重定向到特定端点

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

Python中的函数中是否有充分的理由接受float而不接受int?

如何通过多2多字段过滤查询集

如何使用SubProcess/Shell从Python脚本中调用具有几个带有html标签的参数的Perl脚本?

Python:在类对象内的字典中更改所有键的索引,而不是仅更改一个键

@Property方法上的inspect.getmembers出现意外行为,引发异常

滚动和,句号来自Pandas列

类型错误:输入类型不支持ufuncisnan-在执行Mann-Whitney U测试时[SOLVED]

_repr_html_实现自定义__getattr_时未显示

将两只Pandas rame乘以指数

NP.round解算数据后NP.unique

把一个pandas文件夹从juyter笔记本放到堆栈溢出问题中的最快方法?

如何在Python中找到线性依赖mod 2

多指标不同顺序串联大Pandas 模型

什么是最好的方法来切割一个相框到一个面具的第一个实例?

Polars将相同的自定义函数应用于组中的多个列,

在代码执行后关闭ChromeDriver窗口