Sending mail using CakePHP 3.0

I am developing a website with a new version of CakePHP Framework 3.0. I work on localhost and would like to send an email after the user has filled out the form. Below is the code of my controller for sending email.

public function index(){ if ($this->request->is('post'){ $email = new Email(); $email->from([$this->request->data["sender"] => "Sender"] ->to(" myEmail@hotmail.com ") ->subject($this->request->data["Subject"]) ->send($this->request->data["message"]); } } 

When this code is executed, nothing happens, there is no error, there is no message in my inbox. I saw that in cakephp3.0 there is a class called DebugTransport , but I don't know how to use it to debug my code. Has anyone already used it?

+5
source share
2 answers

You should have used an SMTP server to deliver email from your local host in the configuration email.

There are two ways to achieve this:

  1. Use it from a real server with mail configuration

  2. Use the SMTP server for testing on the local host. There are many SMTP servers with the ability to use it. see mailjet.com

+3
source

Hello everyone for your reply.

Using mailjet.com, I was able to send emails to localhost. Below are the various steps:

Step 1

Create an account on the mailjet website.

Step 2

In app.php, add a new entry to the EmailTransport table. Various host, port, username and password options can be found on the 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

 <?php namespace App\Controller; use App\Controller\AppController; use Cake\Network\Email\Email; class ContactController extends AppController { var $helpers = array('Html'); public function index(){ if($this->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"; } } } 
+16
source

Source: https://habr.com/ru/post/1204701/


All Articles