Retrieve data from the logged-in user to use as the sender of the email in Laravel

Asked

Viewed 839 times

2

How can I make mail.php take the logged user data leaving the data of retemente dynamic. In the controller works, I can access the password of the logged in user. The problem is that in mail.php if I do not describe the email does not work.


Controller

  use Auth;
  use Mail;

  $email = Auth::user()->email;
  $username = Auth::user()->username;

  $emails = ["[email protected]", "[email protected]"];
  $copias = ["[email protected]"];

  $beautyemail = app()->make(\Snowfire\Beautymail\Beautymail::class);

  $beautyemail->send('dashboard.templateemail', [
    'email' => $template, 
    'first_name' => $first_name, 
    'last_name' => $last_name,
    'id' => $id, 'cargo' => $cargo, 
    'phone' => $phone, 
    'ramal' => $ramal, 
    'finalizacaodoemail' => $finalizacaodoemail 
  ],  function($m) use ($emails,  $assunto, $copias, $template, $finalizacaodoemail, $email, $username) {    
       $m->to($emails)->subject($assunto)->from($email, $username)->cc($copias);    
  });

Mail.php

'username' => "[email protected]",

'password' => "senha123",
  • Wouldn’t be the Configuring the Sender described in the documentation?

  • You use some Controller to send?

  • @Darleifernandozillmer yes, use, updated the question with the controller code, and mail.php, what happens is that if the user’s meial is not equal to what is in maisl.php an authentication error. What I wanted to do was in mail.php already trying to get email and password from the logged-in user.

  • First thing mail.php is the fixed configuration file inside the folder config?

4 answers

3


To send the email using a dynamic sender, you need to use the helper config Laravel to change settings at runtime.

Configuration - Laravel

$email = Auth::user()->email;
$username = Auth::user()->username;

$emails = ["[email protected]", "[email protected]"];
$copias = ["[email protected]"];

config()->set([
    'mail.host' => $hostEmail, //Verificar o host do e-mail remetente
    'mail.port' => $hostPort,  //Verificar a porta usada pelo host para envio de e-mail
    'mail.encryption' => $hostEncry, //Verificar encriptação
    'mail.username' => $email, 
    'mail.password' => $password, //Senha do e-mail do remetente descriptografada
    'mail.from' => [ 
        'address' => $email, 
        'name' => $username, 
    ]
]);
(new MailServiceProvider(app()))->register();

$beautyemail = app()->make(\Snowfire\Beautymail\Beautymail::class);

$beautyemail->send('dashboard.templateemail', [
    'email' => $template, 
    'first_name' => $first_name, 
    'last_name' => $last_name,
    'id' => $id, 'cargo' => $cargo, 
    'phone' => $phone, 
    'ramal' => $ramal, 
    'finalizacaodoemail' => $finalizacaodoemail 
], function($m) use ($emails,  $assunto, $copias, $template, $finalizacaodoemail, $email, $username) {    
       $m->to($emails)->subject($assunto)->from($email, $username)->cc($copias);    
});

It may be necessary to clear the project cache:

php artisan cache:clear

  • I updated the question, I think it’s better to understand

  • But why do you want to directly change the configuration file (config/mail.php)? I ask this because I don’t understand the error that is happening.

  • Because the controller always picks up the sender that is there in the configuration file

  • I could not fit this part of your suggestion into my code correctly. new Mailshipped($params)

  • To use my suggestion you need to create the Mailable class as I wrote. If you want help we can start a chat.

  • I created the class, yes we can!

  • Can you help me? I still can’t solve this problem.

  • You can call me on chat

  • just called...

Show 4 more comments

1

You can use this package https://github.com/ImLiam/laravel-env-set-command and then use the Facade Artisan to execute a command that changes the value of the environment variable.

Artisan::call('php artisan env:set username ' . $user_do_email)

Another solution is to change the physical file itself but that would be a lot of work and would open doors to several failures.

1

First remembering that you need to "include the features" of Auth at the beginning of Controller (I imagine you’re sending it through a).

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;

use Auth;

class ExemploController extends Controller
...

Then you can use any field referring to the logged in user:

public function enviaEmail()
{
    $data = [
        'email'   => Auth::user()->email, // lembrando que esse campo pode variar dependendo da sua estrutura na tabela
        'subject' => $request->input('subject'),
        'body'    => $request->input('body')
    ];

    Mail::send('emails.suporte', $data, function($message) use ($data)
    {
        $message->from($data['email']);
        $message->to('[email protected]','Mohammed');
        $message->subject($data['subject']);
    });

    return redirect('algumLugar');
}
  • 1

    give a look I updated the question

-4

  • but this in mail.php itself?

  • I tried using Auth::user(); but gave error

  • Are you using Laravel Authentication? What error?

  • Leonardo apparently she needs to change the authentication data to SEND email and not set the from which is just an attribute of the email itself

  • I understood that it was the sender according to the user logged in to the system

  • I understand she wants to change the sender too...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.