Skip to content

Commit

Permalink
Move send message to single attendee controller to use a mail job lik…
Browse files Browse the repository at this point in the history
…e the other emails, this will also allow it to get the organizer logo in the email
  • Loading branch information
johannac committed Jan 12, 2023
1 parent 68bd391 commit 7c97f1c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 24 deletions.
33 changes: 9 additions & 24 deletions app/Http/Controllers/EventAttendeesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\SendAttendeeInviteJob;
use App\Jobs\SendOrderAttendeeTicketJob;
use App\Jobs\SendMessageToAttendeesJob;
use App\Jobs\SendMessageToAttendeeJob;
use App\Models\Attendee;
use App\Models\Event;
use App\Models\EventStats;
Expand Down Expand Up @@ -362,31 +363,15 @@ public function postMessageAttendee(Request $request, $attendee_id)
}

$attendee = Attendee::scope()->findOrFail($attendee_id);
$event = Event::scope()->findOrFail($attendee->event_id);
$subject = $request->get('subject');
$content = $request->get('message');
$send_copy = $request->get('send_copy');

$data = [
'attendee' => $attendee,
'message_content' => $request->get('message'),
'subject' => $request->get('subject'),
'event' => $attendee->event
];

//@todo move this to the SendAttendeeMessage Job
Mail::send(Lang::locale().'.Emails.messageReceived', $data, function ($message) use ($attendee, $data) {
$message->to($attendee->email, $attendee->full_name)
->from(config('attendize.outgoing_email_noreply'), $attendee->event->organiser->name)
->replyTo($attendee->event->organiser->email, $attendee->event->organiser->name)
->subject($data['subject']);
});

/* Could bcc in the above? */
if ($request->get('send_copy') == '1') {
Mail::send(Lang::locale().'.Emails.messageReceived', $data, function ($message) use ($attendee, $data) {
$message->to($attendee->event->organiser->email, $attendee->event->organiser->name)
->from(config('attendize.outgoing_email_noreply'), $attendee->event->organiser->name)
->replyTo($attendee->event->organiser->email, $attendee->event->organiser->name)
->subject($data['subject'] . trans("Email.organiser_copy"));
});
}
/*
* Queue the emails
*/
SendMessageToAttendeeJob::dispatch($subject, $content, $event, $attendee, $send_copy);

return response()->json([
'status' => 'success',
Expand Down
65 changes: 65 additions & 0 deletions app/Jobs/SendMessageToAttendeeJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Jobs;

use App\Mail\SendMessageToAttendeeMail;
use App\Models\Attendee;
use App\Models\Event;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Config;
use Mail;

class SendMessageToAttendeeJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $subject;
public $content;
public $event;
public $attendee;
public $send_copy;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($subject, $content, Event $event, Attendee $attendee, $send_copy)
{
$this->subject = $subject;
$this->content = $content;
$this->event = $event;
$this->attendee = $attendee;
$this->send_copy = $send_copy;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$mail = new SendMessageToAttendeeMail(
$this->subject,
$this->content,
$this->event,
$this->attendee
);
Mail::to($this->attendee->email, $this->attendee->full_name)
->locale(Config::get('app.locale'))
->send($mail);

if ($this->send_copy == '1') {
$mail->subject = $mail->subject . trans("Email.organiser_copy");
Mail::to($this->event->organiser->email, $this->event->organiser->name)
->locale(Config::get('app.locale'))
->send($mail);
}
}
}
48 changes: 48 additions & 0 deletions app/Mail/SendMessageToAttendeeMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Mail;

use App\Models\Attendee;
use App\Models\Event;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendMessageToAttendeeMail extends Mailable
{
use Queueable, SerializesModels;

public $subject;
public $content;
public $event;
public $attendee;
public $email_logo;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($subject, $content, Event $event, Attendee $attendee)
{
$this->subject = $subject;
$this->content = $content;
$this->event = $event;
$this->attendee = $attendee;
$this->email_logo = $event->organiser->full_logo_path;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject($this->subject)
->from(config('attendize.outgoing_email_noreply'), $this->event->organiser->name)
->replyTo($this->event->organiser->email, $this->event->organiser->name)
->view('Emails.MessageToAttendees');
}
}

0 comments on commit 7c97f1c

Please sign in to comment.