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

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

藏色散人
Release: 2023-04-10 08:14:01
forward
3508 people have browsed it

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',
     ],
 ],
Copy after login

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);
        }
    }
Copy after login

Write the route in \config\routes.php

$routes->connect('/send', ['controller' => 'ControllerName', 'action' => 'send']);
Copy after login
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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template