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 your-name@gmail.com), you can see an annoying 'mailed-by' header. It shows the name of your server, like mailed-by: dreamhost.com. 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 = '-fnoreply@yourdomain.com';
/* 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);
}


Done.

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

$this->Email->additionalParams = '-fnoreply@yourdomain.com';

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

That's it.

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

Take care.

Responses awaited.

3 comments:

rooter said...

This was very useful.

Thanks so much!

rooter said...

This was very helpful.
Thank you so much!

Siddhartha said...

Thanks.