【问题标题】:CakePHP : Add “List-Unsubscribe” in email header using Email componentCakePHP:使用电子邮件组件在电子邮件标题中添加“列表退订”
【发布时间】:2023-03-30 08:14:02
【问题描述】:

我正在使用 cakephp 2.x 并使用电子邮件组件发送电子邮件。

我正在尝试在标题处添加 List-Unsubscribe。

$this->Email->headers = [
'List-Unsubscribe'=>'<mailto:'.$email_from.'?subject=Remove from Mailing List>, <'.$SITEURL.'unsubscribe?em='.$email_to.'>',
'List-Unsubscribe-Post'=>'List-Unsubscribe=One-Click' 
];

现在在电子邮件源中,它显示在 X-header 中。

X-List-Unsubscribe: <mailto:clients@example.com?subject=Remove from Mailing List>, <http://example.com/unsubscribe?em=xyz@example.com>
X-List-Unsubscribe-Post: List-Unsubscribe=One-Click

但它没有显示带有 From 的取消订阅链接。

我需要在邮件标题中列出取消订阅以避免垃圾邮件。

当我尝试使用 $this-&gt;Email-&gt;additionalParams 时,它没有在电子邮件标题中显示 List-Unsubscribe。

这是我正在使用的代码

$send_from = $email_from_name . "<" . $email_from . ">";
$this->Email->sendAs = 'both'; // text / html / both
$this->Email->from = $send_from; 
$this->Email->replyTo = $email_from;
$this->Email->return = $email_from; 
$this->Email->to = $email_to;
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = ['host'=>$imap_server,'port'=>587,'username'=>$email,'password'=>$password];
$this->Email->subject = $subject;

$this->Email->headers = [
'List-Unsubscribe'=>'<mailto:'.$email_from.'?subject=Remove from Mailing List>, <'.SITEURL.'unsubscribe?em='.$email_to.'>',
'List-Unsubscribe-Post'=>'List-Unsubscribe=One-Click' ];
$this->Email->textMessage = $this->_html_to_text($content);
//$this->Email->delivery = 'debug';
$this->Email->send($content);

【问题讨论】:

    标签: php email cakephp


    【解决方案1】:

    电子邮件组件不支持自定义的非 X 前缀标头,如果需要,您必须使用 CakeEmail,默认情况下不使用前缀标头,并要求您显式传递前缀标头以防万一必填。

    鉴于the email component is long deprecated,自从第一次发布 CakePHP 2 以来,现在可能是你最终放弃它的好时机。

    快速而肮脏的例子:

    App::uses('CakeEmail', 'Network/Email');
    
    // The transport/connection configuration should probably better be moved into a config file
    $Email = new CakeEmail(array(
        'transport' => 'Smtp',
        'host' => $imap_server,
        'port' => 587,
        'username' => $email,
        'password' => $password
    ));
    $Email
        ->emailFormat('both')
        ->template('subscribe')
        ->from($send_from)
        ->replyTo($email_from)
        ->returnPath($email_from)
        ->to($email_to)
        ->subject($subject)
        ->addHeaders(array(
            'List-Unsubscribe' =>
                '<mailto:' . $email_from . '?subject=Remove from Mailing List>, ' .
                '<' . $SITEURL . 'unsubscribe?em=' . $email_to . '>',
            'List-Unsubscribe-Post' => 'List-Unsubscribe=One-Click'
        ))
        ->send($content);
    

    这需要两个模板,一个用于app/View/Emails/html/subscribe.ctp 中的 HTML:

    <?php
    // $content contains the data passed to the `send()` method, wrapped to max 998 chars per line
    echo $content;
    

    还有一个用于app/View/Emails/text/subscribe.ctp 中的文本,要求您将 HTML 到文本的转换移动到模板中。你应该make it a helper,下面应该只是说明原理:

    <?php
    echo _html_to_text($content);
    

    另见

    【讨论】:

    • 我将尝试用 CakeMail 替换,但我仍然编辑了电子邮件组件,然后在电子邮件源显示 List-Unsubscribe: , 但仍然没有显示取消订阅链接。
    • @YogeshSaroya 这听起来像是一个新问题/问题,甚至可能与 CakePHP 无关。如果您现在可以生成正确的标头,您可能应该打开一个新问题并详细说明应该评估标头的目标环境(以及它将如何进行),正在生成的确切电子邮件标头,以及使用这些退订标头调查与工作电子邮件的差异。
    猜你喜欢
    • 2011-05-08
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2012-12-05
    • 1970-01-01
    • 2011-01-02
    相关资源
    最近更新 更多