Is it possible to set the mailer within a laravel mailable? - laravel

I have a mailable, and I'd like it to always use a specific mailer. Currently, I send the mail like this:
Mail::mailer('smtp2')->to($recipient)->send(new AuthTokenMail());
What I'd like to do is set the mailer within the mailable, the same way I set the queue, like this:
class AuthTokenMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public function __construct(int $code)
{
$this->onQueue('email2');
$this->mailer('smtp2'); // doesn't work!
}
...

for you to be able to use the specific mail details you need to set it up in your mail.php config file. like this:
'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,
],
'smtp2' => [
'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,
],
...
...
...
];

Related

How to get laravel transport email username with failover

i have setup 2 smtp mailers:
'smtp' => [
'transport' => 'smtp',
'host' => smtp.gmail.org,
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => 'smtp1#gmail.com',
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
],
'smtp-1' => [
'transport' => 'smtp',
'host' => smtp.gmail.org,
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => 'smtp2#gmail.com',
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'local_domain' => env('MAIL_EHLO_DOMAIN'),
],
and set failover:
'failover' => [
'transport' => 'failover',
'mailers' => [
'smtp'
'smtp-1'
],
],
the setup was was run fine.
$transport = $Mail::raw($message, function($msg) use ($to, $subject) {
$msg->to($to)->subject($subject);
});
the problem how can i get the actual sender mailer, since it can failed and swith to other mailer?
i try to get the return path with:
$header = $transport->getOriginalMessage();
$sender = $header->getReturnPath();
dd($sender);
but it return null;
i expect to get either smtp1#gmail.com or smtp2#gmail.com

How to set second mail env configurations and make use of it?

I have this env variables:
MAIL_MAILER=smtp
MAIL_HOST=smtp.zoho.com
MAIL_PORT=587
MAIL_USERNAME=accounts#mozcoders.com
MAIL_PASSWORD=xxxxxxxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=accounts#mozcoders.com
MAIL_FROM_NAME="${APP_NAME}"
INFO_MAIL_MAILER=smtp
INFO_MAIL_HOST=smtp.zoho.com
INFO_MAIL_PORT=587
INFO_MAIL_USERNAME=info#mozcoders.com
INFO_MAIL_PASSWORD=xxxxxxxxxx
INFO_MAIL_ENCRYPTION=tls
INFO_MAIL_FROM_ADDRESS=info#mozcoders.com
And this mail 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,
],
'info' => [
'transport' => 'smtp',
'host' => env('INFO_MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('INFO_MAIL_PORT', 587),
'encryption' => env('INFO_MAIL_ENCRYPTION', 'tls'),
'username' => env('INFO_MAIL_USERNAME'),
'password' => env('INFO_MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
When I send email using the default configurations it works fine, but when I use the custom configurations it returns an error:
local.ERROR: Expected response code 250 but got code "553", with message "553 Relaying disallowed as default#mail.com
" {"userId":1,"exception":"[object] (Swift_TransportException(code: 553): Expected response code 250 but got code \"553\", with message \"553 Relaying disallowed as default#mail.com
\"
Here is the code from my Notification file:
return (new MailMessage)
->mailer('info')
->subject('Some subject')
->markdown('mail.failure', ['data' => $this->failureData]);
I was able to achieve what i wanted by moving this array:
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
to desired mailers array:
...
'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,
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
],
'info' => [
'transport' => 'smtp',
'host' => env('INFO_MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('INFO_MAIL_PORT', 587),
'encryption' => env('INFO_MAIL_ENCRYPTION', 'tls'),
'username' => env('INFO_MAIL_USERNAME'),
'password' => env('INFO_MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
'from' => [
'address' => env('INFO_MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
],

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required "

I got stuck on this error. I am trying to configure SMTP mail on laravel.
I have this issue when I want to send the password reset link:-
I found the reference but I have make change on 'host' and 'port'
'host' => env('MAIL_HOST', 'mailtrap.io'),
'port' => env('MAIL_PORT', 2525),
but I still have the same issue .
My config/mail.php:
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mailtrap.io'),
'port' => env('MAIL_PORT', 2525),
'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'),
],
],
];

Laravel 5.5 and mailgun

I have been trying to send emails from my laravel app with mailgun but cant get it to work.
.env
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=(Default SMTP Login from mailgun)
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS= hello#****.*****.me
MAIL_FROM_NAME= Me
MAILGUN_DOMAIN=*****.*****.me
MAILGUN_SECRET=key-10*****************************f
mail.php
return [
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', '*****.******.me'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#*****.******.me'),
'name' => env('MAIL_FROM_NAME', 'Me'),
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('postmaster#*****.******.me'),
'password' => env('************'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
services.php
'mailgun' => [
'domain' => env('*****.******.me'),
'secret' => env('key-1******************************f'),
],
Using this url to test
Route::get('/send_test_email', function(){
Mail::raw('Sending emails with Mailgun and Laravel is easy!', function($message)
{
$message->to('finchy70#gmail.com');
});
});
No errors but no emails sent. Also no activity on my mailgun dashboard.
All my mailgun DNS settings are verified for my domain.
Can anyone see what I'm doing wrong?
Looks like you're using the value instead of the name in the config for username, password, domain, and secret. Should be
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
And
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],

sending email from laravel using the mailgun can't set the FROM properly

This is the way which I send the email:
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
$message->attach($pathToFile);
});
This is my mail configurations:
return [
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
// 'from' => ['address' => '', 'name' => ''], //
'encryption' => 'tls',
'username' => env('****'),
'password' => env('****'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
This is my services configurations:
'mailgun' => [
'domain' => '****',
'secret' => '****',
],
When I get the email I see FROM is set to(None#mailgun.org). I would like to use different FROM based on different conditions, I don't want to hard code the FROM in the mail configurations. Please help.
The only potential problem I can spot, seems to be that you have commented out from in your mail configurations
'port' => env('MAIL_PORT', 587),
// 'from' => ['address' => '', 'name' => ''], //
'encryption' => 'tls',
This could be causing the problem with ->from not correctly overriding, as it might be dependent on it.

Resources