我无法从我的cakephp 3网站发送邮件。我尝试了一个simiple testmail.php文件,我收到了电子邮件但是当我尝试在app.php上设置相同的用户名和密码时,我的连接超时了。
Error: [Cake\Network\Exception\SocketException] Connection timed out
这是在1and1.com上。我可以在localhost上安装时发送邮件。
可能出错:
这是error.log:
2017-08-24 14:05:09 Error: [Cake\Network\Exception\SocketException] Connection timed out
Request URL: /contactus/
Referer URL: https://example.com/
Stack Trace:
#0 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php(207): Cake\Network\Socket->connect()
#1 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php(162): Cake\Mailer\Transport\SmtpTransport->_connect()
#2 /homepages/30/d665828522/htdocs/app/vendor/cakephp/debug_kit/src/Mailer/Transport/DebugKitTransport.php(79): Cake\Mailer\Transport\SmtpTransport->send(Object(Cake\Mailer\Email))
#3 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Mailer/Email.php(1344): DebugKit\Mailer\Transport\DebugKitTransport->send(Object(Cake\Mailer\Email))
#4 /homepages/30/d665828522/htdocs/app/src/Controller/UsersController.php(335): Cake\Mailer\Email->send()
#5 /homepages/30/d665828522/htdocs/app/src/Controller/UsersController.php(113): App\Controller\UsersController->_sendVerificationCode(Object(Cake\ORM\Entity))
#6 [internal function]: App\Controller\UsersController->ajaxRegister()
#7 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Controller/Controller.php(435): call_user_func_array(Array, Array)
#8 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/ActionDispatcher.php(121): Cake\Controller\Controller->invokeAction()
#9 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/ActionDispatcher.php(95): Cake\Http\ActionDispatcher->_invoke(Object(App\Controller\UsersController))
#10 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/BaseApplication.php(83): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#11 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
#12 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php(62): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#13 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
#14 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#15 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
#16 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php(81): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#17 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
#18 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Runner.php(51): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#19 /homepages/30/d665828522/htdocs/app/vendor/cakephp/cakephp/src/Http/Server.php(90): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#20 /homepages/30/d665828522/htdocs/app/webroot/index.php(37): Cake\Http\Server->run()
#21 {main}
答案 0 :(得分:-1)
您能否验证自定义的EmailTransport配置,并确保使用自定义的EmailTransport配置而不是默认配置?
在App.php中,EmailTransport配置应如下所示:
'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,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'custom' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'A.B.C.D', // Ip of your server
'port' => XX, // Port that you use: 25 or 465 or 587
'timeout' => 60,
'username' => 'MyUsername',
'password' => 'MyPassword',
'client' => null,
'tls' => true,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'debug' => [
'className' => 'Debug',
// The following keys are used in SMTP transports
'host' => 'A.B.C.D', // Ip of your server
'port' => XX, // Port that you use: 25 or 465 or 587
'timeout' => 60,
'username' => 'MyUsername',
'password' => 'MyPassword',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'gmail' => [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'test@gmail.com',
'password' => 'password_of_test',
'headers' => array(
'X-Mailer' => 'PHP'
),
'charset' => 'UTF8',
'headerCharset' => 'UTF8',
'className' => 'Smtp',
'tls' => null
],
],
在你的控制器内发送你的电子邮件:
<?php
namespace App\Controller;
use Cake\Mailer\Email;
/* ... */
private function sendResetEmail($msg, $user) {
$email = new Email();
$email->transport('custom'); // Here we call to ou custom config
$email->template('YourEmailTemplate');
$email->emailFormat('both');
$email->from('noreply@example.com');
$email->to($user->email, $user->username);
$email->subject('Reset your password');
$email->viewVars(['msg' => $msg, 'username' => $user->username]);
try {
$email->send();
} catch(Exception $e) {
throw new Exception("Exception caught with message: " . $e->getMessage().'\n'.$email->smtpError);
}
}
希望有所帮助