0
votes

I have a project on laravel 5.5 my email notification is working fine but I want the sender address to be picked from the database not hand-coded from notifiable.

public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->from('[email protected]', 'Example')
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

my mailtrap mail

scrrenshot

1
Simply make a variable and use it? Why would make the sender variable? If the sender is from another domain (different to your servers domain) then all emails will go to the spam folder. Why? Because your server is trying to send on behave on another servers name. Just imagine if someone who is logged in your website, would be accused of sending emails, which s/he didnt send? Dangerous territory. - Definitely not Rafal

1 Answers

0
votes

You can pass through the (dynamic) from address when you call the notify() function on $user object or via facade

use App\Notifications\DemoNotification;
use Illuminate\Support\Facades\Notification;

class SomeController extends Controller
{
    public function demo()
    {
        //$data = Some model object or anything
        //$fromAddress = Pick from database 

        Notification::send($users, new DemoNotification($data,$fromAddress));

        //Or $user = auth()->user();

        $user->notify(new DemoNotification($data,$fromAddress));
    }
}

Accept the fromAddress in constructor of DemoNotification class

class DemoNotification extends Notification
{

    use Queable;

    public $fromAddress;

    public $data;

    public function __construct($data, $fromAddress)
    {
        $this->data = $data;
        $this->fromAddress = $fromAddress;
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->from($this->fromAddress, 'Example')
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }
}