SwiftMailer - PHP - How to disable ssl certificate validation
Asked Answered
C

4

8

I need to disable the validation of ssl certificate for developing purpose but i don't find anything about this in official documentation. http://swiftmailer.org/docs/introduction.html

I'm using php 5.6 and Symfony2 (v2.7).

The configuration reference of SwiftMailerBundle is:

swiftmailer:
   transport:            smtp
   username:             ~
   password:             ~
   host:                 localhost
   port:                 false
   encryption:           ~
   auth_mode:            ~
   spool:
      type:                 file
      path:                 '%kernel.cache_dir%/swiftmailer/spool'
   sender_address:       ~
   antiflood:
      threshold:            99
      sleep:                0
   delivery_address:     ~
   disable_delivery:     ~
   logging:              '%kernel.debug%'
Chassepot answered 6/4, 2016 at 14:59 Comment(1)
Check github.com/swiftmailer/swiftmailer/issues/571 . You probably need to upgrade Swift to the latest version and follow their instructions there.Athalee
M
19

I found an undocumented feature

symfony 2.8, php 5.6, swiftmailer-bundle 2.5.3

Swiftmailer Configuration

swiftmailer:
   stream_options:
    ssl:
        verify_peer: false
        verify_peer_name: false
Modestine answered 22/3, 2017 at 6:45 Comment(4)
Also works with Symfony 3.3, Swiftmailer-bundle 3.1.6 and php 7.1Defective
Unrecognized option "stream_options" under "swiftmailer" with Symfony 3.3, Swiftmailer-bundle 3.1.6 and php 7.1Chante
Confirmed working on Symfony 4.0.10! I had been struggling with this for nearly two hours D: I suspect my SMTP server SSL certificate isn't set up correctly, but at least I can continue development now :DCribb
If you are using multiple mailers, stream_options must be placed under the appropriate mailer key (like any other options)Cholent
C
8

The configuration option suggested by Maxim didn't work for me (Symfony 3.4 and Swift Mailer 5.4)

The cleanest solution I could find is the following:

$transport = $mailer->getTransport();
if($transport instanceof \Swift_Transport_EsmtpTransport){
    $transport->setStreamOptions([
        'ssl' => ['allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false]
    ]);
}

where $mailer is your SwiftMailer instance which you use in some service or controller

Cholent answered 13/2, 2018 at 14:26 Comment(0)
G
3

I think this is a dirty hack but it works for me fine ;)

$https['ssl']['verify_peer'] = FALSE;
$https['ssl']['verify_peer_name'] = FALSE; // seems to work fine without this line so far
/** @var \Swift_Transport_EsmtpTransport $transport */
$transport = $this->get('swiftmailer.mailer.default.transport');
$transport->setStreamOptions($https);
Geibel answered 24/4, 2016 at 16:1 Comment(0)
D
1

Getting the swiftmailer.mailer.default.transport from Symfony and setting following configuration works for me:

$transport = $this->get("swiftmailer.mailer.default.transport");

// disable SSL certificate validation
$transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));

I don't think that it is possible to set the stream options in the .yml file.

Deryl answered 23/1, 2017 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.