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

前端 未结 5 1211
醉酒成梦
醉酒成梦 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 ';
        $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: 
    Just thought you'd like to know :)
    -Janet
    

提交回复
热议问题