Friday, February 5, 2010

How to Remove Mailed-by Header in CakePHP Email Component

It took me quite sometime to fix this issue.

You can send emails with built-in CakePHP Email Component. But if you are sending email to any gmail address (like, you can see an annoying 'mailed-by' header. It shows the name of your server, like mailed-by: It discloses your webhost. You may not like it. You may want to remove/hide this information. To do so, you need only one line of CakePHP code.

I have used following sendEmail() function in my /app/app_controller.php

file:// /app/app_controller.php

function sendEmail($subject, $view, $to=null, $from = null, $fromName = null) {
/* This function will be used to send email in my CakePHP application */
$this->Email->to = $to;
$this->Email->subject = env('SERVER_NAME') . ' – ' . $subject;
$this->Email->from = $fromName.' <'. $from.'>';
$this->Email->template = 'default';
$this->Email->additionalParams = '';
/* the above line is required to remove 'mailed-by' header' */ 
$this->Email->sendAs = 'both';   // you probably want to use both :)
return $this->Email->send($view);


Note again, whenever you use CakePHP Email Component, just add one extra line:

$this->Email->additionalParams = '';

And you can safely remove that annoying 'mailed-by' header.

That's it.

Just a guess, is NOT essential. You may change it to anything you want.

Take care.

Responses awaited.


rooter said...

This was very useful.

Thanks so much!

rooter said...

This was very helpful.
Thank you so much!

Siddhartha said...