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)?
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 = "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);
}
}
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');
});
});
Basically laravel uses blades for cleaner rendering.
If you want to fire a mail on queue as you know:
Mail::queue('emails.welcome', $data, function ($message) {
//
});
Now if we take a look at the MailQueue.php
queue()
function :
/**
* Queue a new e-mail message for sending.
*
* @param string|array $view
* @param array $data
* @param \Closure|string $callback
* @param string $queue
* @return mixed
*/
public function queue($view, array $data, $callback, $queue = null);
So $view
it can be either string or array. So you could try:
Mail::queue('string|| Array', $data, function ($message) {
//
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.