Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

email - Sending Mail using CakePHP 3.0

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("[email protected]")
             ->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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 AppController;

use AppControllerAppController;
use CakeNetworkEmailEmail;

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(['[email protected]' => 'My Website'])
                  ->subject('Contact')                   
                  ->send($this->request->data['message']);

        } catch (Exception $e) {

            echo 'Exception : ',  $e->getMessage(), "
";

        }


    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...