在yii2中使用swiftmailer发送电子邮件时,Get-error SSL操作失败,代码为1


Get error SSL operation failed with code 1 when using swiftmailer to send an email in yii2

我正在尝试使用yii2中包含的mailer发送电子邮件。但是当我提交电子邮件时收到了这个错误。

stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

我不确定是本地主机的设置问题。

以下是我在common/config/main-local.php 中设置的mailer配置代码

 'mailer' => [
    'class' => 'yii'swiftmailer'Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp-mail.outlook.com',
        'username' => 'myMail@outlook.com',
        'password' => 'myPassword',
        'port' => '587',
        'encryption' => 'tls',
    ],
]

以下是我试图提交电子邮件的代码

$model = new email;
if($model->load(Yii::$app->request->post(),'email') && $model->validate()){
    if(count($model->htmlBody)>=1){
    Yii::$app->mailer->compose()
         ->setFrom("myPass@outlook.com")
         ->setTo($model->receiver)
         ->setSubject($model->subject)
         ->send();
         Yii::$app->session->setFlash("msg","A mail has been sent");
    }
    else{
          Yii::$app->mailer->compose()
         ->setFrom("imotthegod@outlook.com")
         ->setTo($model->receiver)
         ->setSubject($model->subject)
         ->send();
         Yii::$app->session->setFlash("msg","A mail has been sent");
    }
}
return $this->render("email",['model'=>$model]);

尝试此配置

'mailer' => [
    'class' => 'yii'swiftmailer'Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp-mail.outlook.com',
        'username' => 'myMail@outlook.com',
        'password' => 'myPassword',
        'port' => '587',
        'encryption' => 'tls',
        'streamOptions' => [
            'ssl' => [
                'verify_peer' => false,
                'allow_self_signed' => true
            ],
        ],
    ],
]

根据文件common/config/main-local.php中的以下代码更改mailer设置

'mailer' => [
    'class' => 'yii'swiftmailer'Mailer',
    'viewPath' => '@common/mail',
    'useFileTransport' => false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => 'your username',
        'password' => 'your password',
        'port' => 587,
        'encryption' => 'tls',
        'streamOptions' => [ 
            'ssl' => [ 
                'allow_self_signed' => true, 
                'verify_peer' => false, 
                'verify_peer_name' => false, 
            ], 
        ],
    ],
],

在上面的代码中看到这个数组:

    'streamOptions' => [ 
        'ssl' => [ 
            'allow_self_signed' => true, 
            'verify_peer' => false, 
            'verify_peer_name' => false, 
        ], 
    ],

verify_peer用于验证所使用的SSL证书。

verify_peer_name用于对等体名称的验证。

这两个变量的默认值都是TRUE,这导致代码出现问题。

点击此处阅读更多信息。

<?php
    require_once 'vendor/autoload.php';
    // Create the Transport
    $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
      ->setUsername('cyr.freaxxx@gmail.com')
      ->setPassword('kwbbmewlbylwnuoh')
    ;
    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);
    $emailBody = 'Here is the message itself';
    if (isset($_POST['submit'])) {
        //echo '<pre>'; print_r($_POST); exit;
        $name        =   $_POST['name'];
        $email       =   $_POST['email'];
        $phone       =   $_POST['phone'];
        $message     =   $_POST['message'];
        $emailBody = "New Enquiry ! <br> Name:  $name <br> Email: $email <br> Phone: $phone <br> Message: $message";
    }

    // Create a message
    $message = (new Swift_Message('New Enquiry'))
      ->setFrom(['cyr.freaxxx@gmail.com' => 'AKP learning'])
      ->setTo(['vaibhavrajput201997@gmail.com'])
      ->setBody($emailBody)
      ;
    // Send the message
    $result = $mailer->send($message);
    if($result){
        echo "Email has been sent successfully!";
    }else{
        echo "Email not sent! please debug";
    }
?>