3
votes

I'm trying to send an email w/ CakePHP though Mandrill, but it just spins then gives:

Error: Connection timed out

Cake\Network\Exception\SocketException

According to the on-page error/stack trace, the error occurs on: vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php line 206:

if (!$this->_socket->connect()) {

From the /tmp/error.log:

2015-09-21 13:48:04 Error: [Cake\Network\Exception\SocketException] Connection timed out Request URL: /portal/users/add Stack Trace:

#0 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php(206): Cake\Network\Socket->connect()

#1 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php(161): Cake\Mailer\Transport\SmtpTransport->_connect()

#2 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Mailer/Email.php(1304): Cake\Mailer\Transport\SmtpTransport->send(Object(Cake\Mailer\Email))

#3 /home/user/public_html/user/mysitefolder/src/Controller/Portal/UsersController.php(92): Cake\Mailer\Email->send()

#4 [internal function]: App\Controller\Portal\UsersController->add()

#5 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Controller/Controller.php(416): call_user_func_array(Array, Array)

#6 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(114): Cake\Controller\Controller->invokeAction()

#7 /home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(87): Cake\Routing\Dispatcher->_invoke(Object(App\Controller\Portal\UsersController))

#8 /home/user/public_html/user/mysitefolder/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))

#9 {main}

Sending the email in a controller:

use Cake\Mailer\Email;

// ...

$email = new Email('default');
$email->template('welcome', 'portal')
    ->from('[email protected]')
    ->to('[email protected]')
    ->send();

In my app.php:

'EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        'host' => 'smtp.mandrillapp.com',
        'port' => 587,
        'timeout' => 30,
        'username' => 'myusername',
        'password' => 'mymandrillapikey', // Key
        'client' => null,
        'tls' => true, // have also tried false
    ],
],
'Email' => [
    'default' => [
        'transport' => 'default',
        'from' => ['[email protected]' => 'My Name'],
        'replyTo' => ['[email protected]' => 'My Name'],
        'charset' => 'utf-8',
        'headerCharset' => 'utf-8',
        'log' => false,
        'emailFormat' => 'html'
    ],
],

If I try just standard PHP mail it works:

'EmailTransport' => [
    'default' => [
        'className' => 'Mail'
    ]

Recap: I've checked w/ Mandrill and they say they don't block any ports or anything like that. I've verified w/ my host (HostMonster), and they say the port is open and they're not blocking anything.

I'm at a loss. Even if it's not possible to get a definitive answer, I'd appreciate any help pointing me in the right direction or some idea how I could even debug this issue.

1
Can you connect using SSL (host' => 'ssl://smtp.mandrillapp.com' and 'port' => 465)? May also help to turn on more logging ('log' => true) as described here: book.cakephp.org/3.0/en/core-libraries/… to expose the actual SMTP conversation errors between your server and Mandrill's.terrorbox
@terrorbox - I tried, but same issue. Turned on error logging, and posted the log above.Dave
@Dave Did you find the solution? I am also getting the same errorDeepanshu Goyal

1 Answers

2
votes

I hope you're speaking about CakePHP 3.x/Linux (Ubuntu)/Mandrill. You need not to change the default Email Transport and Email in Config/app.php, rather you can use this method if you like to send transaction email.

1: Get Composer.phar

In Terminal run the following, to get composer.phar,

cd /var/www/html/your-project-name/

php -r "readfile('https://getcomposer.org/installer');" | php

The above will install composer.phar in your project folder.

2: Update Composer.json to install this plugin

Lennaert/cakephp3-mandrill

In the "require" part of the file add the following,

"lennaert/cakephp3-mandrill": "*"

in the last like below.

"require": {
    "php": ">=5.4.16",
    "cakephp/cakephp": "~3.0",
    "mobiledetect/mobiledetectlib": "2.*",
    "cakephp/migrations": "~1.0",
    "cakephp/plugin-installer": "*",
    "lennaert/cakephp3-mandrill": "*"
},

(or)

php composer.phar require lennaert/cakephp3-mandrill:*

3: Run Composer.json

In the terminal run the following

php composer.phar update

This will update the vendor file with the plugin folder, again run to get permissions,(Ubuntu)

chmod 777 -R /var/www/html/your-project-name/vendor/lennaert

4: Update Config/app.php

    'Email' => [
//        'default' => [
//            'transport' => 'default',
//            'from' => 'you@localhost',
//            //'charset' => 'utf-8',
//            //'headerCharset' => 'utf-8',
//        ],
        'Mandrill'=> [
            'template' => 'default',
            'transport' => 'Mandrill',
            'emailFormat' => 'both',
            'from' => ['[email protected]' => 'Prasi'],
            'sender' => ['[email protected]' => 'Prasi'],
            'Mandrill' => [] // Don't ask, the plugin needs/wants this empty array
        ],
    ],

    '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,
//        ],
        'Mandrill' => [
            'className' => 'MandrillEmail\Network\Email\MandrillTransport',
            'host' => 'smtp.mandrillapp.com',
            'key' => 'your-secret-key'
        ],
    ],

5: Send Email

In the controller add

use MandrillEmail\Network\Email\MandrillTransport;
use Cake\Network\Email\Email;

In the your function,

public function send_mail()
    {       
        if(!empty($this->request->data)){
            if($this->request->is('post')){

                $emailObject=new Email('Mandrill');
                $emailObject->subject('Mandrill sends emails')
                            ->profile('Mandrill') // This is the profile you set above, in your config file
                            ->to('[email protected]', 'Receiver Name');

                $result=$emailObject->send();
                pr($result);
            }
        }
    }

If we print and see the result it will look like this,

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [status] => sent
            [_id] => 42d1468565be448ea357cad95304bd83
            [reject_reason] => 
        )

    [status] => sent
)

Hope this helps your need. Share me if it worked for you.