Can't send mail on server using Laravel and mailjet - php

I am working on a Laravel project and try to send emails using mailjet. Sending mails local works fine, but using the same settings on my centos 7 webserver gives me an error which I can't resolve for days now.
.env settings
MAIL_MAILER=smtp
MAIL_DRIVER=smtp
MAIL_HOST=in-v3.mailjet.com
MAIL_PORT=587
MAIL_USERNAME=***
MAIL_PASSWORD=***
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=no-reply#***.com
MAIL_FROM_NAME="${APP_NAME}"
Sending the email
Mail::send('emails.auth.verify-email', ['token' => $token, 'user' => $user], function($message) use($user){
$message->to($user->email);
$message->subject('Email Verification Mail');
});
Error on CentOS 7 Server
Swift_TransportException
Connection could not be established with host in-v3.mailjet.com :stream_socket_client(): unable to connect to tcp://in-v3.mailjet.com:587 (Permission denied)
I already tried
restarting apache
sudo reboot
php artisan cache:clear
php artisan config:clear
I have also tried mailgun instead of mailjet, got me the same error (only on the server, worked fine local)
hard coding the connection credentials in config/mail.php instead of loading them from the .env file
as requested in the comments, here is the output of: dd(Config::get('mail'))
array:4 [▼
"default" => "smtp"
"mailers" => array:8 [▼
"smtp" => array:8 [▼
"transport" => "smtp"
"host" => "in-v3.mailjet.com"
"port" => 587
"encryption" => "tls"
"username" => "***"
"password" => "***"
"timeout" => null
"auth_mode" => null
]
"ses" => array:1 [▼
"transport" => "ses"
]
"mailgun" => array:1 [▼
"transport" => "mailgun"
]
"postmark" => array:1 [▼
"transport" => "postmark"
]
"sendmail" => array:2 [▼
"transport" => "sendmail"
"path" => "/usr/sbin/sendmail -t -i"
]
"log" => array:2 [▼
"transport" => "log"
"channel" => null
]
"array" => array:1 [▼
"transport" => "array"
]
"failover" => array:2 [▼
"transport" => "failover"
"mailers" => array:2 [▼
0 => "smtp"
1 => "log"
]
]
]
"from" => array:2 [▼
"address" => "no-reply#pokeshares.com"
"name" => "PokeShares"
]
"markdown" => array:2 [▼
"theme" => "default"
"paths" => array:1 [▼
0 => "/var/www/pokeshares/resources/views/vendor/mail"
]
]
]
These look like the correct settings.
Any help is appreciated!

Related

How to get user in Laravel by XSRF-TOKEN cookie?

I can't get the authenticated user in Laravel app. I have this codes:
config/auth.php
return [
'defaults' => [
'guard' => 'sanctum',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'members',
],
'api' => [
'driver' => 'session',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'members' => [
'driver' => 'eloquent',
'model' => Domain\Customer\Models\Member::class,
]
],
];
config/sanctum.php
return [
'stateful' => // ...
'guard' => null,
'expiration' => null,
'middleware' => [
'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class,
],
];
routes/web.php
Route::prefix('auth')->group(function ($router) {
Route::post('login', [AuthController::class, 'loginAsMember']);
// ...
});
Route::middleware('lang')->group(function ($router) {
// ...
Route::prefix('{locale}')->group(function () {
Route::middleware('auth')->group(function () {
Route::get('webshop/basket', [PublicBasketController::class, 'show'])->name(RouteName::BASKET);
});
});
// ...
});
I have an Authenticate middleware where I try to catch the user and if it's not logged in I redirect to the custom login url.
class Authenticate extends Middleware
{
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route(RouteName::LOGIN, ['local' => App::getLocale()]);
}
}
}
In here if I dd(Auth::user()) it receives null.
But if I dd($request) I see this:
+cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▼
#parameters: array:4 [ ▼
"XSRF-TOKEN" => "MbJcRadlrAJ2mDhECvWwMFyIe0fyqrQUO83K2U1K"
"laravel_session" => "mqWuCqTRD074TOvOPMGRfIIgP0jxKLyoD8VyyWCS"
]
}
+headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▼
#headers: array:13 [▼
"cookie" => array:1 [▼
0 => "_ga=... ▶"
]
"accept-language" => array:1 [▶]
"accept-encoding" => array:1 [▶]
"referer" => array:1 [▶]
"accept" => array:1 [▶]
"user-agent" => array:1 [▶]
"upgrade-insecure-requests" => array:1 [▶]
"cache-control" => array:1 [▶]
"pragma" => array:1 [▶]
"connection" => array:1 [▶]
"host" => array:1 [▶]
"content-length" => array:1 [▶]
"content-type" => array:1 [▶]
]
#cacheControl: array:1 [▼
"no-cache" => true
]
}
So there is an valid XSRF-TOKEN cookie, but Laravel did not identify the user.
How can I get user by XSRF-TOKEN cookie?
Try using dd($request->user) or dd(auth('sanctum')->user().

Laravel Mailgun Unable to send an email: 404 page not found

I'm trying to use mailgun in laravel 9 but I always get this error:
Symfony\Component\Mailer\Exception\HttpTransportException: Unable to send an email: 404 page not found
My env:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=xxxxxxx
MAILGUN_SECRET=xxxxxxx
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_ENCRYPTION=tls
My mail.php
'default' => env('MAIL_MAILER', 'mailgun'),
My services.php
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
Try replacing "tls" with "starttls" for your MAIL_ENCRYPTION value
MAIL_ENCRYPTION=starttls

Laravel Passport: Provider for API Guard returning null

I have an API using Laravel 5.7 and Passport. I'm trying to get the User Provider from the Guard when calling the API but it's returning null ONLY for the API Guard.
When I use dd(auth()) I get the following:
#guards: array:2 [▼
"web" => SessionGuard {#489 ▼
#name: "web"
#lastAttempted: null
#viaRemember: false
#session: Store {#492 ▶}
#cookie: CookieJar {#493 ▶}
#request: Request {#55 ▶}
#events: Dispatcher {#35 ▶}
#loggedOut: false
#recallAttempted: false
#user: null
#provider: EloquentUserProvider {#483 ▼
#hasher: HashManager {#488 ▶}
#model: "App\User"
}
}
"api" => RequestGuard {#526 ▼
#callback: Closure {#527 ▶}
#request: Request {#55 ▶}
#user: User {#655 ▶}
#provider: null
}
]
On my config/auth.php file:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
Is there any other configuration I have to set to make this work?

Laravel sending mail issue after site migration

I know this question has been asked many times before, but I have attempted a lots of these solutions without any success.
Hello i have issue in sending mail to laravel as its working fine but after migrating site to other server with ssl and now having issue as its giving below error.
Expected response code 220 but got code \"\", with message \"\"
below is the code used for .end and mail.php
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
mail.php
<?php
return [
'driver' => env('MAIL_DRIVER'),
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'developer.eww#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'GBAN'),
],
'encryption' => env('MAIL_ENCRYPTION','tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/lib/sendmail -i -t',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];

Cannot connect to gmail SMTP Server

I am using cakephp 3.0. I am trying to send mail using cakephp through gmail SMTP Server. I am trying to do it from my localhost so I don't have ssl. This is the configuration I've done in app.php:
'EmailTransport' => [
'gmail'=>[
'className'=>'Smtp',
'host'=>'smtp.gmail.com',
'port'=>587,
'timeout' => 60,
'username'=>'myemail#gmail.com',
'password'=>'mypassword',
'tls' => true,
]
]
This is where I've created my email profile :
'Email' => [
'gmail' => [
'transport' => 'gmail',
'from' => 'myemail#gmail.com'
]
],
This is the code from my custom mailer class :
$mail
->to($email)
->profile('gmail')
->subject($this->subject)
->emailFormat('html')
->template('welcome')
->viewVars([
$name=>$name,
$code=>$code
]);
I've already allowed access to less secure apps in my gmail account. This is the error cakephp is throwing :
SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS
Attaching a complete snapshot of the error :
Found the solution. Added these parameters in the transport config to bypass ssl authentication:
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
Worked like a charm.

Categories