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

3

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('[email protected]', 'John Smith')->subject('This is my subject');
});
Meliorism answered 19/8, 2014 at 23:18 Comment(0)
S
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('[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.

Sanitize answered 19/8, 2014 at 23:38 Comment(2)
Great way to send plain text! TanksMaui
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('<p>Hi, welcome user!</p>', 'text/html');Shiny
W
13

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]');
});
Wavelet answered 25/4, 2015 at 16:28 Comment(1)
This method is only made in laravel 5 and the original post said larave 4 is used in the project.Revealment
I
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.

Incondensable answered 19/8, 2014 at 23:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.