Reputation: 31998
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
Reputation: 695
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
Reputation: 522
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:
<?php echo $content;
$this->content
$message->
with $this
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
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