php - CakePHP : Add “List-Unsubscribe” in email header using Email component
Get the solution ↓↓↓I am usign cakephp 2.x and using Email components to send email.
I am trying to add List-Unsubscribe at headers.
$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.
X-List-Unsubscribe: <mailto:[email protected]?subject=Remove from Mailing List>, <http://example.com/[email protected]>
X-List-Unsubscribe-Post: List-Unsubscribe=One-Click
But it's not showing Unsubscribe link with From.
I need List-Unsubscribe in message header to avoid spam email.
When i try to use$this->Email->additionalParams
its not showing List-Unsubscribe in 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);
Answer
Solution:
The email component doesn't support custom non X-prefixed headers, if you need that you'll have to useCakeEmail
, which by default doesn't prefix headers, and requires you to explicitly pass prefixed headers in case required.
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 inapp/View/Emails/html/subscribe.ctp
:
<?php
// $content contains the data passed to the `send()` method, wrapped to max 998 chars per line
echo $content;
and one for text inapp/View/Emails/text/subscribe.ctp
, requiring you to move the HTML to text conversion into the template. You should probably , the following should just illustrate the principle:
<?php
echo _html_to_text($content);
See also
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.