Question

SSL error while sending mail using SwiftMailer on DigitalOcean droplet. How to debug?

Posted November 21, 2020 108 views
PHPLaravel

0

I don’t know how to go about debugging this. Any help would be great!

I am using Laravel 4.2 (I know it’s high time I upgrade). I am sending mail using Gmail SMTP. I am able to send mail on local environment without issue, but on the production server it is throwing the following error:

[2020-11-20 15:55:52] production.ERROR: ErrorException: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14161044:SSL routines:state_machine:internal error in /var/www/domain.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:94
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleError(2, 'stream_socket_e...', '/var/www/domain...', 94, Array)
#1 /var/www/domain.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(94): stream_socket_enable_crypto(Resource id #10, true, 9)
#2 /var/www/domain.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(313): Swift_Transport_StreamBuffer->startTLS()
#3 /var/www/domain.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand()
#4 /var/www/domain.com/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start()
#5 /var/www/domain.com/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(307): Swift_Mailer->send(Object(Swift_Message), Array)
#6 /var/www/domain.com/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(134): Illuminate\Mail\Mailer->sendSwiftMessage(Object(Swift_Message)) #7 /var/www/domain.com/bootstrap/compiled.php(3180): Illuminate\Mail\Mailer->send('emails.bulk-ord...', Array, Object(Closure))
#8 /var/www/domain.com/app/libraries/functions/sendMail.php(34): Illuminate\Support\Facades\Facade::__callStatic('send', Array)
#9 /var/www/domain.com/app/controllers/BulkOrderController.php(70): App\Libraries\Functions\sendMail('emails.bulk-ord...', Array, Array, 1)
#10 [internal function]: BulkOrderController->checkout()
#11 /var/www/domain.com/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(231): call_user_func_array(Array, Array)
#12 /var/www/domain.com/bootstrap/compiled.php(5437): Illuminate\Routing\Controller->callAction('checkout', Array)
#13 /var/www/domain.com/bootstrap/compiled.php(5425): Illuminate\Routing\ControllerDispatcher->call(Object(BulkOrderController), Object(Illuminate\Routing\Route), 'checkout')
#14 /var/www/domain.com/bootstrap/compiled.php(4657): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'BulkOrderContro...', 'checkout')
#15 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#16 /var/www/domain.com/bootstrap/compiled.php(4984): call_user_func_array(Object(Closure), Array)
#17 /var/www/domain.com/bootstrap/compiled.php(4682): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#18 /var/www/domain.com/bootstrap/compiled.php(4670): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#19 /var/www/domain.com/bootstrap/compiled.php(706): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#20 /var/www/domain.com/bootstrap/compiled.php(687): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#21 /var/www/domain.com/bootstrap/compiled.php(7311): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#22 /var/www/domain.com/bootstrap/compiled.php(7908): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#23 /var/www/domain.com/bootstrap/compiled.php(7855): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#24 /var/www/domain.com/bootstrap/compiled.php(10990): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#25 /var/www/domain.com/bootstrap/compiled.php(648): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#26 /var/www/domain.com/public/index.php(49): Illuminate\Foundation\Application->run()

The code is hosted on a DigitalOcean droplet (Ubuntu 20.04).

Contents of app/config/mail.php:

<?php

return array(
    'driver' => 'smtp',
    'host' => 'smtp.gmail.com',
    'port' => 587,
    'from' => array('address' => 'contact@domain.com', 'name' => 'Company Name'),
    'encryption' => 'tls',
    'username' => 'GmailUserName',
    'password' => 'GmailPassword',
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
);

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @tuhinpaul,

Indeed as you mentioned, it would be best to go ahead and upgrade but it definitely can be challenging.

I could not replicate this myself, but you could try the suggestion from this discussion here:

Edit the config/mail.php file and add the following:

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

Note: this is not ideal as it is essentially disabling TLS and could have some security implications.

Let me know how it goes!
Regards,
Bobby

  • Hi @bobbyiliev,
    Thanks for the reply. I have already gone through the stackoverflow link and have already tried disabling TLS. But it still throws that same error. In fact that solution is suggested for SSL3GETSERVER_CERTIFICATE:certificate verify failed error.

    In my case that is not the case. In my case it simply mentions, state_machine:internal error in...

    So I am not even sure where to look for in order to pin point the issue. I am also not able to locate php_errors.log on the server to investigate the issue.

    • Hi there @tuhinpaul,

      What I could suggest is checking your Apache error log first, you can do this with the following command:

      • sudo tail -100 /var/log/apache2/error.log

      You can also check the location of your PHP error log by adding a PHP info file to your public directory:

      <?php
      phpinfo();
      ?>
      

      And then visiting the file via your browser. On that page, you will see all of the information for your PHP settings and find the location of the PHP error log.

      Regards,
      Bobby

      • Thank you. I checked the apache log. The following is what I find relevant to the issue (I have removed the domain and ip)

        [Sat Nov 21 11:16:26.918109 2020] [ssl:info] [pid 135509] [client xxx.xxx.xxx.xxx:45703] AH01964: Connection to child 2 established (server domain.com:443)
        [Sat Nov 21 11:16:26.918520 2020] [ssl:debug] [pid 135509] ssl_engine_kernel.c(2372): [client xxx.xxx.xxx.xxx:45703] AH02043: SSL virtual host for servername www.domain.com found
        [Sat Nov 21 11:16:26.918535 2020] [ssl:debug] [pid 135509] ssl_engine_kernel.c(2372): [client xxx.xxx.xxx.xxx:45703] AH02043: SSL virtual host for servername www.domain.com found
        [Sat Nov 21 11:16:26.918542 2020] [core:debug] [pid 135509] protocol.c(2313): [client xxx.xxx.xxx.xxx:45703] AH03155: select protocol from , choices=h2,http/1.1 for server domain.com
        [Sat Nov 21 11:16:27.178098 2020] [ssl:debug] [pid 135509] ssl_engine_kernel.c(2254): [client xxx.xxx.xxx.xxx:45703] AH02041: Protocol: TLSv1.3, Cipher: TLS_AES_256_GCM_SHA384 (256/256 bits)
        [Sat Nov 21 11:16:27.178243 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(493): AH00831: socache_shmcb_store (0x8e -> subcache 14)
        [Sat Nov 21 11:16:27.178576 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(847): AH00847: insert happened at idx=28, data=(6174:6206)
        [Sat Nov 21 11:16:27.178581 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(850): AH00848: finished insert, subcache: idx_pos/idx_used=25/4, data_pos/data_used=5505/893
        [Sat Nov 21 11:16:27.178584 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(515): AH00834: leaving socache_shmcb_store successfully
        [Sat Nov 21 11:16:27.178678 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(493): AH00831: socache_shmcb_store (0x6a -> subcache 10)
        [Sat Nov 21 11:16:27.178687 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(847): AH00847: insert happened at idx=31, data=(6863:6895)
        [Sat Nov 21 11:16:27.178690 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(850): AH00848: finished insert, subcache: idx_pos/idx_used=28/4, data_pos/data_used=6193/895
        [Sat Nov 21 11:16:27.178694 2020] [socache_shmcb:debug] [pid 135509] mod_socache_shmcb.c(515): AH00834: leaving socache_shmcb_store successfully
        [Sat Nov 21 11:16:27.182201 2020] [ssl:debug] [pid 135509] ssl_engine_kernel.c(415): [client xxx.xxx.xxx.xxx:45703] AH02034: Initial (No.1) HTTPS request received for child 2 (server domain.com:443), referer: https://www.domain.com/path
        [Sat Nov 21 11:16:27.182388 2020] [authz_core:debug] [pid 135509] mod_authz_core.c(817): [client xxx.xxx.xxx.xxx:45703] AH01626: authorization result of Require all granted: granted, referer: https://www.domain.com/path
        [Sat Nov 21 11:16:27.182394 2020] [authz_core:debug] [pid 135509] mod_authz_core.c(817): [client xxx.xxx.xxx.xxx:45703] AH01626: authorization result of <RequireAny>: granted, referer: https://www.domain.com/path
        [Sat Nov 21 11:16:27.182493 2020] [authz_core:debug] [pid 135509] mod_authz_core.c(817): [client xxx.xxx.xxx.xxx:45703] AH01626: authorization result of Require all granted: granted, referer: https://www.domain.com/path
        [Sat Nov 21 11:16:27.182508 2020] [authz_core:debug] [pid 135509] mod_authz_core.c(817): [client xxx.xxx.xxx.xxx:45703] AH01626: authorization result of <RequireAny>: granted, referer: https://www.domain.com/path
        [Sat Nov 21 11:16:27.315568 2020] [ssl:debug] [pid 135509] ssl_engine_io.c(1102): [client xxx.xxx.xxx.xxx:45703] AH02001: Connection closed to child 2 with standard shutdown (server domain.com:443)
        

        Thanks for the suggestion on phpinfo(). error_log was set to no value. I made the change in the php.ini file by uncommenting and setting a value to it

        error_log = "/var/log/apache2/php_errors.log"
        

        The change is reflecting on phpinfo(), so I know I have made the change in the right php.ini. I have created an empty php_errors.log at the location. But no errors are being logged in it. The read and write permissions are the same as other log files.

Submit an Answer