How to get laravel transport email username with failover - laravel

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

Related

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

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,
],
...
...
...
];

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'),
],
],

Import databases infos from mysql in database.php [duplicate]

This question already has answers here:
Laravel: connect to databases dynamically
(8 answers)
Closed 3 years ago.
I would like to get databases informations stored in my database and add it in the database.php.
I tried to get all infos in the database but I always have an error like this :
[2019-04-18 20:22:20] laravel.ERROR: Call to a member function
connection() on null {"exception":"[object]
(Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Call to a member function connection() on null at
/var/www/vhosts/xxxx.net/httpdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1249)
I added this on the top of database.php :
<?php
use App\Campaign;
$database_infos = Campaign::all();
$campains_db = array();
foreach ($database_infos as $info) {
if (isset($info->db_name)) {
$campains_db[$info->keyword] = array(
'driver' => 'mysql',
'host' => $info->db_host,
'database' => $info->db_name,
'username' => $info->db_user,
'password' => decrypt($info->db_password),
'prefix' => $info->db_prefix,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => false,
'engine' => null,
);
}
}
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
$campains_db,
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'wordpress' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
I found this on an other post :
The simplest solution is to set your database config at runtime.
Laravel might expect these settings to be loaded from the
config/database.php file, but that doesn't mean you can't set or
change them later on.
The config loaded from config/database.php is stored as database in
Laravel config. Meaning, the connections array inside
config/database.php is stored at database.connections.
Config::set("database.connections.mysql", [
"host" => "...",
"database" => "...",
"username" => "...",
"password" => "..."
]);

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'),
],
],
];

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