5

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 code in my controller to send the 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 happen, no error, no message in my mailbox. I have seen that it exist in cakephp3.0 a class called DebugTransport but I don't know how to use it in order to debug my code. Someone has already use it ?

dandan78
  • 13,328
  • 13
  • 64
  • 78
Snnopy_87
  • 271
  • 1
  • 3
  • 11
  • I think that you can't use the email() in your localhost, try to upload it to a live site. :) btw, I remember my friend can send an email using localhost xampp, maybe it depends in your apache or something. :) – r3mmel Oct 15 '14 at 05:42
  • you can use smtp to send mail from localhost – Abhishek Oct 15 '14 at 06:13
  • Please always mention your exact CakePHP version! If you are using a recent 3.x release, then this should fail hard with an exception regarding the missing transport. Also your code is missing a closing `(` for the `from()` call, please make sure that this problem is not present in your actual code. That being said, please check the docs on [**how to configure a transport**](http://book.cakephp.org/3.0/en/core-libraries/email.html#configuration) for the email class (note: the basic usage section is missing that). – ndm Oct 15 '14 at 21:21
  • There are many more options for sending email from localhost, ex: Mailgun, Mandrill etc – MotsManish Dec 14 '15 at 04:56

2 Answers2

16

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

<?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";

        }


    }
}
Snnopy_87
  • 271
  • 1
  • 3
  • 11
3

You had to use a SMTP server for delivery your email from your localhost in your config email.

There are 2 ways to achieve this:

  1. Use it from a real server with a mail configuration

  2. Use SMTP server for your test on your localhost. there are a lot of SMTP server with let you use it. see mailjet.com

Manoj Selvin
  • 2,247
  • 24
  • 20
MouradK
  • 1,527
  • 1
  • 11
  • 14