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('hr@example.com', 'Example')
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
my mailtrap mail
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!');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.