首页 > 解决方案 > 使用 PHPMailer 发送的电子邮件会忽略 CSS 样式

问题描述

我正在使用 PHPMailer (SMTP) 发送一封简单的电子邮件。在这里,我遇到了 CSS 格式的问题。一般来说,像颜色变化这样的事情是有效的。但是其他诸如更改字体大小之类的事情不起作用。电子邮件客户端忽略了我的风格。我正在使用火花。但我不认为问题出在客户身上。我还收到其他 HTML 电子邮件。

这是我的代码:

$email_body = "
    <html>
        <head>
            <style>
                span {
                font-family: Verdana, Geneva, sans-serif;
                font-size:12px;
                color:#FF0000;
                }
                h2 {font-size: 50px}
            </style>
        </head>
        <body>
            <div>
                Hi there,<br><br>
                this is just a test e-mail.
                <br><br>
                <h2 style='color: darkblue;font-size:50px'>This is a headline</h2><br><br>
                <span>This is just a text</span>
            </div>
        </body>
    </html>
    ";

try {
    //Server settings
    $mail->isSMTP();
    ..... 
    $mail->Port = 587;

    //Recipients
    $mail->setFrom('email@email.de', 'John Doe');
    $mail->addAddress('email@email.de', 'John Doe'); //Add a recipient
    $mail->addReplyTo('noreply@email.de', 'Information');
    $mail->CharSet = 'UTF-8';

    //Attachments
    $path = '../tempfiles/';
    $files = preg_grep('/^([^.])/', scandir($path));
    foreach ($files as $f) {
        if (in_array($f, $file_names_arr)) {
            $mail->addAttachment("../tempfiles/" . $f);
        }
    }

    //Content
    $mail->isHTML(true);
    $mail->Subject = 'Email Subject';
    $mail->Body = $email_body;
    $mail->AltBody = "Test";

    $mail->send();

    // Delete images afterwards
    $path = '../tempfiles/';
    $files = preg_grep('/^([^.])/', scandir($path));
    foreach ($files as $f) {
        if (in_array($f, $file_names_arr)) {
            if (is_file("../tempfiles/" . $f)) {
                unlink("../tempfiles/" . $f);
            }
        }
    }
    echo json_encode(1);
} catch (Exception $e) {
    echo json_encode(0);
}

所有排版元素,如 h2、h3、p、span 都具有相同的字体大小,我无法更改。这里可能是什么问题?

标签: phpsmtpphpmailer

解决方案


推荐阅读