Mail doesnt work after deploying - laravel

I am building laravel project, I used mailgun to send emails. Locally it worked just fine. After deploying on heroku it does not send any emails.
services.php
'mailgun' => [
'domain' => env('sandbox.....'),
'secret' => env('key-.......'),
mail.php
driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'example#example.com'),
'name' => env('MAIL_FROM_NAME', 'shop'),
],
'encryption' => env('MAIL_ENCRYPTION', ''),
Tried looking at heroku logs but cannot see anything about mail so I dont really understand it. What could I be missing?

I had the same issue when deploying my app. I came up with following checkpoints so it works on production with Heroku. My bet for your issue is number 4.
Make sure you have set verified domain correctly (mailgun -> domains -> verified domains & dns - all green).
Make sure you updated env variables in Heroku to work with verified domain (heroku -> settings -> reveal config vars) should have fields: MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_SMTP_PASSWORD, MAILGUN_SMTP_LOGIN, MAILGUN_SMTP_SERVER, MAILGUN_SMTP_PORT with proper values.
Make sure you have cleared the list of authorized recipients (mailgun -> domains -> authorized reciepients) - so everyone can receive your emails.
Make sure you have cleared the list of IP whitelist (mailgun -> account overview -> account settings -> security -> IP whitelist) or included ip of your Heroku app. Note this point is not about IP management for domain but for your mailgun account.
In my case the only ip there was my localhost ip (for dev) and it was blocking Heroku deployed app for sending emails.

Related

Can one user table be used for both API and regular authentication?

I have multiple front end apps using one database. I wanted to use a single user table to authenticate users for all apps. The problem is some apps access tables in the database through regular authentication while some are isolated reacts apps accessing the database through API authentication.
My question is, can I use the same user table to authenticate users from different apps using both regular and API authentication. For your info, I am using Laravel as a backend. Thank you for your help :).
I guess with regular authentication you mean session and the answer is yes.
Because authentication data and logic are always separated from the authenticatable table (when using jwt you get another table for tokens and with session authentication you use cache or database).
so yes you will use as many guards as you wish with the same table it will look like this in your config/auth.php file:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],

Laravel 7 UI - Sessions Guard Driver - Spatie Permission Package

In short: The Sessions guard driver is rejecting my login attempts.
Before creating the Laravel UI I had already created a authentication system for my frontend using JWT as the api guard driver.
I installed the Laravel UI in order to use the Spatie Permission Package which had also been installed and had generated some extra tables.
Once the Laravel UI was installed I successfully registered a new user but was not redirected to the home screen. I attempted to log in as the new user and got a response stating that my credentials were not recognised. I then tried to log in with a user that had been previously created in the users table through the frontend registration...
Whilst it gave no error this time it still did not redirect me. I then attempted to log into the frontend app with the newly created user and was able to do so. At this point I figured there was a conflict with the tokens.
I looked in RegisterController.php and saw the following:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
My frontend JWT auth system also uses User.php and so I had a look and saw that the password was being encrypted twice. First with Hash:: and then with bcrypt. I therefore removed the Hash encryption.
I registered successfully but again was not redirected to home. I attempted to log in again and was again unable to do so, but this time I received no error message! The registered user was now behaving in the same manner as users registered through the front end app.
I went through the code line-by-line spending hours logging out values etc. and could see that everything was being accepted and I was being redirected to home but the HomeController.php was then passing my credentials through the auth guard which was rejecting then and then redirecting me back to login!!
The only way I can access home is by changing the middleware in HomeController.php from 'auth' to 'guest':
public function __construct()
{
$this->middleware('guest'); // changed from 'auth'
}
In the Spatie documention it states the following:
If your app uses only a single guard, but is not web (Laravel’s default, which shows “first” in the auth config file) then change the order of your listed guards in your config/auth.php to list your primary guard as the default and as the first in the list of defined guards. While you’re editing that file, best to remove any guards you don’t use, too.
I therefore switched the guards around in config/auth.php so that api was first but it had no effect.
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
The frontend is using the api guard and the backend is using web but they are both using the same app/User. This I guess must be the cause of the problem but I am new to Laravel and, after hours of reading and testing, I am still struggling. What does it want that I am missing!? The password is now being accepted so it cannot be that..
Any help would be very much appreciated. Thank you.
The problem was in the top section of config/auth which I had missed:
'defaults' => [
'guard' =>'api',
'passwords' => 'users',
],
It wasn't that I was being rejected by the web session driver - it was that the web guard was being overwritten by the api guard in the defaults section above.
I have to specify in the routing which middleware I wanted so as to avoid the default:
Route::get('/home', [
'middleware' => 'auth:web', 'uses' => 'HomeController#index'])->name('home');
EDIT: Since auth:web was only used by a couple of controllers the better solution was to change the default guard in config/auth to web and then do Route::group with the rest that used the api guard:
Route::group([
'middleware' => 'api'
], function () {
Route::post('login', 'AuthController#login');
Route::post('register', 'AuthController#register');
Route::post('logout', 'AuthController#logout');
Route::post('refresh', 'AuthController#refresh');
});

Not Getting Email using Gmail smtp on live server Laravel

I am on Laravel 5.6 and my application working well.
I am using Gmail SMTP for mail services.
here is my .env file.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username#gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
I have already Turned ON Less secure apps in Gmail also turned ON 2 step verification.
From localhost, the mail system working like charm and I am getting all emails from Gmail SMTP.
But From Live Server, I am not getting any Emails and even there is no error popup.
My Host is a 000webhost free account because I am testing my Application.
so I read forums in 000webhost and change "SET MX RECORD " to "GOOGLE'S MX RECORD".
Now I am getting this error while sending any mail.
Expected response code 250 but got code "530", with message "530-5.5.1
Authentication Required. Learn more at 530 5.5.1
https://support.google.com/mail/?p=WantAuthError k142sm4568022ywa.67 -
gsmtp "
Please help!
Thanks in advance.
try changing your config/mail.php to your current credentials:
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME', 'username#gmail.com'),
'password' => env('MAIL_PASSWORD', 'password'),
Please allow access to less secure app from your google account following these steps:
Go to your Google Account.
On the left navigation panel, click Security.
On the bottom of the page, in the Less secure app access panel, click Turn on access.
If you don't see this setting, your administrator might have turned off less secure app account access.

Laravel - emails not sent using mandrill

I have mandrill configured for sending emails. Recently it stopped working so I tried to login to the mandrill account to find the following notice. I checked the MailChimp account and it was linked but still nothing is working. I have followed the documentation for sending using SwiftMailer but still nothing works.
In the documentation, it was stated to use MANDRILL_USERNAME and MANDRILL_PASSWORD. Are these the SMTP credentials is the account. If not, where can I find them in order to double check all configurations?
I'm using mandrill without problems but i have configured the service with a key to send emails.
This is how i do it:
Please check you have this in your .env:
MAIL_DRIVER=mandrill
MANDRILL_SECRET=kOAYRloydIXXXXXXXXXXXxxx
Next check your config/services.php
'mandrill' => [
'secret' => env('MANDRILL_SECRET'),
],
And your config/mail.php
'driver' => env('MAIL_DRIVER', 'mailgun'),
Hope this helps.

Outlook Oauth - send emails with wl.imap scope in php

I would like to automate sending few emails using Live Connect API.
I had been looking at this page here from the Live Connect SDK and i found is this :
http://msdn.microsoft.com/en-ca/library/hh243646.aspx : it is stating exactly :
wl.imap The wl.imap scope enables read and write access to a user's
email using IMAP, and send access using SMTP
I wanted to use that scope to use the smtp send email right, Im able to get the access token however there is no doc about how to communicate with smtp server etc...
You can follow this example that is for gmail:
http://www.chargenevier.com/2013/02/google-smtp-and-oauth2-authentication/
For outlook you need change this:
$config = array('ssl' => 'tls', 'port' => '587', 'auth' => 'oauth2', 'xoauth2_request' => $initClientRequestEncoded);
$transport = new Zend_Mail_Transport_Smtp('smtp-mail.outlook.com', $config);

Resources