SSL error in Laravel 5.2 [duplicate] - php

This question already has answers here:
Laravel certificate verification errors when sending TLS email
(2 answers)
Closed 1 year ago.
I was unable to send an email in laravel 5,when I click send it return this message
ErrorException in StreamBuffer.php line 95:
stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Here is my .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp-relay.gmail.com
MAIL_PORT=587
MAIL_USERNAME=**********#gmail.com
MAIL_PASSWORD=*********
MAIL_ENCRYPTION=ssl
file mail.php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp-relay.gmail.comt'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
I researched for hours but couldn't resolve this, any advice ?
P/S: I use Windows 10 and XAMPP for Webserver

Change in .env file:
MAIL_DRIVER=smtp to MAIL_DRIVER=mail

Here are two possible solutions:
1) Get rid of "tls" and leave MAIL_ENCRYPTION as blank.
2) If you are on MacOS Sierra and want to keep your 'tls' setting, type these two lines in your terminal/shall:
$ sudo mkdir -p /usr/local/libressl/etc/ssl/certs
$ sudo curl -o /usr/local/libressl/etc/ssl/cert.pem https://curl.haxx.se/ca/cacert.pem
The two lines basically help you create the default folder and import the latest certificate store from curl.haxx.se.
Here is the source article I got if interested: https://andrewyager.com/2016/10/04/php-on-macos-sierra-cant-access-ssl-data/comment-page-1/#comment-52
Hope it helps.

In your config file
the host should be smtp.gmail.com
From and name should not be null
change your mail encryption from ssl to tsl
dont forget to turn on less secure app in your google accout setting

Go to location \vendor\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php
at line 259 and comment the following:
//$options = array();
and add.
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);

Related

how to solve ssl3_get_server_certificate error [duplicate]

I'm trying to send an email with this configuration:
return [
'driver' => 'smtp',
'host' => 'mail.mydomain.com',
'port' => 26,
'from' => ['address' => 'mailer#mydomain.com', 'name' => 'Mailer'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
When I submit the form I receive this erorr:
ErrorException in StreamBuffer.php line 95:
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 found this solution where people seems to have solved the problem with the same library but I cant manage to solve it in Laravel.
https://github.com/PHPMailer/PHPMailer/issues/368
Editor's note: disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack.
Be sure you fully understand the security issues before using this as a solution.
Add this at bottom of your config/mail.php
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
this will solve your problem.
In case you are using Laravel 7.0 you can disable SSL verification in SwiftMailer this way (please note that disabling SSL verification is not recommended!):
config/mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
],
],
EDIT: Since Laravel 9 depreciated SwiftMailer has been replaced with Symfony Mailer. According to update guide they way you disable TLS peer verification has changed as well.
'smtp' => [
// Laravel 8.x...
'stream' => [
'ssl' => [
'verify_peer' => false,
],
],
// Laravel 9.x...
'verify_peer' => false,
],
Well in that link you provided the solution is straight-forward.
The correct solution is to fix your SSL config - it's not PHP's fault!
how to fix it? in config/mail.php ,'driver' => env('MAIL_DRIVER', 'smtp'), should be 'driver' => env('MAIL_DRIVER', 'mail'), (credits: Danyal Sandeelo)
In Laravel 9 defining stream options for the SMTP transport is no longer supported. Instead, you must define the relevant options directly within the configuration if they are supported. For example, to disable TLS peer verification:
'smtp' => [
// Laravel 8.x...
'stream' => [
'ssl' => [
'verify_peer' => false,
],
],
// Laravel 9.x...
'verify_peer' => false,
],
In my case the problem was related to SSL. My SMTP has a self-signed certificate and my laravel was running on top of PHP 5.6 which disables the 'allow_self_signed' context variable to false and enables 'verify_peer' and hence poping the error when sending an email.
Since I didn't wanted to hack around swiftmailer code I added the Certificate Authority (CA) file of my server as trusted CA for my system executing laravel.
I did that getting the CA cert of my smtp server, something like
-----BEGIN CERTIFICATE-----
MIIElTCCA32gAwIBAgIJAMZjjNg64RQwMA0GCSqGSIb3DQEBCwUAMIGNMQswCQYD
VQQGEwJVUzEMMAoGA1UECBMDTi9BMQwwCgYDVQQHEwNOL0ExJDAiBgNVBAoTG1pp
...
5a8a4QEwWmnAOgHetsOCvhfeGW3yAJPD8Q==
-----END CERTIFICATE-----
and write it in my laravel machine which has an ubuntu 14.04 to a file named /usr/local/share/ca-certificates/my_cert.crt. It is crucial to end the file with .crt and also make it readable for everyone.
Then call update-ca-certificates and the certificate will be added to the list of valid CAs of your server.

Swift_TransportException:Laravel Swift Mail

I have a Laravel 5 project, and it is having trouble sending mail from our mail server. This mail server is working for another Laravel project with the almost identical setup.
I get the following error:
(1/1) Swift_TransportException
Expected response code 250 but got code "", with message ""
I have my mail.php file setup as such:
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'no-reply#xtreme.com.au'),
'name' => env('MAIL_FROM_NAME', 'Xtreme IT'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
And the .env file that is overwriting those settings:
MAIL_DRIVER=smtp
MAIL_HOST=mail.ourdomain.com.au
MAIL_PORT=25
MAIL_USERNAME=username#ourdomain.com.au
MAIL_PASSWORD=OURPASSWORD
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=no-reply#clientdomain.com.au
MAIL_FROM_NAME="Client"
Is there an easy way to check that my .env file is being used?
I can't work out why the mail will send on one site but not another.
Answering to the question: "Is there an easy way to check that my .env file is being used?"
You can use the env and config helpers doing:
dd(env($var_name), config($var_name));
Using it, you'll can see if they're the same.
If not, is possible that config cache is outdated, so you can try:
php artisan config:clear
and then
php artisan config:cache
Good look!

Could not send password reset link in laravel 5.3?

I cannot send the password reset link in the email with the error:
Swift_TransportException in AbstractSmtpTransport.php line 383:
Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
I have followed this video tutorial but still I could not solve the problem.
.env file
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=52ca17394c74f6
MAIL_PASSWORD=e89831236006c1
MAIL_ENCRYPTION=tls
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => 'hello#example.com',
'name' => 'Example',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
The mailtrap host is smtp.mailtrap.io and that's not what your .env file has. Also when you make changes to .env file, you should clear the config since it might be cached. You can clear it by running php artisan config:clear

Cannot send message without a sender address in laravel 5.2 I have set .env and mail.php both

In forgot password laravel sends email which gives error like :
Swift_TransportException in AbstractSmtpTransport.php line 162:
Cannot send message without a sender address
i already have set my mail account details in .env and mail.php both.
My .env :
MAIL_DRIVER=smtp
MAIL_HOST=smtp-mail.outlook.com
MAIL_PORT=587
MAIL_USERNAME=raval_himanshu#live.in
MAIL_PASSWORD=pass
MAIL_ENCRYPTION=tls
and my mail.php is
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp-mail.outlook.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['raval_himanshu#live.in' => null, 'Himanshu Raval' => null],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('raval_himanshu#live.in'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
i have tried php artisan config:cache too but after that it gives same error.
i have made mistake in mail.php
'from' => ['raval_himanshu#live.in' => null, 'Himanshu Raval' => null],
that should be
'from' => ['address' => 'raval_himanshu#live.in', 'name' => 'Himanshu Raval'],
just try this code below, because this work for me
in .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=myAPPpassword_Generatedfromhttps://myaccount.google.com/security#signin
MAIL_ENCRYPTION=tls
in config/mail.php i change like this
'from' => ['address' => 'myemail#gmail.com', 'name' => 'Admin.com'],//default :'from' => ['address' => null, 'name' => null],
then run the php artisan config:cache to clear configurations cache and cache again with new configurations, i hope can help
Just run php artisan optimize:clear, its work for me.
you should do it in build function in your Mail which you created example is given below
public function build()
{
return $this->from('example#gmail.com')->view('welcome');
}
After .env update, please restart php artisan (CTRL+C then php artisan serve)

How to deal with self-signed TLS certificates in Laravel's SMTP driver?

I'm trying to send an email with this configuration:
return [
'driver' => 'smtp',
'host' => 'mail.mydomain.com',
'port' => 26,
'from' => ['address' => 'mailer#mydomain.com', 'name' => 'Mailer'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
When I submit the form I receive this erorr:
ErrorException in StreamBuffer.php line 95:
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 found this solution where people seems to have solved the problem with the same library but I cant manage to solve it in Laravel.
https://github.com/PHPMailer/PHPMailer/issues/368
Editor's note: disabling SSL verification has security implications. Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack.
Be sure you fully understand the security issues before using this as a solution.
Add this at bottom of your config/mail.php
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
this will solve your problem.
In case you are using Laravel 7.0 you can disable SSL verification in SwiftMailer this way (please note that disabling SSL verification is not recommended!):
config/mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
],
],
EDIT: Since Laravel 9 depreciated SwiftMailer has been replaced with Symfony Mailer. According to update guide they way you disable TLS peer verification has changed as well.
'smtp' => [
// Laravel 8.x...
'stream' => [
'ssl' => [
'verify_peer' => false,
],
],
// Laravel 9.x...
'verify_peer' => false,
],
Well in that link you provided the solution is straight-forward.
The correct solution is to fix your SSL config - it's not PHP's fault!
how to fix it? in config/mail.php ,'driver' => env('MAIL_DRIVER', 'smtp'), should be 'driver' => env('MAIL_DRIVER', 'mail'), (credits: Danyal Sandeelo)
In Laravel 9 defining stream options for the SMTP transport is no longer supported. Instead, you must define the relevant options directly within the configuration if they are supported. For example, to disable TLS peer verification:
'smtp' => [
// Laravel 8.x...
'stream' => [
'ssl' => [
'verify_peer' => false,
],
],
// Laravel 9.x...
'verify_peer' => false,
],
In my case the problem was related to SSL. My SMTP has a self-signed certificate and my laravel was running on top of PHP 5.6 which disables the 'allow_self_signed' context variable to false and enables 'verify_peer' and hence poping the error when sending an email.
Since I didn't wanted to hack around swiftmailer code I added the Certificate Authority (CA) file of my server as trusted CA for my system executing laravel.
I did that getting the CA cert of my smtp server, something like
-----BEGIN CERTIFICATE-----
MIIElTCCA32gAwIBAgIJAMZjjNg64RQwMA0GCSqGSIb3DQEBCwUAMIGNMQswCQYD
VQQGEwJVUzEMMAoGA1UECBMDTi9BMQwwCgYDVQQHEwNOL0ExJDAiBgNVBAoTG1pp
...
5a8a4QEwWmnAOgHetsOCvhfeGW3yAJPD8Q==
-----END CERTIFICATE-----
and write it in my laravel machine which has an ubuntu 14.04 to a file named /usr/local/share/ca-certificates/my_cert.crt. It is crucial to end the file with .crt and also make it readable for everyone.
Then call update-ca-certificates and the certificate will be added to the list of valid CAs of your server.

Categories