Home  >  Article  >  Backend Development  >  How to send email (Gmail) through CakePHP built-in module

How to send email (Gmail) through CakePHP built-in module

藏色散人
藏色散人forward
2021-06-11 11:45:063466browse

This article introduces how to send emails (Gmail) through the built-in module of CakePHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Effect

The function of sending emails is realized by configuring the module in advance. (This code cannot change the sending email address based on user input information)

Prerequisite

Readers already have basic knowledge of CakePHP

Version

CakePHP 3.6.1
PHP 7.4.10

Preparation

\config\app.php Add the following configuration (remember to replace the email information)

'EmailTransport' => [
        'default' => [
            'className' => 'Smtp',
            'host' => 'smtp.gmail.com',
            'port' => 587,
            'timeout' => 30,
            'username' => 'name@gmail.com',
            'password' => '12345678',
            'tls' => true,
            'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],
    ],
'Email' => [
     'default' => [
         'transport' => 'default',
         'from' => 'name@gamail.com',
         //'charset' => 'utf-8',
         //'headerCharset' => 'utf-8',
     ],
 ],

Go to any Controller.php and write the function for sending emails.
*There is a reference here stackoverflow

//在最上面加载模块
use Cake\Mailer\Email;

//在任意class下写邮件发送的函数
public function send()
    {
        $email = new Email('default');
        try {
            $email->setFrom(['name@gmail.com' => 'My Site'])
                ->setTo('接受者邮箱@126.com')
                ->setSubject('主题')
                ->send('本文');
            echo "success";
        } catch (\Cake\Network\Exception\SocketException $exception) {
            $lastResponse = $email->transport()->getLastResponse();
            var_dump($lastResponse);
        }
    }

Write the route in \config\routes.php

$routes->connect('/send', ['controller' => 'ControllerName', 'action' => 'send']);
access, test
How to send email (Gmail) through CakePHP built-in module

error report,This is because the security level of gmail is too high.

Log in to your Google Account Management Center->Security

Turn off two-step verification

How to send email (Gmail) through CakePHP built-in module

Enable access

How to send email (Gmail) through CakePHP built-in module

How to send email (Gmail) through CakePHP built-in module

#Then refresh the page and you will find that we have successfully sent the email through gmail.
How to send email (Gmail) through CakePHP built-in module

For account security, remember to increase the security protection level after the test is successful.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to send email (Gmail) through CakePHP built-in module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete