【问题标题】:Get error SSL operation failed with code 1 when using swiftmailer to send an email in yii2使用 swiftmailer 在 yii2 中发送电子邮件时出现错误 SSL 操作失败,代码为 1
【发布时间】:2016-07-26 04:53:50
【问题描述】:

我正在尝试使用 yii2 中包含的邮​​件程序发送电子邮件。但是当我提交电子邮件时收到此错误。

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' => [
    '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]);

【问题讨论】:

    标签: php yii2 swiftmailer


    【解决方案1】:

    试试这个配置

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

    【讨论】:

    • 感谢@Bizley,它通过添加 'streamOptions' => [ 'ssl' => [ 'verify_peer' => false, 'allow_self_signed' => true ], ], 对我有用
    • 它工作了一段时间,现在突然出现这个错误。这是什么原因!
    【解决方案2】:

    根据文件 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,这会导致您的代码出现问题。

    阅读更多here

    【讨论】:

      【解决方案3】:
      <?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";
          }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2016-01-23
        • 2016-04-07
        • 2020-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-08
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多