Send a simple notification email to myself with Laravel

Advertisements

I have a Laravel web app where I’d like to be notified when users take a certain rare action. I’ve already set up logging for this, but logs are written quietly to a file and I’d like something that notifies me to check the logs – ideally an automated email that I can trigger within the app to go to my Zoho-hosted email (so Outlook/Gmail spam filters shouldn’t be a problem). The problem is that Laravel doesn’t seem to have any simple way to send a quick plain-text email on the fly, without creating a whole class, view and possibly controller to go with it.

Is this really not possible, or am I missing something? The web app is hosted on a DigitalOcean droplet so setting it up for something like sendmail shouldn’t be a problem.

>Solution :

You can use Mail::raw to send a plain, classless/viewless message.

Mail::raw('Hello world', function (Message $message) {
    $message->to('example@gmail.com')
        ->from('example@example.com');
});

If you want a more complicated email, you might consider Mail::plain, which will accept a view, bindings, but doesn’t need a Mailable class.

Personally, though, I prefer a real mailable for any programmatic email, even if it’s rare. There’s no downside to having class/views for it; they’re only loaded if necessary, and having them in the mail folders makes it much clearer what mails your app might possibly send.

Leave a Reply Cancel reply