Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Send a simple notification email to myself with Laravel

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading