How to rewrite or set the Return-Path in cakePHP E

2019-04-22 12:16发布

I'm using the cakePHP email component for sending mails from my application. Now the return-path has something like www@domain.tld

How can I set or rewrite the Return-Path value in emails when using the cakePHP Component?

I know how to do it when sending mails via 'mail' in PHP but the cakePHP email component seems to missing such a feature... or am I missing something? :)

5条回答
Fickle 薄情
2楼-- · 2019-04-22 12:29

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

查看更多
3楼-- · 2019-04-22 12:35

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

查看更多
疯言疯语
4楼-- · 2019-04-22 12:36

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

查看更多
Animai°情兽
5楼-- · 2019-04-22 12:41

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();
查看更多
唯我独甜
6楼-- · 2019-04-22 12:42

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.

查看更多
登录 后发表回答