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



我试图发送一封电子邮件w/CakePHP虽然Mandrill,但它只是旋转然后给出:

错误:连接超时

蛋糕 网络异常 SocketException

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

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

来自/tmp/error.log:

2015-09-21 13:48:04错误:[CakeNetworkExceptionSocketException]请求URL:/portal/users/add Stack Trace:

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

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

#2/home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Mailer/Email.php(1304): CakeMailerTransportSmtpTransport->send(Object(CakeMailerEmail))

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

#4[内部函数]:AppControllerPortalUsersController->add()

# 5/home/user/public_html/user/mysitefolder/供应商/cakephp/cakephp/src/控制器/Controller.php (416):中的call_user_func_array(数组,数组)

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

#7/home/user/public_html/user/mysitefolder/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(87): CakeRoutingDispatcher->_invoke(Object(AppControllerPortalUsersController))

#8/home/user/public_html/user/mysitefolder/webroot/index.php(37): CakeRoutingDispatcher->dispatch(Object(CakeNetworkRequest), Object(CakeNetworkResponse))

# 9{主要}

在控制器内发送邮件:

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

在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' => ['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'
    ]

回顾:我查过w/Mandrill,他们说他们没有封锁任何端口或类似的东西。我已经验证了我的主机(HostMonster),他们说端口是开放的,他们没有阻止任何东西。

我很茫然。即使不可能得到一个明确的答案,我也很感激任何帮助我指出正确的方向或一些想法,我甚至可以调试这个问题。

我希望你说的是CakePHP 3。x/Linux (Ubuntu)/山魈。你不需要改变默认的电子邮件传输和电子邮件在Config/app.php,而你可以使用这种方法,如果你喜欢发送交易电子邮件。

1:获取Composer.phar

在Terminal中运行以下命令,获取composer.phar,

cd /var/www/html/your-project-name/
php -r "readfile('https://getcomposer.org/installer');" | php

上面的命令将安装composer。

2:更新Composer。

Lennaert/cakephp3-mandrill

"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: 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' => ['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' => 'MandrillEmailNetworkEmailMandrillTransport',
            'host' => 'smtp.mandrillapp.com',
            'key' => 'your-secret-key'
        ],
    ],

5: Send Email

控制器中添加

use MandrillEmailNetworkEmailMandrillTransport;
use CakeNetworkEmailEmail;

在your函数中,

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
)

希望这有助于你的需要。

相关内容

  • 没有找到相关文章

最新更新