【问题标题】:CakePHP email with CSS带有 CSS 的 CakePHP 电子邮件
【发布时间】:2014-07-15 03:38:58
【问题描述】:

我无法获取电子邮件中的 css,以下是 CakePHP 电子邮件功能:

function _sendMailUser($mailTo = array(), $mailFrom = array(), $repTo = array(), $mailSubject = null, $mailTemplate = null) {
    App::uses('CakeEmail', 'Network/Email');
    $email = new CakeEmail();
    $email -> from($mailFrom);
    $email -> bcc($mailTo);
    $email -> replyTo($repTo);
    $email -> subject($mailSubject);
    $email -> template($mailTemplate);
    $email -> emailFormat('html');

    return $email -> send();

}

电子邮件模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="<?php echo HTTP_ROOT; ?>css/style.css" type="text/css" />

</head>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">

    <center>
        <?php echo $this -> fetch('content'); ?>
    </center>

</body>

除了 CSS 之外,一切正常。我什至尝试在头部包含&lt;style&gt;....&lt;/style&gt;,但没有成功。

【问题讨论】:

  • 显示你的css代码。并且不要在电子邮件上链接 css 样式。您向哪个电子邮件服务提供商发送电子邮件?
  • 没有得到 css,尝试了多个电子邮件提供商 Gmail、Yahoo 等...
  • 我使用this 将内联样式和 CSS 转换为“内联 CSS”,这是最有效的 CSS 方法。这可以通过帮助回调发生,例如在实际将其传递给 Transport 类之前。
  • 你应该使用内联样式。并非每个邮件客户端都支持标题和/或正文中的样式标签。
  • 把完整的 URI 放到你的 css 文件:)

标签: php css email cakephp


【解决方案1】:
function _sendNewUserMail($emaildetail = null)
    {
        $this->Email->to = $emaildetail['to'];
        $this->Email->bcc = array('er.ahmad.hassan@gmail.com');
        //$this->Email->bcc = '';
        $this->Email->subject = $emaildetail['subject'];
        $this->Email->replyTo = 'XXX@gmail.com';
        $this->Email->from = 'XYZ@apyl.com';
        $this->Email->template = $emaildetail['messagebody'];
        $this->Email->sendAs = 'html';
        $this->Email->__header[] = 'MIME-Version: 1.0';
        $this->Email->smtpOptions = array('port' => '25', 'timeout' => '30', 'host' =>     "000.0.000.000",
        'username' => "xyz@example.com", 'password' => 'XXX', 'client' =>    'smtp_helo_hostname');
        $this->Email->delivery = 'smtp';
        $this->set('emaildetail', $emaildetail);
        $this->Email->send();
        $this->set('smtp-errors', $this->Email->smtpError);
        return true;
}

Mail Template Here

【讨论】:

    【解决方案2】:

    代码兄弟

    我的名字是约翰,我推荐使用https://github.com/tijsverkoyen/CssToInlineStyles

    它是 html 和 css 分隔文件的一个组件,可将 css 插入样式 balise inline 以供所有元素组成 html。

    警告,我在我的项目中使用,并且在使用多个 htmls 文件时经常出现问题。

    我为问题创建了一个补丁,请与我联系以获取补丁。

    【讨论】:

    【解决方案3】:

    如果您使用 css 文件,它应该在 /app/webroot/css 中,扩展名为 style.css,然后您应该在模板文件中包含以下行:

    <?php echo $this->Html->css(array('style')); ?>
    

    对于视图文件:

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
            <?php echo $this->Html->css(array('style')); ?>
        </head>
        <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
    
            <center>
                <?php echo $this -> fetch('content'); ?>
            </center>
    
        </body>
    

    【讨论】:

      【解决方案4】:

      你可以在模板中写css inline:

       <h2 style="color:#818381">Here are the latest stories everyone's talking about...</h2>
      

      【讨论】:

        猜你喜欢
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-08
        • 1970-01-01
        • 2011-05-08
        相关资源
        最近更新 更多