Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftMailer - PHP - How to disable ssl certificate validation

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%'
like image 368
Angelo Giuffredi Avatar asked Apr 06 '16 14:04

Angelo Giuffredi


People also ask

How do I disable SSL certificate validation in IIS?

In the navigation tree, under SSL 3.0, select Server and then, in the right pane, double-click the Enabled DWORD value. In the Edit DWORD (32-bit) Value window, in the Value Data box leave the value at 0 and then, click OK. Restart your Windows server. You have successfully disabled the SSL v3 protocol.

Should I disable SSL verification?

SSL helps prevent a website from leaking sensitive personal or business data, such as a social security number or bank account information, to unapproved third parties. Firefox recommends that you do not turn off SSL certificates, but you may need to disable them temporarily to troubleshoot problems with the browser.

What is swift mailer?

Swift Mailer is a component based library for sending e-mails from PHP applications.


4 Answers

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
like image 137
Maxim Fazlutdinov Avatar answered Oct 18 '22 02:10

Maxim Fazlutdinov


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

like image 21
Atan Avatar answered Oct 18 '22 03:10

Atan


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);
like image 3
adam Avatar answered Oct 18 '22 03:10

adam


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.

like image 1
baris1892 Avatar answered Oct 18 '22 04:10

baris1892