Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send an email with Laravel 4 without using a view?

I'm developing a site using Laravel 4 and would like to send myself ad-hoc emails during testing, but it seems like the only way to send emails is to go through a view.

Is it possible to do something like this?

Mail::queue('This is the body of my email', $data, function($message)
{
    $message->to('[email protected]', 'John Smith')->subject('This is my subject');
});
like image 289
Jason Thuli Avatar asked Aug 19 '14 23:08

Jason Thuli


People also ask

Can we send mail from localhost in Laravel?

In the same way, you can also set up other Mail providers like Mailgun, Sendgrid, Mandrill, Mailchimp, etc. to send the mail from Localhost in Laravel.

Can we use PHP mail function in Laravel?

Laravel provides drivers for SMTP, Mailgun, Mandrill, SparkPost, Amazon SES, PHP's mail function, and sendmail , allowing you to quickly get started sending mail through a local or cloud based service of your choice.


2 Answers

As mentioned in an answer on Laravel mail: pass string instead of view, you can do this (code copied verbatim from Jarek's answer):

Mail::send([], [], function ($message) {
  $message->to(..)
    ->subject(..)
    // here comes what you want
    ->setBody('Hi, welcome user!');
});

You can also use an empty view, by putting this into app/views/email/blank.blade.php

{{{ $msg }}}

And nothing else. Then you code

Mail::queue('email.blank', array('msg' => 'This is the body of my email'), function($message)
{
    $message->to('[email protected]', 'John Smith')->subject('This is my subject');
});

And this allows you to send custom blank emails from different parts of your application without having to create different views for each one.

like image 82
Laurence Avatar answered Sep 29 '22 18:09

Laurence


If you want to send just text, you can use included method:

Mail::raw('Message text', function($message) {
    $message->from('[email protected]', 'Laravel');
    $message->to('[email protected]')->cc('[email protected]');
});
like image 13
Aron Balog Avatar answered Sep 29 '22 18:09

Aron Balog