Unable to use SMTP Gmail server to send email authentications - php

I am trying to send User email verification. I have updated the env and mail configuration to suit my Google Mail. However, I am encountering a Swift_TransportException (530) error.
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=okaydots#gmail.com
MAIL_PASSWORD=
mail.php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'okaydots#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Kyle Jeynes'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('okaydots#gmail'),
'password' => env(''),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
];
User.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController#index')->name('home');
HomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('verified');
}
public function index()
{
return view('home');
}
}
This is the exact error message I am receiving:
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 133sm47765wme.9 - gsmtp "
After googling this error, I was told to Turn on less secure apps. After doing so, I still receive the same error. How can I allow Laravel to send email verification / emails?

Please revert back your mail.php. You only need to change your .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=okaydots#gmail.com
MAIL_PASSWORD= #use gmail app password not your gmail password
MAIL_ENCRYPTION=tls

After intensive Googling, I found that the mail.php contains the env() method for the configuration, and therefore expects arg 1 to be the configuration name inside the .env file.
I changed my mail.php to this and now it works:
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'okaydots#gmail.com'),
'name' => env('MAIL_FROM_NAME', 'Kyle Jeynes'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'), # Changed
'password' => env('MAIL_PASSWORD'), # Changed
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
];

Related

Mailgun emails not sending from Laravel 8 Valet app

Here's my relevant .env:
MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=https://api.mailgun.net/v3/mail.example.com
MAILGUN_SECRET=fb...a1
Note: I use example.com as an example above, but I've put my actual domain name there. I do not get any errors from the Laravel app, and I do not see anything in the logs on the Mailgun dashboard. My domain is verified. fb...a1 is also the redacted API code, I of course use my full API code I get from the mailgun dashboard.
config/mail.php:
<?php
return [
'default' => env('MAIL_MAILER', 'mailgun'),
'mailers' => [
'mailgun' => [
'transport' => 'mailgun',
],
],
];
config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
],
In my controller I have:
$email = $validated['email']; // I've verified this is my actual email
Mail::to($email)->send(new OrderCreated());
app/Mail/OrderCreated.php:
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class OrderCreated extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this
->from('no-reply#example.com')
->markdown('emails.order-created');
}
}
And finally, resources/views/emails/order-created.blade.php:
#component('mail::message')
# Order Confirmation
This email is test.
#endcomponent
I'm using Laravel 8.14.0 and Valet 2.13.0, so I'm testing it locally with an https://my-app.test domain. The app is using InertiaJS, in case that makes any difference. The controller code runs without error, but I see no logs on my mailgun dashboard and the email never arrives in my inbox. I have no idea what's wrong or how to debug this.
UPDATE:
I've noticed if I set MAILGUN_DOMAIN and MAILGUN_SECRET to null, I get the same behaviour as above. If I set MAILGUN_DOMAIN to a nonsense value like abcd I get the following error:
GuzzleHttp\Exception\ClientException
Client error: `POST https://api.mailgun.net/v3/abcd/messages.mime` resulted in a `401 UNAUTHORIZED` response: Forbidden
And if I set MAILGUN_SECRET to abcd it works as originally described (no error, but also no email).
These days I've worked with mailgun. It was on Laravel 7, and it worked perfectly. I dont think that it will cause a problem for v8.
Actually I've sent emails from controller, but you can implement the same for you as you want.
So I'll share my experience anyway.
.env
#MAIL_DRIVER=mailgun
MAIL_MAILER=mailgun
MAIL_HOST="smtp.mailgun.org"
MAIL_PORT=587
MAIL_USERNAME="postmaster#sandbox********************************.mailgun.org"
MAIL_PASSWORD="123456"
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN="sandbox********************************.mailgun.org"
MAILGUN_SECRET="key-********************************"
MAIL_FROM_NAME="ProjectName"
MAIL_FROM_ADDRESS="no-reply#yourfuturesite.com"
MAIL_ENV=test
MAIL_TEST="recipient#yourfuturesite.com"
#MAIL_LOG_CHANNEL
#MAILGUN_ENDPOINT="api.eu.mailgun.net"
config/mail.php
<?php
return [
'default' => env('MAIL_MAILER'),
'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,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
'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,
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS'),
'name' => env('MAIL_FROM_NAME'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
// CUSTOM CONFIGS
'mail_env' => env('MAIL_ENV'), // 'local' for testing via mailgun, 'production' for all mails
'mail_test' => env('MAIL_TEST'), // test email for 'local' testing
// ADDITIONAL UNNECESSARY CONFIGS
// 'sendmail' => '/usr/sbin/sendmail -bs',
// 'sendmail' => '/usr/sbin/sendmail -t-i',
// 'pretend' => false,
// 'log_channel' => env('MAIL_LOG_CHANNEL'),
// 'pretend' => false,
];
config/services.php (the same as you have)
Controller
try {
$name = array_key_exists('first_name', $email_data) ? $email_data['first_name'] : $email_data['name'];
Mail::send('emails.confirm-registration', [
'role' => $email_data['role'],
'name' => $name,
'email' => $email_data['email'],
'confirm_registration' => route('front.auth.confirm_registration', ['registration_token' => $email_data['registration_token']]),
], function ($message) use ($email_data, $name) {
$to = (config('mail.mail_env') == 'prod' || config('mail.mail_env') == 'production') ? $email_data['email'] : config('mail.mail_test');
$message
->subject(config('app.name') . ": Email Confirmation")
->from(config('mail.from.address'), config('mail.from.name'))
->to($to, $name);
});
return true;
}
catch(\Exception $e) {
// TODO: report all the "$e->message"s like this
return false;
}
resources/views/emails/confirm-registration.php
<a href="{{ route('front.main') }}" target="_blank">
<img label="logo" alt="{{ config('app.name') }}"
src="{{ $message->embed(public_path('images/logo.svg')) }}" width="128" height="96">
</a>
<p>{{ $name }}</p>
<p>{{ $confirm_registration }}</p>
<p>© {{ date("Y") == 2020 ? '2020' : '2020-' . date("Y") }} {{ config('app.name') }}</p>
This is just my example, so there you may need to replace as you want.
Just pay attention to "config/mail.php" file. I think there you need to set some additional props. (Also don't forget to reload your cache after these last config changes: "php artisan config:cache")

I am getting this error while implementing Mail in Laravel 5.5---->Missing argument 1 for Illuminate\Support\Manager::createDriver()

Error - Missing argument 1 for Illuminate\Support\Manager::createDriver()
My env file-
MAIL_DRIVER=smtp
MAIL_HOST=smtp.****.com
MAIL_PORT=587
MAIL_USERNAME=info#*****.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls
All the keys in mail.php of config folder are correct.
My controller function looks like this
function send(Request $request)
{
$this->validate($request,[
'name'=>'required',
'email'=>'required|email',
'subject'=>'required',
'message'=>'min:10'
]);
$data=array(
'name'=>$request->name,
'email'=>$request->email,
'subject'=>$request->subject,
'bodyMessage'=>$request->message
);
Mail::send('mail.admin',$data,function($message) use ($data)
{
$message->from($data['email']);
$message->to($data['info#****.com']);
$message->subject($data['subject']);
});
}
What mistake am i making here?
Here is my mail.php file
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.****.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs', 'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
I replaced all the settings and codes with my own codes and the email was sent correctly. I suggest removing the composer.lock file and remove the vendor directory and run the composer install again. Maybe the complete package is not installed.

Mailgun emails not going out

I'm creating a verification email, however the mail isn't going out and I don't receive any errors.
My call to send the email:
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
Mail::to($user->email)
->queue(new VerifyEmail($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
here's my email build function:
public function build()
{
return $this->view('emails.account.verify_email')
->with([
'id' => $this->user->id,
'firstname' => $this->user->firstname,
'token' => $this->user->email_verification_token,
]);
}
I installed guzzlehttp/guzzle
and changed my files:
ENV (not sure of the port setting)
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandbox...655.mailgun.org
MAIL_PASSWORD=Default mailgun sandbox Password
MAIL_ENCRYPTION=tls
config/services
'mailgun' => [
'domain' => env('sandbox...655.mailgun.org'),
'secret' => env('key-...'),
],
config/mail
<?php
return [
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
I don't receive any errors, however there are no outgoing mails when I check the mailgun dashboard
Your services config file is wrong:
'mailgun' => [
'domain' => env('sandbox...655.mailgun.org'),
'secret' => env('key-...'),
],
You're trying to look up the values from the .env file here. Should reference the keys, not the values. For instance:
'mailgun' => [
'domain' => env('MAIL_DOMAIN'),
'secret' => env('MAIL_SECRET'),
],
And then add them to your .env file:
MAIL_DOMAIN=sandbox...655.mailgun.org
MAIL_KEY=key-...
I'm assuming the rest is correct but I don't know for certain. :)

How to achieve password reset functionality in laravel 5.4

I implemented laravel 5.4 auth login. My other functionality is in working like login, logout etc. But for password reset functionality I am receiving following error. I am confused where I am wrong.
Swift_TransportException in StreamBuffer.php line 268: Connection
could not be established with host smtp.thenaturalgolfcourse.com
[php_network_getaddresses: getaddrinfo failed: Name or service not
known #0]
Below is my .env file detail is reqired for mail setting.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mysite.com
MAIL_PORT=465
MAIL_USERNAME=support#mysite.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
same details I used in mail.php like below
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mysite.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'support#mysite.com'),
'name' => env('MAIL_FROM_NAME', 'The My Site'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME','support#mysite.com'),
'password' => env('MAIL_PASSWORD','mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Please give me solution or tell me where I am wrong..

Laravel 5.4 SMTP error "send AUTH command first"

I'm getting the following error trying to send mail from localhost using smtp:
Expected response code 250 but got code "503", with message "503 5.5.4
Error: send AUTH command first. "
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#domain.com
MAIL_PASSWORD=11111111
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#domain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => 'robot#domain.com',
'name' => 'MY.NAME',
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('robot#domain.com'),
'password' => env('11111111'),
'sendmail' => '/usr/sbin/sendmail -bs',
];
Tried: changing ports, encryption, clearing cache, restarting server in all possible combinations. :)
As I see it there's one more parameter I need to pass to the mailer library. Some thing like
auth_mode=login_first
Can this be done through laravel settings?
I'm posting my working settings. You've got to check how laravel env helper function is used in your config file. Also when using smtp.yandex.com auth email and form email must match.
Laravel Docs for env()
The env function gets the value of an environment variable or returns a default value:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.yandex.com
MAIL_PORT=465
MAIL_USERNAME=robot#mydomain.com
MAIL_PASSWORD=123123123
MAIL_ENCRYPTION=ssl
MAIL_FROM=robot#mydomain.com
MAIL_NAME=MY.NAME
config/mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.yandex.com'),
'port' => env('MAIL_PORT', 465),
'from' => [
'address' => env('MAIL_FROM','robot#mydomain.com'),
'name' => env('MAIL_NAME','MY.NAME'),
],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('MAIL_USERNAME','robot#mydomain.com'),
'password' => env('MAIL_PASSWORD','123123123'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Controller function
public function testmail()
{
$user = Auth::user();
$pathToLogo = config('app.url').'/images/logo/logo_250.png';
Mail::send('emails.testmail', array('user' => $user, 'pathToLogo' => $pathToLogo), function($message) use ($user)
{
$message->to($user->email);
$message->subject('Test message');
});
return redirect()->route('home')->with('message','Test message sent.');
}

Categories