20

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('foo@example.com', 'John Smith')->subject('This is my subject');
});
Jason Thuli
  • 736
  • 2
  • 8
  • 23

3 Answers3

41

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('foo@example.com', '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.

Community
  • 1
  • 1
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Great way to send plain text! Tanks – Gabriel Glauber Apr 18 '16 at 12:36
  • 1
    For the 1st solution, note that if you want to send HTML emails, you have to add another argument to the `setBody` function of "text/html" so that it becomes, for example, `$message->setBody('

    Hi, welcome user!

    ', 'text/html');`
    – trysis May 02 '16 at 17:44
13

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

Mail::raw('Message text', function($message) {
    $message->from('us@example.com', 'Laravel');
    $message->to('foo@example.com')->cc('bar@example.com');
});
Aron Balog
  • 574
  • 6
  • 9
  • 8
    This method is only made in laravel 5 and the original post said larave 4 is used in the project. – cytsunny May 19 '15 at 06:47
0

No, with out of the box Laravel Mail you will have to pass a view, even if it is empty. You would need to write your own mailer to enable that functionality.

TunaMaxx
  • 1,782
  • 12
  • 18