Sending Mail using CakePHP 3.0

后端 未结 2 1979
既然无缘
既然无缘 2020-12-16 06:40

I am developing a website with new version 3.0 of CakePHP Framework. I am working on localhost and would like to send an email after a user has filled a form. Below is the c

2条回答
  •  既然无缘
    2020-12-16 07:10

    Hi thanks everyone for your answer.

    By using mailjet.com I was able to send e-mails in localhost. Below the different steps:

    Step 1

    Create an account on mailjet website

    Step 2

    In app.php add a new entry in the table EmailTransport. The different parameter host, port, username and password can be found on mailjet website.

    'EmailTransport' => [
       'default' => [
         'className' => 'Mail',
            // The following keys are used in SMTP transports
            'host' => 'localhost',
            'port' => 25,
            'timeout' => 30,
            'username' => 'user',
            'password' => 'secret',
            'client' => null,
            'tls' => null,
        ],
        'mailjet' => [
            'host' => 'in-v3.mailjet.com',
            'port' => 587,
            'timeout' => 60,
            'username' => 'xxxxx',
            'password' => 'xxxxx',
            'className' => 'Smtp'
        ]
    ],
    

    Step 3

    In your controller

    request->is('post')){
    
            $userName = $this->request->data['firstname'] . " " . $this->request->data['lastname'];
    
            $email = new Email();
            $email->transport('mailjet');
    
    
            try {
                $res = $email->from([$this->request->data['email'] => $userName])
                      ->to(['myEmail@hotmail.com' => 'My Website'])
                      ->subject('Contact')                   
                      ->send($this->request->data['message']);
    
            } catch (Exception $e) {
    
                echo 'Exception : ',  $e->getMessage(), "\n";
    
            }
    
    
        }
    }
    

提交回复
热议问题