3

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

错误:连接超时

蛋糕\网络\异常\套接字异常

根据页面错误/堆栈跟踪,错误发生在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 堆栈跟踪:

#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(对象(Cake\Mailer\Email))

#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(对象(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 {主要}

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

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),他们说端口是开放的,他们没有阻止任何东西。

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

4

1 回答 1

2

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

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-mandrill

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

"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:更新配置/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
)

希望这对您的需求有所帮助。分享我,如果它对你有用。

于 2015-09-24T15:19:39.090 回答