rap-2-h
rap-2-h

Reputation: 31998

Send raw mail via queue Laravel

For testing purpose, I want to send raw mail via Queue.

I can send a raw mail like this:

Mail::raw('bonjour', function($message) {
   $message->subject('Email de test')
           ->to('[email protected]');
});

But is there a way to send a raw mail via Queue (without creating a View nor Mailable)?

Upvotes: 9

Views: 5678

Answers (3)

Leave the paramaters view and variables with an empty array each one and add the line $mail->setBody($html, 'text/html') inside the function.

Mail::queueOn(
        'name_of_queue',
        [],
        [],
        function($mail) use ($destination_email, $destination_name, $html) {
            $mail->to($destination_email, $destination_name);
            $mail->subject($subject);
            $mail->setBody($html, 'text/html');
        }
    );

Upvotes: 0

I searched the past days without any outcome to accomplish exact this: a raw mail that can be queued.

Unfortunately I didn't found a solution without using Mailables and Views.

I assume you have the same reason as me: You want to send a 100% dynamically generated Mail from a string.

My solution was:

  1. creating a view that only contains one variable: <?php echo $content;
  2. create a mailable, passing the content to the constructor and set it to $this->content
  3. copy everything inside the old mail-closure into the build-method of the mailable and replace every $message-> with $this
  4. queue it ;)

public function send(Request $request) {
    $to = "[email protected]";
    $subject = "email de test";
    $content = "bonjour";
    Mail::send(new RawMailable($to, $subject, $content));
}

view (/ressources/view/emails/raw.blade.php):

{!! $content !!}

mailable:

<?php

namespace App\Mail;

use Dingo\Api\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class RawMailable extends Mailable
{
    use Queueable, SerializesModels, ShouldQueue;

    private $mailTo;
    private $mailSubject;
    // the values that shouldnt appear in the mail should be private

    public $content;
    // public properties are accessible from the view

    /**
     * Create a new message instance.
     *
     * @param LayoutMailRawRequest $request
     */
    public function __construct($to, $subject, $content)
    {
        $this->content = $content;
        $this->mailSubject = $subject;
        $this->mailTo = $to;
    }

    /**
     * Build the message.
     *
     * @throws \Exception
     */
    public function build()
    {
         $this->view('emails.raw');

         $this->subject($this->mailSubject)
              ->to($this->mailTo);
    }
}

Upvotes: 8

Onbalt
Onbalt

Reputation: 101

You can use dispatch helper function to push Closure onto the Laravel job queue:

dispatch(function () use ($name) {
   Mail::raw('bonjour ' . $name, function($message) {
      $message->subject('Email de test')
              ->to('[email protected]');
   });
});

Upvotes: 10

Related Questions