yii2中的Swiftmailer和smtp gmail

yii2中的Swiftmailer和smtp gmail,yii2,yii2-advanced-app,swiftmailer,Yii2,Yii2 Advanced App,Swiftmailer,我试着在yii2 advanced中配置swiftmailer。我阅读了教程,使用了这里的代码,但仍然有一个错误 在我的情况下,客人必须在下载电子书之前提交他的姓名和电子邮件。然后,他们会从我的办公室收到一封包含电子书下载链接的电子邮件 我在common/config/main local.php中的配置 'components' => [ 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer',

我试着在yii2 advanced中配置swiftmailer。我阅读了教程,使用了这里的代码,但仍然有一个错误

在我的情况下,客人必须在下载电子书之前提交他的姓名和电子邮件。然后,他们会从我的办公室收到一封包含电子书下载链接的电子邮件

我在
common/config/main local.php中的配置

'components' => [
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        'useFileTransport' => false,
        'transport' => [ 
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'myaccount@gmail.com',
            'password' => 'password',
            'port' => '465',
            'encryption' => 'ssl',

        ],     
    ],
],
在my
controller/GuestController.php中

public function actionCreate()
{       
    $model = new guest();
    if($model->load(Yii::$app->request->post())){
        return Yii::$app->mailer->compose()
        ->setFrom('myaccount@gmail.com')
        ->setTo($model->email)
        ->setSubject('halo')
        ->setTextBody('body')
        ->send();
        $model->save();
            return $this->redirect(['buku/index']);
    }
    else{
        return $this->renderAjax('create',[
                'model' => $model,
        ]);
    }
}
客串

<div class="guest-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'nama')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>

<?php
    echo Alert::widget([
    'options' => [
        'class' => 'alert-warning',
    ],
    'body' => '[Alert] Check your email for Download',
    ]);
?> 
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>

还有我的错误

无法与主机建立Swift_TransportException连接 smtp.gmail.com[#0]


我怎样才能解决这个问题?谢谢,问题解决了

我一直使用
加密的设置作为
tls
和端口
587
,您不需要将
'useFileTransport'=>设置为false,
现在默认为false,请参见


我使用了您的代码,但出现错误,stream\u socket\u enable\u crypto():SSL:操作已成功完成:(您是否使用localhost@Ayunda上的当前代码?您是否可以显示异常的完整跟踪,它说了什么?在我的代码中,我指定了
tls
加密,您这样做了吗?是的,我使用端口
587
tls
并且我没有设置
'useFileTransport'=>false
。但是我得到了一个错误
stream\u套接字。)_启用加密():SSL:操作已成功完成
'mailer' => [
            'class' => 'yii\swiftmailer\Mailer' ,
            'viewPath' => '@common/mail',
            'transport' => [
                'class' => 'Swift_SmtpTransport' ,
                'host' => 'smtp.gmail.com' ,
                'username' => 'myaccount@gmail.com',
                'password' => 'password',
                'port' => '587' ,
                'encryption' => 'tls' ,
            ] ,
        ] ,