“如何使用 win32 在 Django 中向 html_body 添加样式?”

问题描述 投票:0回答:1

我尝试使用 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 = '[email protected]'

        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()
python django winapi win32com
1个回答
0
投票

由于不同平台或电子邮件应用程序呈现电子邮件模板的方式不同,因此处理电子邮件模板可能非常棘手。您遇到的问题是 HTML 电子邮件模板中的一个非常常见的问题,其中标记中定义的样式应用不一致。许多电子邮件客户端(包括 Outlook)对

<head>
<style>
标签中定义的样式的支持有限或不一致。

设置 HTML 电子邮件样式的最可靠方法是尽可能使用内联 CSS,或者如果特定部分需要自定义按钮或某些现代样式,则使用图像。

内联样式具有最高优先级,并且通常在大多数电子邮件客户端中得到良好支持。通过使用内联 CSS,您可以显着提高电子邮件在各种平台上按预期呈现的可能性。您可以访问“电子邮件客户端 CSS 终极指南”来检查支持哪些 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>
"""
© www.soinside.com 2019 - 2024. All rights reserved.