Carlos Goce
Carlos Goce

Reputation: 1665

Sending e-mail in Laravel with custom HTML

I need to send e-mails but I already have the HTML generated, I don't want to use laravel blade because I need to apply a CSS Inliner to the HTML, so this is how i generate the html:

getRenderedView($viewData) {
    //code
    $html =  View::make('email.notification', $viewData)->render();
    $this->cssInliner->setCSS($this->getCssForNotificationEmail());
    $this->cssInliner->setHTML($html);
    return $this->cssInliner->convert();
}

So, to send the mail with Laravel you usually do something like this:

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'John Smith')->subject('Welcome!');
});

But I don't want to pass a view, I already have the html, how can I do that?

Upvotes: 5

Views: 17918

Answers (5)

Sachin Aghera
Sachin Aghera

Reputation: 506

In your controller:

Mail::to("xyz.gmail.com")->send(new contactMailAdmin($userData));

Upvotes: 1

Kenan Tumer
Kenan Tumer

Reputation: 61

I want to share a tip which might help sending emails without "blade".

Laravel Mail function is actually a wrapper for Swift. Just assign empty arrays to $template and $data, I mean the first 2 parameters of the function, then do the rest inside callback.

Mail::send([], [], function($message) use($to, $title, $email_body)
{
    $message->setBody($email_body)->to($to)->subject($title);
});

Upvotes: 6

Ketan Akbari
Ketan Akbari

Reputation: 11257

$data = array( 'email' => '[email protected]', 'first_name' => 'Laravel', 
        'from' => '[email protected]', 'from_name' => 'learming' );

Mail::send( 'email.welcome', $data, function( $message ) use ($data)
{
 $message->to( $data['email'] )->from( $data['from'], 
 $data['first_name'] )->subject( 'Welcome!' );
 });

Upvotes: -1

Christopher Pecoraro
Christopher Pecoraro

Reputation: 136

The body is not accessible from the closure.

You can create the swift message by hand:

    $message = \Swift_Message::newInstance();
    $message->setFrom($messageToParse->template['fromEmail']);
    $message->setTo($messageToParse->to['email']);
    $message->setBody($messageToParse->body);
    $message->addPart($messageToParse->body, 'html contents');
    $message->setSubject('subject');

But then you to need to create the transport:

    $mailer = self::setMailer( [your transport] );
    $response =  $mailer->send($message);

Upvotes: 1

fossman83
fossman83

Reputation: 347

If I'm right in what you want to achieve, to get round this I created a view called echo.php and inside that just echo $html.

Assign your html to something like $data['html'].

Then below pass your $data['html'] to the echo view.

Mail::send('emails.echo', $data, function($message) { $message->to('[email protected]', 'John Smith')->subject('Welcome!'); });

Let me know how you get on.

Upvotes: 14

Related Questions