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

前端 未结 5 1215
醉酒成梦
醉酒成梦 2021-02-10 03:49

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 a

相关标签:
5条回答
  • 2021-02-10 03:54

    I am currently using a gmail account to send outbound mail. I'm using templates and a reusable email setup function. Here is a copy of my working code:

    // app/controllers/users_controller.php
    function sendemail($subject, $body, $to, $template) {
        $this->Email->smtpOptions = array(
            'port'=>'465',
            'timeout'=>'30',
            'host' => 'ssl://smtp.gmail.com',
            'username'=>'username@domain.com',
            'password'=>'secret',
        );
        $this->Email->delivery = 'smtp';
        //$this->Email->delivery = 'debug';
        $this->Email->from    = 'Username <username@Domain.com>';
        $this->Email->to      = $to;
        $this->Email->subject = $subject;
        $this->set('body', $body);
        $this->set('smtp_errors', $this->Email->smtpError);
        $this->Email->send($content, $template);
    }
    
    // app/controllers/users_controller.php 
    // Excerpt from new user method in users controller:
    function add() {
        // ...other stuff
        $body['user'] = $user['User']['username'];
        $this->sendemail('Domain.com New User Signup!', $body, 'destination@Domain.com', 'newuser');
        // ...other stuff
    }
    
    // app/views/elements/email/text/newuser.ctp
    Everyone,
    Another new user just signed up for Domain. Stats below:
    User: <?php echo $body['user'] . "\r\r"; ?>
    Just thought you'd like to know :)
    -Janet
    
    0 讨论(0)
  • 2021-02-10 03:58

    Just set the from:

    <?php
    $email = new CakeEmail();
    $email->from(array('my@gmail.com' => 'Your Name'));
    $email->to('foo@stackoverflow.com');
    $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('noreply@mydomain.com', 'MyApp emailer');

    0 讨论(0)
  • 2021-02-10 04:02

    The right configuration is:

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

    So, don't forget the transport element.

    0 讨论(0)
  • 2021-02-10 04:05

    Use the Swiftmailer component; this is the easiest component to use.

    http://bakery.cakephp.org/articles/mhuggins/2008/06/11/improved-swiftmailer-component

    There are some changes that you need to do while using this with CakePHP 2.0 onwards. CakePHP 2.0 provides an 'Emails' view directory, which is used to store all the email templates.

    Changes to the component:

    1. Change all var declarations to public
    2. Change public $layout = 'Emails'; to public $viewPath = '/Emails';

    3. Change the render path in _getBodyText:

    $body = $this->controller->render($this->viewPath . DS . 'text' . DS . $view, $this->layout . DS . 'text'.DS.'default');

    1. Change the render path in _getBodyHtml:

    $body = $this->controller->render($this->viewPath . DS . 'html' . DS . $view, $this->layout . DS . 'html'.DS.'default');

    1. Comment out the lines:

    $bodyText = $this->_getBodyText($view); $message->setBody($bodyText, "text/plain");

    The Swiftmailer component sends a blank email if you set both HTML & TEXT active. It reads from both the email views & adds the body for text. That's the reason to comment these two lines if you want to send html emails.

    The second reason is if both are activated & you have content in both email.html.ctp & email.text.ctp files, it creates an header issue in that only the text format gets displayed in emails (in reality both the formats are present in header, but it suppresses the html part & shows the text part).

    0 讨论(0)
  • 2021-02-10 04:12

    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' => 'my@gmail.com',
            'password' => 'secret'
        );
    }
    

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

    0 讨论(0)
提交回复
热议问题