简体   繁体   English

获取错误使用swiftmailer在yii2中发送电子邮件时,代码1的SSL操作失败

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

I'm trying to send a email by using the included mailer in yii2. 我正在尝试使用yii2中包含的邮​​件程序发送电子邮件。 But getting this error when I submit the email. 但是在提交电子邮件时收到此错误。

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

I not sure is that the setup issue with my localhost. 我不确定是本地主机的安装问题。

Following is my mailer config code set in common/config/main-local.php 以下是我在common / config / main-local.php中设置的邮件程序配置代码

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

Following is the code I trying to submit an email 以下是我尝试提交电子邮件的代码

$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]);

Try this config 试试这个配置

'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
            ],
        ],
    ],
]

Change your mailer setting as per below code in file common/config/main-local.php 根据文件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, 
            ], 
        ],
    ],
],

See this array in the above code: 在上面的代码中查看此数组:

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

verify_peer is used to for the verification of SSL certificate used. verify_peer用于验证所使用的SSL证书。

verify_peer_name is used for verification of peer name. verify_peer_name用于验证对等名称。

The default values for both of these variables is TRUE which was causing problem with your code. 这两个变量的默认值为TRUE ,这导致您的代码出现问题。

Read more here . 在这里阅读更多。

<?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";
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM