2

我想用 mailtrap 测试从 Yii2 应用程序发送的电子邮件。我已将 mailtrap 中的确切配置复制到我的 Web 配置文件中。

我的邮箱配置如图:

        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.mailtrap.io',
                'username' => '00xxxxxxcbe',
                'password' => '35xxxxxxx9d6',
                'port' => '2525',
                'encryption' => 'tls',
            ],
            'enableSwiftMailerLogging' => true,
        ],

我使用以下代码发送测试电子邮件:

     public function actionIndex()
     {
            try{
                Yii::$app->mailer->compose()
                    ->setFrom('name@email.com')
                    ->setTo('name@email.com')
                    ->setSubject('Message subject')
                    ->setTextBody('Plain text content')
                    ->setHtmlBody('<b>HTML content</b>')
                    ->send();
    
                return $this->render('index');
            }
            catch(Exception $ex){
                $message = $ex->getMessage();
                throw new ServerErrorHttpException($message, '500');
            }
     }

如果我将配置中的 useFileTransport 选项设置为 TRUE,我可以看到已创建的电子邮件文件。但是,如果我将此选项设置为 FALSE,并尝试使用 mailtrap,则会出现以下异常:

Connection could not be established with host smtp.mailtrap.io :stream_socket_client(): unable to connect to tcp://smtp.mailtrap.io:2525 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

我在 Windows 上使用 WAMP 堆栈。

我该如何解决这个问题?

4

0 回答 0