CakePhp 4 中 SMTP 的邮件程序问题

发布于 2025-01-15 17:11:29 字数 1957 浏览 3 评论 0原文

我正在使用 CakePHP 4.2.8 并尝试通过 smtp 发送电子邮件,但出现超时错误,并在其他 php 应用程序中使用相同的电子邮件帐户(使用 phpmailer )并且工作正常。 这是我的代码。

use Cake\Mailer\Mailer;
use Cake\Mailer\TransportFactory;

public function sendEmail()
{
    TransportFactory::setConfig('Manual', [
        'className' => 'Smtp',
        //'className' => 'Debug',
        'host' => 'mail.abc.com',
        'port' => 465,
        'username' => '[email protected]',
        'password' => 'password',
        'tls' => true // Tired with true and false
    ]);

    $mailer = new Mailer('default');
    $mailer->setTransport('Manual');

    try {
        $res = $mailer->setFrom(['[email protected]' => 'My Site'])
        ->setTo('[email protected]')
        ->setSubject('About')
        ->deliver('My message');
    pr($res);
    } catch (\Throwable $th) {
        ec($th);
    }
}

收到此错误

Cake\Network\Exception\SocketException Object
(
    [_attributes:protected] => Array
        (
        )
    [_messageTemplate:protected] => 
    [_responseHeaders:protected] => 
    [_defaultCode:protected] => 0
    [message:protected] => SMTP timeout.
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /Applications/MAMP/htdocs/cake/superpad/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php
    [line:protected] => 510

请帮助我我的代码有什么问题。使用相同的端口和其他信息与核心 php Web 应用程序中的 phpmailer 一起工作。

谢谢

更新

我找到了解决方案,端口应该是587。

I am using CakePHP 4.2.8 and trying to send email via smtp, but i am getting time out error and used same email account in other php app (using phpmailer ) and its working fine.
Here is my code.

use Cake\Mailer\Mailer;
use Cake\Mailer\TransportFactory;

public function sendEmail()
{
    TransportFactory::setConfig('Manual', [
        'className' => 'Smtp',
        //'className' => 'Debug',
        'host' => 'mail.abc.com',
        'port' => 465,
        'username' => '[email protected]',
        'password' => 'password',
        'tls' => true // Tired with true and false
    ]);

    $mailer = new Mailer('default');
    $mailer->setTransport('Manual');

    try {
        $res = $mailer->setFrom(['[email protected]' => 'My Site'])
        ->setTo('[email protected]')
        ->setSubject('About')
        ->deliver('My message');
    pr($res);
    } catch (\Throwable $th) {
        ec($th);
    }
}

Getting this error

Cake\Network\Exception\SocketException Object
(
    [_attributes:protected] => Array
        (
        )
    [_messageTemplate:protected] => 
    [_responseHeaders:protected] => 
    [_defaultCode:protected] => 0
    [message:protected] => SMTP timeout.
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /Applications/MAMP/htdocs/cake/superpad/vendor/cakephp/cakephp/src/Mailer/Transport/SmtpTransport.php
    [line:protected] => 510

Please help me what is wrong with my code. Using same port and other info its working with phpmailer in core php web app.

Thanks

UPDATE

I found solutions, port should be 587.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文