CakePHP send email

前端 未结 2 1751
小蘑菇
小蘑菇 2021-01-07 09:41

I\'ve got a problem with sending mail using CakePHP. Everythings giong well, but i didn\'t receive any single mail , i tired to send to 2 different emails .

//Websit

相关标签:
2条回答
  • 2021-01-07 09:54

    Please follow the steps:

    step 1: In this file (app\Config\email.php) add this:

    public $gmail = array(
            'transport' => 'Smtp',
            'from' => array('site@localhost' => 'Any Text...'),
            'host' => 'ssl://smtp.gmail.com',
                    'port' => 465,
            'timeout' => 30,
            'username' => 'youremail@example.com',
            'password' => 'yourPassword',
            'client' => null,
            'log' => false,
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        );
    

    step 2: Add an email template (app\View\Emails\html\sample.ctp)

    <body>
    <h1>Email Testing: <?php echo $first_name?></h1>
    </body>
    

    step 3: Change the code in your method as shown below:

    public function send_my_email() {
            App::uses('CakeEmail', 'Network/Email');
            $Email = new CakeEmail();
            $Email->config('gmail'); //configuration
            $Email->emailFormat('html'); //email format
            $Email->to('receiveremail@ex.com');
            $Email->subject('Testing the emails');
            $Email->template('sample');//created in above step
            $Email->viewVars(array('first_name'=>'John Doe' ));//variable will be replaced from template
            if ($Email->send('Hi did you receive the mail')) {
                $this->Flash->success(__('Email successfully send on receiveremail@ex.com'));
            } else {
                $this->Flash->error(__('Could not send the mail. Please try again'));
            }
        }
    
    0 讨论(0)
  • 2021-01-07 10:03

    try this, you didn't set the config

    public function contact_email()
    { /* all data is taken from contact.ctp, I debuged all data below and it's correct */
        $useremail = $this->data['Website']['useremail'];
        $usertopic = $this->data['Website']['usertopic'];
        $usermessage = $this->data['Website']['usermessage'];
        $Email = new CakeEmail();
        $Email->config('smtp')
              ->emailFormat('html')
              ->from($useremail)        
              ->to('wigan@mail.com')
              ->subject($usertopic); // all data is correct i checked several times
        if($Email->send($usermessage))
        {
            $this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
            return $this->redirect(array('controller'=>'websites','action'=>'contact'));
        } else  {
            $this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
        }
    }
    
    0 讨论(0)
提交回复
热议问题