Before production version I used mailtrap to test my emails and everything worked like it should be, but today I uploaded my website to a public server and decided to use mailgun, I know its not so simple like mailtrap.io, but still. Anyway I verified my account and can now send 10k emails per month. Not bad, but the thing is when I try to send an email I get a notification that the email was sent, but there is no email in any inbox.
My .env file
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=myEmail
MAIL_PASSWORD=myPassword
MAIL_ENCRYPTION=tls
My services.php file
return [
'mailgun' => [
'domain' => env('myDomain'),
'secret' => env('secretKey'),
],
'ses' => [
'key' => env('SES_KEY'),
'secret' => env('SES_SECRET'),
'region' => 'us-east-1',
],
'sparkpost' => [
'secret' => env('SPARKPOST_SECRET'),
],
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
];
One of the functions:
public function store(StoreListingContactFormRequest $request, Area $area, Listing $listing){
Mail::to($listing->user)->queue(
new ListingContactCreated($listing,
$request->name,
$request->email,
$request->number,
$request->message
)
);
return back()->withSuccess("Teie sõnum on edukalt saadetud firmale {$listing->user->name}");
}
Another one:
public function __construct()
{
$this->middleware(['auth']);
}
public function index(Area $area, Listing $listing){
return view('listings.share.index', compact('listing'));
}
public function store(StoreListingShareFormRequest $request, Area $area, Listing $listing){
collect(array_filter($request->emails))->each(function($email) use ($listing, $request){
Mail::to($email)->queue(
new ListingShared($listing, $request->user(), $request->messages)
);
});
return redirect()->route('listings.show',[$area, $listing])->withSuccess('Kuulutus on jagatud edukalt!');
}
seems the error is here
MAIL_USERNAME=myEmail
MAIL_PASSWORD=myPassword
MAIL_USERNAME should be not your email but Default SMTP Login from your domain settings page. And Default Password on the same page for MAIL_PASSWORD
'domain' => env('myDomain'),
here you need to enter not your site's domain but the domain you've registered on mailgun, something like mg.exmaple.com
Related
I already implemented a Laravel passport oauth2 as server and integrated with my mobile application without the issue.
Laravel + vue(just default oauth page) integrate with JavaScript mobile application.
However, I trying to implement registration flow from my mobile application. The problem is.
My Laravel server will always redirect me to login page first if no authenticated then click authorize (already skipped this page with custom Passport client).
This is so far what I have tried
Registration api
public function first_register(Request $request)
{
$valid = validator([
'domain' => 'required|unique:domains',
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
if ($valid->fails()) {
$jsonError = response()->json($valid->errors()->all(), 400);
return response($jsonError, Response::HTTP_BAD_REQUEST);
}
User::create()......
if(! auth()->attempt(['email' => $request->email, 'password' => $request->password])){
return response()->json(['error' => 'UnAuthorised Access'], 401);
}
$client = Client::where('name', 'mobile-app')->first();
// create oauth redirect
$queries = http_build_query([
'client_id' => $client->id,
'redirect_uri' => 'http://'.$request->domain.'.localhost:3301/oauthcallback',
'response_type' => 'code',
'scope' => [],
'state' => (string) Str::orderedUuid(),
]);
return response([
'user' => $request->user(),
'url'=>
'http://'.$request->domain.'.localhost/oauth/authorize?' . $queries], Response::HTTP_OK);
}
The function above is working fine. The login page always will display after I clicked oauth/authorize url. Therefore, is there anywhere to skip this login page after register?
In addition, I also did try to use Auth::login() but still the same. Do I need to set session for Laravel passport API?
I have a Laravel 8 project that I want to send emails with. I develop it on macOS and I want to test it, but it does not work, but won't give any errors. (I replaced sensitive data with ********)
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=********#gmail.com
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=********#gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Note: I validated those settings, I also tested other working SMTP settings. So it's not a configuration issue in this file
mail.php
/// ...
'default' => env('MAIL_MAILER', 'mail'),
/// ...
'mailers' => [
'smtp' => [
'transport' => 'mail',
'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,
'auth_mode' => null,
],
/// ...
],
/// ...
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
resource_path('views/emails'),
],
],
/// ...
MailController.php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as Controller;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
public function testMail(Request $request)
{
$email = '********#live.com';
$data = [
'title' => 'Test mail'
];
Mail::send(['html' => 'emails.testMail'], $data,
function ($message) use ($email, $data) {
$message->to($email)
->subject($data['title']);
});
$response = [
'error' => 0,
'to' => $email,
'data' => $data,
];
return response()->json($response, 200);
}
}
Note: There is a blade template containing only HTML for test purposes at resources/views/emails/testMail.blade.php. I also added the appropriate GET-route.
When I access the endpoint in Postman, I get
{
"error": 0,
"to": "********#live.com",
"data": {
"title": "Test mail"
}
}
Little Snitch popped up, asking if I want to allow /usr/libexec/postfix/smtp to access smtp.gmail.com, so I know the call goes through. I gave the process all rights to access any server and retried, but the mail will never reach my destination.
Yes, I checked the SPAM folder. Yes, I checked other credentials. Yes I tested manual delivery between both accounts. Yes, I tested with Little Snitch disabled, entirely. Yes, I checked the laravel.log and it reveals nothing.
I noticed, that I won't get any error from Laravel when I put in wrong information in the .env file (except when I change MAIL_MAILER).
How can I get the output from the SMTP process to find out what the issue is? How can I fix the issue?
Trying to get socialite to work on my app. Facebook returns the The parameter app_id is required error.
Routes:
Route::get('/login/facebook', '\CommendMe\Http\Controllers\AuthController#redirectToProvider');
Route::get('/login/facebook/callback', '\CommendMe\Http\Controllers\AuthController#handleProviderCallback');
services.php:
'facebook' => [
'client_id' => env('426129694395672'),
'client_secret' => env('840fca14fc9fac4b592cd49f285c2ee9'),
'redirect' => 'http://localhost/login/facebook/callback',
],
AuthController.php
public function redirectToProvider() {
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback() {
$user = Socialite::driver('facebook')->user();
$user->name;
}
When trying the /login/facebook route, facebook returns this error.
Why is this happening?
Either use as
'client_id' => '426129694395672',
Or
'client_id' => env("FB_APP",'426129694395672'),
and use FB_APP = '426129694395672' in .env file
Instead
'client_id' => env('426129694395672'),
Using env('VarName') is to get value of environment variable named as VarName in .env file
Assuming that you have the following in your .env file:
CLIENT_ID=426129694395672
CLIENT_SECRET=840fca14fc9fac4b592cd49f285c2ee9
The facebook[] in your services.php should be like this:
'facebook' => [
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'redirect' => 'http://localhost/login/facebook/callback',
],
I write a game for football fans. So, I have to send similar mails to a group of people (not completely duplicated e-mail copies).
When I send the mails in a cycle - Yii framework sends the mails twice.
I suppose - it is because of the static variable Yii::$app.
Can someone give me a hint, please.
A code for example.
foreach ($oRace->user as $currUser) {
$htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
Yii::$app->mailer->compose()
->setFrom('info#example.com')
->setTo($currUser->mail)
->setSubject('Race "' . $raceName . '" has Started')
->setHtmlBody($htmlContent)
->send();
}
Thanks all in advance!
My Mailer config.
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'mail.example.eu',
'username' => 'support#example.com',
'password' => 'password',
'port' => '587',
'encryption' => 'TLS',
]
],
One more thing. The last mail in the cycle is never duplicated (only the last).
Another failed option.
Yii::$app->mailer->sendMultiple($allMails);
I recommend you to use CC or BCC option in the email instead of using foreach loop to send emails. I hope this will help someone.
$email = [];
foreach ($oRace->user as $currUser) {
$email[] = $currUser->mail;
}
$htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
Yii::$app->mailer->compose()
->setFrom('info#example.com')
->setCc($email) // If you're using Bcc use "setBcc()"
->setSubject('Race "' . $raceName . '" has Started')
->setHtmlBody($htmlContent)
->send();
From the provided code snippets, there are 3 possible reasons for that. Either:
$oRace->user contains every user twice
$currUser->mail contains the email twice like `email#example.com;email#example.com"
something is wrong inside the send function of SwiftMailer
After all - I have found that the issue was not with my Yii2 framework, but with my hosting mail server.
I have used https://github.com/ChangemakerStudios/Papercut for listening what my framework sends. It receives mails on port 25, while it listens for events on port 37804. It's a little bit confusing. Yii2 web.php simple configuration for local mail server is:
$config = [
'components' =>
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'localhost', // '127.0.0.1'
'port' => '25',
]
],
],
];
Thank of all, who have read my post!
I have an API I'm building with Lumen 5.3. For Authentication I'm using Laravel Passport (ported to Lumen via the dusterio/lumen-passport package).
All works well in Postman, and all tests pass fine with a single request.
However once I make a test that has multiple requests I get the error: 'Auth guard driver [api] is not defined'. The guard is defined in my auth config, and as I said works perfect outside of this test case.
Example test:
public function it_requires_users_password_when_updating_email(ApiTester $I)
{
$I->wantTo('Require password when updating email');
$user = factory(\App\User::class)->create();
$I->sendPOST('oauth/token', [
'grant_type' => 'password',
'client_id' => 1,
'client_secret' => env('OAUTH_SECRET'),
'username' => $user->email,
'password' => 'password',
'scope' => ''
]);
$token = $I->grabDataFromResponseByJsonPath('$.access_token')[0];
$I->amBearerAuthenticated($token);
$I->sendPUT('users/' . $user->id, ['email' => 'bender.rodriguez#planetexpress.com']);
$I->seeResponseCodeIs(422);
$I->seeRecord('users', array_only($user->toArray(), ['id', 'email']));
$I->dontSeeRecord('users', ['id' => $user->id, 'email' => 'bender.rodriguez#planetexpress.com']);
$I->sendPUT('users/' . $user->id, ['email' => 'bender.rodriguez#planetexpress.com', 'password' => 'password']);
$I->seeResponseCodeIs(200);
$I->seeRecord('users', ['id' => $user->id, 'email' => 'bender.rodriguez#planetexpress.com']);
}
The test passes fine if I remove the last 3 lines (everything from the 2nd sendPUT request), but once I include that, I get the error.
Any ideas?