【问题标题】:CakePHP remove HTML from plain text emailCakePHP 从纯文本电子邮件中删除 HTML
【发布时间】:2012-11-18 12:06:18
【问题描述】:

当在我的应用中添加新文章时,我正在使用我的 cakephp 应用 (2.2) 中的电子邮件组件向订阅的人发送电子邮件。

我使用 TinyMCE 允许管理员用户格式化文本,这会导致一些格式化 HTML 保存在数据库中(这很好)但是我想以电子邮件格式(html 和纯文本)通过电子邮件将整篇文章发送给选择加入的用户添加新文章时。这适用于 html 版本,但是如何从纯文本版本中剥离 html,同时将其保留在 html 版本中?到目前为止,这是我的代码:

public function admin_add() {
    if ($this->request->is('post')) {
        $this->NewsArticle->create();
        if ($this->NewsArticle->save($this->request->data)) {

            // If is a tech alart - send email
            if ($this->request->data['NewsCategory']['NewsCategory']['0'] == '2') {

                // Email users who have opted in to webform updates
                $usersToEmail = $this->NewsArticle->query("SELECT username, tech_email FROM users WHERE tech_email = 1");

                // Loop throughh all opt'ed in users
                foreach ($usersToEmail as $user) {

                    $this->Email->template = 'newTechAlert';
                    $this->Email->from    = 'Client Area <clientarea@someurl.co.uk>';
                    $this->Email->to      = $user['users']['username'];
                    $this->Email->subject = 'New Technical Alert';
                    // Send as both HTML and Text
                                            $this->Email->sendAs = 'both';

                    // Set vars for email
                    $this->set('techAlertTitle', $this->request->data['NewsArticle']['title']);

                    ##  NEED TO STRIP THE HTML OUT FOR NONE HTML EMAILS HERE - BUT HOW???
                    $this->set('techAlertBody', $this->request->data['NewsArticle']['body']);


                    $this->set('user', $user['users']['username']);
                    $this->Email->send();

                }

            }

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    我用:

    $this->Email->emailFormat('both');
    
    // Convert <br> to \n
    $text = preg_replace('/<br(\s+)?\/?>/i', "\n", $html);
    // Remove html markup
    $text = trim(strip_tags($text));
    // Replace multiple (one ore more) line breaks with a single one.
    $text = preg_replace("/(\r\n|\r|\n)+/", "\n", $text);
    
    $this->Email->viewVars(compact('text', 'html'));
    

    请注意,如果您使用 foreach,则应在每次运行后重置电子邮件类以避免出现问题:

    $this->Email->reset();
    

    【讨论】:

    • 您还可以将两个以上的换行符替换为两个换行符(如果您想保留段落但由于 html 标记仍然过滤 3-xxx 换行符:$text = preg_replace("/(\r\n|\r|\n)+(\r\n|\r|\n)+/", "\n\n", $text);
    【解决方案2】:

    你可以使用phpstrip_tags方法 http://php.net/manual/en/function.strip-tags.php

    //HTML VERSION
    $this->set('techAlertHtmlBody', $this->request->data['NewsArticle']['body']);
    
    //PLAIN TEXT VERSION
    $this->set('techAlertPlainBody', strip_tags($this->request->data['NewsArticle']['body']));
    

    您还可以将第二个参数传递给函数以仍然允许换行符或 href 标记。

    【讨论】:

    • 啊,当然很简单。我想我今天早上需要更多的咖啡!谢谢
    • 哈哈,第二杯来了。这就是我得到答案的原因。
    • 澄清:仅删除 HTML 标记可能会破坏基本格式(段落和换行符)。例如:&lt;p&gt;Foo&lt;/p&gt;&lt;p&gt;Bar&lt;/p&gt; -> FooBar.
    • 这就是为什么我提到了 strip_tags 函数的第二个参数
    • 但是&lt;p&gt;&lt;br&gt; 在纯文本中是没有意义的:-?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多