Hello I try send email from my registration script to user (link). I have config file:
config/mail.php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 's44.linuxpl.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'verify#coins.webmg.pl'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('verify#coins.webmg.pl'),
'password' => env('MySecretPassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'stream' => [
'tls' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]
];
And file .env:
MAIL_DRIVER=smtp
MAIL_HOST=s44.linuxpl.com
MAIL_PORT=587
MAIL_USERNAME=verify#coins.webmg.pl
MAIL_PASSWORD=MySecretPassword
MAIL_ENCRYPTION=tls
Script to send mail from RegisterController.php
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
$sendMail = Mail::to($user->email)->send(new VerifyMail($user));
return $user;
}
Class VerifyMail
class VerifyMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->view('emails.verifyUser');
}
}
And file to send verifyUser.blade.php
<h2>Welcome to the site {{$user['name']}}</h2>
I don't know what is wrong in this configuration, because I can log in correctly to the mailbox, so the email address and password are correct. Additionally, laravel does not return any errors, the script itself is executed correctly.
Restore your mail.php file to its defaults:
Replace:
'username' => env('verify#coins.webmg.pl'),
'password' => env('MySecretPassword'),
with
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
You misunderstood how the env($key, $default = null) method works. The first argument it takes is the key of the environment variable (eg. 'MAIL_USERNAME'), and the second argument is the default value (which can be optional).
Store your mail credentials in the .env file only, and never within your config files' env() calls.
Laravel Docs: Configuration
I found solution. The problem is with file .env. The file was loaded with previous settings. It was enough to clean the cache files.
php artisan config:cache
php artisan config:clear
php artisan cache:clear
Here it is well described Trying to get Laravel 5 email to work
just restore your config/mail.php to default one and try to set the below values in your laravel .env file directly
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=abc#gmail.com
MAIL_PASSWORD=abc#123
May wish this will help you Thanks
Related
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")
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.
I have just moved my laravel 5.5 website to a server (digitalocean, debian9). For some reason emails wont get sent from my server and I'm not getting any errors.
.env:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAILGUN_DOMAIN=MYDOMAIN
MAILGUN_SECRET=key-SECRET_KEY
MAIL_USERNAME=MYUSERNAME
MAIL_PASSWORD=MYPASSWORD
MAIL_ENCRYPTION=tls
config/mail.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', 'MY_EMAIL'),
'name' => env('MAIL_FROM_NAME', 'NAME'),
],
'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'),
],
],
];
config/services.php:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
send mail controller:
public function sendMail( $id, Request $request )
{
$user = User::find($id);
$data = [
'subject' => $request->subject,
'bodyMessage' => $request->message,
'image' => $request->image,
];
Mail::to($user->email)
->send(new test($data));
Session::flash('email', 'Message !');
return back();
}
I looked through my UFW firewall and i couldn't see anything related to mail getting blocked.
My email Service (mailgun) doesn't output anything in the logs when i try to send a mail. So I guess its not even reaching them.
I have no idea what to do at this point. I don't even know where I should look for the problem, is it a server problem? laravel problem? firewall problem?...
Would really appreciate some help!
Well, you can check several things :
• Is your production .env correct ?
• Have you make a composer-install recently ?
• Are you sure the php versions of your dev and prod environnement are the same ? because Swift Mailer requires PHP 7.0 or higher (proc_* functions must be available).
I have no idea why it didn't work, feels like i tried everything. Ended up using PHPMailer instead, which worked fine.
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. :)
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.');
}