【问题标题】:Send message over SMTP using Swiftmailer and Yii2使用 Swiftmailer 和 Yii2 通过 SMTP 发送消息
【发布时间】:2017-04-16 05:51:00
【问题描述】:

我需要一些关于以下案例的建议:

我已经配置了config/web.php文件如下

'components' => [
    ...
    'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',

            'useFileTransport' => false,
            'transport' => [
                    'class' => 'Swift_SmtpTransport',
                    'host' => 'smtp.gmail.com',
                    'username' => 'myusername',
                    'password' => 'mypassword',
                    'port' => '587',
                    'encryption' => 'tls',
                        ],
                   ],

也在控制器中:

$message = Swift_Message::newInstance()
                          ->setSubject('...')
                          ->setFrom(['mymail'])
                          ->setTo(['recipient'])
                          ->setBody('......');

$transport = Swift_SmtpTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$mailer->send($message);

我收到错误:'无法与主机建立连接' (Swift_TransportException)

有什么想法吗?谢谢。

【问题讨论】:

    标签: yii2 smtp swiftmailer


    【解决方案1】:

    既然你已经把它配置为 Yii 组件,请正确使用它。
    documentation例子:

    Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
        ->setFrom('from@domain.com')
        ->setTo($form->email)
        ->setSubject($form->subject)
        ->send();
    

    另外:见this SO question

    【讨论】:

      【解决方案2】:

      在您的配置中将其设置为

       'mailer' => [
              'class' => 'yii\swiftmailer\Mailer',
              // send all mails to a file by default. You have to set
              // 'useFileTransport' to false and configure a transport
              // for the mailer to send real emails.
                  'transport' => [
                      'class' => 'Swift_SmtpTransport',
                      'host' => 'smtp.gmail.com',
                      'username' => 'yourusername',
                      'password' => 'yourpassword',
                      'port' => '587',
                      'encryption' => 'tls',
                  ]
      
          ],
      
      • 您可以在控制器中撰写电子邮件。

         Yii::$app->mailer->compose('@app/mail/layouts/reset',['model' =>$model])
         ->setTo('to@example.com')
         ->setFrom('from@example.com')
         ->setSubject('Subject Here')
         ->setTextBody('You can either render the layout or declare your body here')
         ->send();
        
      • 在上面的代码中,我使用了布局。您可以使用布局并将变量传递给布局并使用它们。如果您不想使用效果很好的布局,只需调用 compose 方法

        Yii::$app->mailer->compose()

      【讨论】:

        【解决方案3】:

        我做了以下工作:

        $transport = Swift_SmtpTransport::newInstance(Yii::$app->components['mailer']['transport']['host'], Yii::$app->components['mailer']['transport']['port']);
        $transport->setUsername(Yii::$app->components['mailer']['transport']['username']);
        $transport->setPassword(Yii::$app->components['mailer']['transport']['password']);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-18
          • 2011-06-19
          相关资源
          最近更新 更多