首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP :使用邮件组件在邮件头添加“List-Unsubscribe”

CakePHP :使用邮件组件在邮件头添加“List-Unsubscribe”
EN

Stack Overflow用户
提问于 2020-07-16 21:27:18
回答 1查看 199关注 0票数 0

我是usign cakephp 2.x和使用电子邮件组件发送电子邮件。

我正在尝试在标题中添加列表-取消订阅。

代码语言:javascript
复制
$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中。

代码语言:javascript
复制
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

但它没有显示取消订阅链接与来自。

我需要在邮件头列表-取消订阅,以避免垃圾邮件。

当我尝试使用$this->Email->additionalParams时,它不会在电子邮件标题中显示列表-取消订阅。

下面是我使用的代码

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

发布于 2020-07-16 23:00:51

电子邮件组件不支持自定义的非X前缀的报头,如果你需要的话,你将不得不使用CakeEmail,它在默认情况下不会给报头加前缀,并要求你在需要的情况下显式地传递前缀的报头。

鉴于,从CakePHP 2的第一个发行版开始,现在可能是您最终放弃它的好时机。

简单明了的例子:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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 to text转换到模板中。你可能应该使用,下面的代码只是说明了这个原理:

代码语言:javascript
复制
<?php
echo _html_to_text($content);

另请参阅

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62935827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档