0
votes

I am working on jobs (queue) with Laravel 5.7. I have scheduled emails for every weekends into jobs table. For email scheduler I have stored all email information like From, to , reply-to email address along with body of the email into jobs table.

Here I have added by Job class

use Dispatchable,
    InteractsWithQueue,
    Queueable,
    SerializesModels;

protected $details;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct() {

    $this->details = $details;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle() {
    //
    $payload = json_decode($event->job->getRawBody());
    $data = unserialize($payload->data->command);
    echo $data;
    exit;
    Mail::send(
            ['html' => 'emails.templates'], array('body' => $body, 'title' => $post_data['subject']), function($message) use ($post_data, $employee, $clientName, $docName, $filename) {
        $message->to($post_data['email'], $clientName)->subject($post_data['subject']);
        $message->from('xxxx', $employee->first_name . " " . $employee->last_name);
        $message->replyTo($employee->email);
    }
    );
    echo "send Email";
    exit;
}

Now, When Job handle function executed I am facing issue with get content of email for sending email. Here is code for get email content

$payload = json_decode($event->job->getRawBody());
$data = unserialize($payload->data->command);

But this code is not working. My aim is get that email content use that content for sending emails.

Or if you have any other solutions or options for send emails. Please share your idea as well.

Thanks you in advance.

1
you are exiting before sending any mail...jtwes
What is the error that you receive?linuxartisan
@linuxartisan I don't received any error, but I can not read the email data from job table.Nikunj K.

1 Answers

0
votes

You can pass any data you want available in your job when constructing it like so:

MySendEmailJob::dispatch('body', 'subject', 'email', $employee, 'clientname', 'docname', 'filename');

The job itself will look something like this:

    use Dispatchable,
        InteractsWithQueue,
        Queueable,
        SerializesModels;

    /**
     * @var string
     */
    private $body;

    /**
     * @var string
     */
    private $subject;

    /**
     * @var string
     */
    private $email;

    /**
     * @var User
     */
    private $employee;

    /**
     * @var string
     */
    private $clientName;

    /**
     * @var string
     */
    private $docName;

    /**
     * @var string
     */
    private $filename;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(
        string $body,
        string $subject,
        string $email,
        User $employee,
        string $clientName,
        string $docName,
        string $filename
    ) {
        $this->body = $body;
        $this->subject = $subject;
        $this->email = $email;
        $this->employee = $employee;
        $this->clientName = $clientName;
        $this->docName = $docName;
        $this->filename = $filename;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::send(
            ['html' => 'emails.templates'],
            ['body' => $this->body, 'title' => $this->subject],
            function ($message) {
                $message->to($this->email, $this->clientName)->subject($this->subject);
                $message->from('xxxx', $this->employee->first_name . " " . $this->employee->last_name);
                $message->replyTo($this->employee->email);
            }
        );
    }

Check out the Laravel docs for more examples and documentation