user1443519
user1443519

Reputation: 589

CakePHP mail not working on server

I recently uploaded a CakePHP 2.3.8 app to a Ubuntu 12.04 EC2 instance and now I cannot send emails when using $Email->send() but I can do it with the "fast" method of CakeEmail::deliver('[email protected]', 'Subject', 'Content');, however I have a layout that I want to use.

When I try to send an email I get an error stating "Could not send email. Error: An Internal Error Has Occurred."

Here is my code for sending the email.

App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('[email protected]' => 'Me'));
$Email->to('[email protected]');
$Email->subject('Test Email');
$Email->template('layout_1');
$Email->emailFormat('html');
$testvalues = array('item1' => 'test1', 'item2' => 'test2');            
$Email->viewVars(array('tesvalues'=> $testvalues));
$Email->send();
$this->Session->setFlash('Email has been sent');
$this->redirect($this->referer(), null, true); 

In /App/Config/email.php here is what I have for smtp

public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('[email protected]' => 'Me'),
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'timeout' => 30,
    'username' => '[email protected]',
    'password' => '****',
    'client' => null,
    'log' => false,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

The exact line that the stack trace flags is in CORE/Cake/Network/Email/MailTransport.php

$this->_mail($to, $email->subject(), $message, $headers, $params);

I checked the error log and it says

Error: [SocketException] Could not send email.

Upvotes: 1

Views: 2529

Answers (1)

Guillermo Mansilla
Guillermo Mansilla

Reputation: 3889

I am going to shoot in the dark here.

It seems to me that you are not setting the layout correctly, according to the documentation you need to tell cake the layout and the view you want to use, for example:

$Email = new CakeEmail();
$Email->template('welcome', 'fancy')
    ->emailFormat('html')
    ->to('[email protected]')
    ->from('[email protected]')
    ->send();

The above would use app/View/Emails/html/welcome.ctp for the view, and app/View/Layouts/Emails/html/fancy.ctp for the layout.

Anyways, I recommend taking a look at Cake logs (app/tmp/logs) and see the cause of your error

Upvotes: 3

Related Questions