简体   繁体   中英

CakePHP email with CSS

I am unable to get the css in email, Following is the CakePHP email function:

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();

}

Email Template:

<!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>

Everything works fine except the CSS. I even tried to include <style>....</style> in the head section but didn't work.

您可以在模板中内联编写CSS:

 <h2 style="color:#818381">Here are the latest stories everyone's talking about...</h2>
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

brother of code

My name is john, I recommended use https://github.com/tijsverkoyen/CssToInlineStyles .

It's one component for html and css separated file insert css into style balise inline for all element compose html.

Warning, I use in my project and frequent problem when use with multiple htmls files.

I created one patch for problem, contact me for retreive patch.

If you use a css file, it should be in /app/webroot/css with the extension style.css and then you should include the following line in your template file:

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

for the view file:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM