尝试通过CakePHP 3.1中的Mandrill发送电子邮件时超时

时间:2015-09-19 02:58:08

标签: email cakephp timeout mandrill cakephp-3.1

我试图通过Mandrill发送带有CakePHP的电子邮件,但它只是旋转然后给出:

  

错误:连接超时

     

蛋糕\网络\异常\ SocketException

根据页面错误/堆栈跟踪,错误发生在:vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php line 206

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

来自/tmp/error.log:

  

2015-09-21 13:48:04错误:[Cake \ Network \ Exception \ SocketException]   连接超时请求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) \电子邮件))

     

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

     

#4 [内部功能]: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 \)响应))

     

#9 {main}

在控制器中发送电子邮件:

use Cake\Mailer\Email;

// ...

$email = new Email('default');
$email->template('welcome', 'portal')
    ->from('myemail@gmail.com')
    ->to('myemail@gmail.com')
    ->send();

在我的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' => ['myemail@gmail.com' => 'My Name'],
        'replyTo' => ['myemail@gmail.com' => 'My Name'],
        'charset' => 'utf-8',
        'headerCharset' => 'utf-8',
        'log' => false,
        'emailFormat' => 'html'
    ],
],

如果我尝试使用标准的PHP邮件,那就可以了:

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

回顾: 我已经检查过Mandrill,他们说他们不会阻止任何端口或类似的东西。我已经与我的主机(HostMonster)进行了验证,他们说端口已打开,并且他们没有阻止任何内容。

我不知所措。即使不可能获得确定的答案,我也会感谢任何指导我正确方向的帮助,或者想知道如何调试这个问题。

1 个答案:

答案 0 :(得分:2)

我希望您谈论CakePHP 3.x / Linux(Ubuntu)/ Mandrill。您无需在Config / app.php中更改默认的电子邮件传输和电子邮件,而是如果您愿意,可以使用此方法来发送交易电子邮件。

1:获取Composer.phar

在终端运行以下命令,以获取composer.phar,

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

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

以上将在您的项目文件夹中安装composer.phar。

2:更新Composer.json以安装此插件

<强> Lennaert / cakephp3-山魈

在文件的“require”部分添加以下内容,

"lennaert/cakephp3-mandrill": "*"

在下面的最后一个。

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

(或)

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

3:运行Composer.json

在终端中运行以下

php composer.phar update

这将使用插件文件夹更新供应商文件,再次运行以获取权限,(Ubuntu)

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

4:更新Config / app.php

    'Email' => [
//        'default' => [
//            'transport' => 'default',
//            'from' => 'you@localhost',
//            //'charset' => 'utf-8',
//            //'headerCharset' => 'utf-8',
//        ],
        'Mandrill'=> [
            'template' => 'default',
            'transport' => 'Mandrill',
            'emailFormat' => 'both',
            'from' => ['xxx@xxx.com' => 'Prasi'],
            'sender' => ['xxx@xxx.com' => '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:发送电子邮件

在控制器中添加

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

在你的功能中,

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('sendto@email.com', 'Receiver Name');

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

如果我们打印并看到结果,它将如下所示,

Array
(
    [0] => Array
        (
            [email] => sendto@email.com
            [status] => sent
            [_id] => 42d1468565be448ea357cad95304bd83
            [reject_reason] => 
        )

    [status] => sent
)

希望这有助于您的需求。如果它适合你,请与我分享。