How to rewrite or set the Return-Path in cakePHP Email Component?

折月煮酒 提交于 2019-12-05 02:03:30
Travis Leleu

There's an attribute called EmailComponent::return that is the return path for error messages. Note that this is different than the replyTo attribute.

$this->Email->return = 'name@example.com';

http://book.cakephp.org/1.3/en/The-Manual/Core-Components/Email.html

In CakePHP 2 (where the Email Component is largely replaced by the CakeEmail class), you can do this configuration inside /app/Config/email.php:

class EmailConfig {
    public $email = array(
        ...
        // The next line attempts to create a 'Return-path' header
        'returnPath' => 'myaddress@mydomain.com',

        // But in some sendmail configurations (esp. on cPanel)
        // you have to pass the -f parameter to sendmail, like this
        'additionalParameters' => '-fmyaddress@mydomain.com',
        ...
    );
}

Or if you need to do it just for a single email, something like this should work...

App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('MyConfig');
$email->from(...)
      ->to(...)
      ->subject(...)
      ->returnPath('myaddress@mydomain.com')
      // Haven't tested this next line, but may possibly work?
      ->config(array('additionalParameters' => '-fmyaddress@mydomain.com'))
      ->send();

A co-worker and I were working on this same issue, we found that editing the following line in php.ini gave us our fix:

from:

sendmail_path = /usr/sbin/sendmail -t -i

to:

sendmail_path = /usr/sbin/sendmail -t -i -f youremail@address

when testing be sure to send your emails to a valid domain. this caught us for a few minutes.

tadasZ

To change the return path in CakePHP Email component I do like this:

...
$return_path_email = 'return@email.com';
...

$this->Email->additionalParams = '-f'.$return_path_email;

and it works like charm ;)

Digging into the cake manual when you were looking at how to use the rest of the component you should have seen something like the following. This is what set the Return-Path.

$this->Email->return = 'name@tld.com';

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!