CakePHP-2.0: How can i send email from a gmail account using CakEmail and SMTP setting?

22,817

Solution 1

From the docs:

You can configure SSL SMTP servers, like GMail. To do so, put the 'ssl://' at prefix in the host and configure the port value accordingly. Example:

<?php
class EmailConfig {
    public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => '[email protected]',
        'password' => 'secret'
    );
}

http://book.cakephp.org/2.0/en/core-utility-libraries/email.html?highlight=cakeemail#CakeEmail

Solution 2

The right configuration is:

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

So, don't forget the transport element.

Solution 3

Just set the from:

<?php
$email = new CakeEmail();
$email->from(array('[email protected]' => 'Your Name'));
$email->to('[email protected]');
$email->subject('Sent from Gmail');
$email->send('My message'); // or use a template etc

should do it.

You may want to set the sender as well; I'm not 100% but I imagine it will be useful when sending email "from" gmail via your own website; perhaps to stop the email being picked up as spam.

$email->sender('[email protected]', 'MyApp emailer');

Share:
22,817
shibly
Author by

shibly

Updated on February 11, 2020

Comments

  • shibly
    shibly about 4 years

    I'm trying to send email from a gmail account using CakEmail and SMTP settings .

    It would be nice if someone tell the process step by step what to do .

    I have added the following in app/Config/email.php=>

    <?php
    class EmailConfig {
        public $smtp = array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => 465,
            'username' => '[email protected]',
            'password' => 'secret'
        );
    }
    

    Now how can i send email to any email account from "[email protected]" ?

    It's CakePHP-2.0

  • shibly
    shibly over 12 years
    This is older email component.