8

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('test@example.org');
});

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

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
  • Maybe it helps, but was looking for this as well, and Laravel mentions the message that is passed (both in queue and raw) is a SwiftMailer message object, and all methods can be used - I tried some. It Works. maybe you can just use the setBody method in the callback with anything you need the body to be. I needed to addPart to add an iCal invite to the mail. https://swiftmailer.symfony.com/docs/messages.html – morksinaanab Apr 08 '20 at 04:50

3 Answers3

9

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('test@example.org');
   });
});
Onbalt
  • 91
  • 1
  • 1
8

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 = "test@example.org";
    $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);
    }
}
Dennis Richter
  • 522
  • 6
  • 16
0

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');
        }
    );