使用 mailtrap 测试 Yii2 电子邮件

问题描述

我想用 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('[email protected]')
                    ->setTo('[email protected]')
                    ->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 堆栈。

我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)