简体   繁体   English

CakePHP:在 email header 中添加“列表取消订阅”,使用 ZCE8AE9DA5B7CD6C3DF2929543A9 组件

[英]CakePHP : Add “List-Unsubscribe” in email header using Email component

I am usign cakephp 2.x and using Email components to send email.我使用 cakephp 2.x 并使用 Email 组件发送 email。

I am trying to add List-Unsubscribe at headers.我正在尝试在标题处添加 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' 
];

Now in email source its showing in X-header.现在在 email 源中,它在 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

But it's not showing Unsubscribe link with From.但它没有显示与 From 的取消订阅链接。

I need List-Unsubscribe in message header to avoid spam email.我需要在消息 header 中取消订阅,以避免垃圾邮件 email。

When i try to use $this->Email->additionalParams its not showing List-Unsubscribe in email header.当我尝试使用$this->Email->additionalParams ,它没有在 email header 中显示列表取消订阅。

Here is code that i am using这是我正在使用的代码

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

The email component doesn't support custom non X-prefixed headers, if you need that you'll have to use CakeEmail , which by default doesn't prefix headers, and requires you to explicitly pass prefixed headers in case required. email 组件不支持自定义的非 X 前缀标头,如果您需要,您必须使用CakeEmail ,默认情况下不为标头添加前缀,并要求您在需要时显式传递带前缀的标头。

Given that the email component is long deprecated , ever since the first release of CakePHP 2 to be specific, now is probably a good time for you to finally drop it.鉴于email 组件已被长期弃用,自从 CakePHP 2 的第一个版本具体发布以来,现在可能是您最终放弃它的好时机。

Quick and dirty example:快速而肮脏的例子:

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

This would require two templates, one for HTML in app/View/Emails/html/subscribe.ctp :这将需要两个模板,一个用于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;

and one for text in app/View/Emails/text/subscribe.ctp , requiring you to move the HTML to text conversion into the template.一个用于app/View/Emails/text/subscribe.ctp中的文本,需要您将 HTML 移动到文本转换到模板中。 You should probably make it a helper , the following should just illustrate the principle:您可能应该将其设为 helper ,以下仅说明原理:

<?php
echo _html_to_text($content);

See also也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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