Search code examples
emailcakephp-2.x

CakePHP Email changing from address on SMTP


I have this email config to send via SMTP through our Google Apps Business Account.

class EmailConfig {
    public $default = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => '[email protected]',
        'password' => 'secret_password',
        'transport' => 'Smtp'
    );
}

When I send an email doing this:

$email = new CakeEmail('default');
$email->from('[email protected]', 'My App');
$email->to(array('[email protected]' => 'Recipient Name'));
$email->subject('Test Email');
$email->emailFormat('html');
$email->send();

The email gets delivered, but it sends the mail from: [email protected] and not [email protected].

Is there another setting that I need to use or is this not possible?

EDIT:

I've tried the solution provided in the comment below, however it still doesn't make the email come from no-reply


Solution

  • When using Google's SMTP server, the from header is ignored.

    $email->from('[email protected]', 'My App'); //Ignored by Gmail
    

    From DigitalOcean's tutorial How to use Google's SMTP server:

    NOTE: Google automatically rewrites the From line of any email you send via its SMTP server to the default Send mail as email address in your Gmail or Google Apps email account Settings. You need to be aware of this nuance because it affects the presentation of your email, from the point of view of the recepient, and it may also affect the Reply-To setting of some programs.

    For this to work, you have to modify your mail settings in your Google Apps control panel:

    Workaround: In your Google email Settings, go to the Accounts tab/section and make "default" an account other than your Gmail/Google Apps account. This will cause Google's SMTP server to re-write the From field with whatever address you enabled as the default Send mail as address.