Peter
Peter

Reputation: 231

How to use CakeEmail

I'm trying to find out how to use CakeEmail. To this end I've created a view called email-tester.ctp, using the Pages controller. I access it by calling the page directly (mydomain/mypath/pages/email-tester).

The view itself just holds the standard code from the documentation, more or less:

<?php
App::uses('CakeEmail', 'Network/Email');

$Email = new CakeEmail();
$Email->from(array('[email protected]' => 'My Gmail Address'))
    ->to('my@email-address')
    ->subject('About')
    ->send('My message');
?>
<p>Email sent...</p>

I have also created an email config file (email.php) as follows:

class EmailConfig {
        public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => '[email protected]',
        'password' => 'mypassword-for-gmail',
        'transport' => 'Smtp',
           );
}

When I run this page I just get an internal server error:

Error: An Internal Error Has Occurred. Stack Trace

CORE/Cake/Network/Email/MailTransport.php line 51 → MailTransport->_mail(string, string, string, string, null)
CORE/Cake/Network/Email/CakeEmail.php line 1158 → MailTransport->send(CakeEmail)
APP/View/Pages/email-tester.ctp line 8 → CakeEmail->send(string)
CORE/Cake/View/View.php line 948 → include(string)
CORE/Cake/View/View.php line 910 → View->_evaluate(string, array)
CORE/Cake/View/View.php line 471 → View->_render(string)
CORE/Cake/Controller/Controller.php line 954 → View->render(string, null)
APP/Controller/PagesController.php line 69 → Controller->render(string)
[internal function] → PagesController->display(string)
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(PagesController, array)
CORE/Cake/Routing/Dispatcher.php line 191 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 165 → Dispatcher->_invoke(PagesController, CakeRequest)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)

Can anyone point out to me where I'm going wrong, please? Do I need to have a mail server running on my local development machine where I'm working? Or is the problem something more fundamental?

From other research I saw some suggestions that the php.ini file should contain a line for the extension php_openssl, however this just causes an error message to appear saying that the extension is not found: probably because it's included via gnutls (I'm on Ubuntu 14.10).

Any help would be much appreciated.

Peter

Upvotes: 2

Views: 1007

Answers (1)

Carlos Montoya
Carlos Montoya

Reputation: 51

If you are working from a local server such as wampp, xampp, etc, this function wont work, yo do need to have a mail server setup for this to work. Once you have on, or if you can test in a real server this might help:

In:

Views->Layouts->Emails->html

I have an html template for my emails called "clientsreports":

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
   <head>
       <title><?php echo $title_for_layout;?></title>
   </head>
   <body>   
        <?php echo $content_for_layout;?>       
    </body>
</html>

Also your var in the EmailConfig file should be named smtp for it to work, check how mine is setup

class EmailConfig {
    public $smtp = array(
        'transport' => 'Smtp',
        'from' => array('[email protected]' => 'Senders name'),
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'timeout' => 30,
        'username' => '[email protected]',
        'password' => 'mypassword',
        'client' => null,
        'log' => false
    );
}

Finally this is a simple example of how i send out emails:

$email = new CakeEmail('smtp');
$email->template('clientsreport', 'clientsreport');
$email->emailFormat('html');
$email->viewVars(array('message' => "This is the body of the message"));
$email->from(array('[email protected]' => 'Senders name'));
$email->to('[email protected]');
//Only if neccesary this is how to carbon copy someone
$email->cc(array('[email protected]','[email protected]'));
$email->subject('Subject for the email');        
$email->send(); 

I hope that helps, good luck!

Upvotes: 1

Related Questions