Skip to content

Emails Queue

Introduction

The email queue is employed to dispatch emails through a queue mechanism, ensuring that emails are sent in the background and preventing any interference with the user interface during the email sending process. The email jobs are added to the "emails" queue.

Mail::raw(['html' => $data["html"]], function ($message) use ($data) {
$message = $this->prepareMessage($message, $data);
return $message;
});
 
// OR
 
Mail::send($data["template_name"], $data["message_vars"], function ($message) use ($data) {
$message = $this->prepareMessage($message, $data);
return $message;
});

There are two types of emails in this system. The first type is a raw email, which accepts the HTML content of the email as a parameter and utilizes the Mail::raw function for sending. The second type accepts a template name along with variables and utilizes the Mail::send function for email delivery.

A helper function named prepareMessage is utilized to configure message parameters such as sender, recipient, subject, attachments, and other relevant settings.

private function prepareMessage($message, $data)
{
if (isset($data["to_email"]) && $data["to_email"]) {
if (isset($data["to_name"])) {
$message->to($data["to_email"], $data["to_name"]);
} else {
$message->to($data["to_email"]);
}
}
if (isset($data["from_email"]) && $data["from_email"]) {
// $from_email = $data["from_email"];
// $from_name = isset($data["from_name"]) ? $data["from_name"]: $from_email;
// $message->from($from_email, $from_name);
}
if (isset($data["sender_email"]) && $data["sender_email"]) {
$sender_email = $data["sender_email"];
if (isset($data["sender_name"])) {
$message->sender($sender_email, $data["sender_name"]);
} else {
$message->sender($sender_email);
}
}
if (isset($data["reply_to"]) && $data["reply_to"]) {
$reply_to = $data["reply_to"];
if (isset($data["reply_to_name"])) {
$message->replyTo($reply_to, $data["reply_to_name"]);
} else {
$message->replyTo($reply_to);
}
}
if (isset($data["subject"]) && $data["subject"]) {
$message->subject($data["subject"]);
}
if (isset($data["cc"]) && $data["cc"]) {
$message->cc(explode(",", preg_replace('/\s/', '', $data["cc"])));
}
if (isset($data["bcc"]) && $data["bcc"]) {
$message->bcc($data["bcc"]);
}
if (isset($data["attachments"]) && $data["attachments"]) {
foreach ($data["attachments"] as $attachment) {
$c = curl_init();
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $attachment['url']);
$contents = curl_exec($c);
curl_close($c);
$extension = pathinfo($attachment['url'], PATHINFO_EXTENSION);
$message->attachData($contents, $attachment['name'] . '.' . $extension);
}
}
 
return $message;
}

Notes: All parameters in the prepareMessage function are optional, with the exception of to_email, which is mandatory to ensure the email is sent to a valid recipient.

Function Name: prepareMessage

Purpose: This function is responsible for configuring various message parameters for email communication, such as sender, recipient, subject, attachments, and more. It ensures that email messages are properly formatted before sending.

Parameters:

  • $message (Object): The email message object that is being prepared.
  • $data (Array): An associative array containing the email-related data and parameters, including recipient information, sender information, subject, attachments, and more.

Description:

  • The function begins by checking if the recipient's email address (to_email) is set in the $data array. If so, it sets the recipient's email address and name (if provided) using the to method of the $message object.
  • The section related to the sender's email address (from_email) is commented out in the code because the mail server used in the system does not support changing the "from email" address. The sender's email address is set to a user email by default.
  • Similarly, if a sender's email address (sender_email) is provided in the $data array, the function sets the sender's email address and name (if available) using the sender method of the $message object.
  • The function also handles the "Reply-To" email address and name using the replyTo method of the $message object if provided in the $data array.
  • The email subject is set using the subject method based on the subject key in the $data array.
  • If carbon copy (CC) recipients are specified (cc), they are processed and added to the email using the cc method.
  • Blind carbon copy (BCC) recipients (bcc) are similarly added to the email using the bcc method.
  • Finally, if attachments are included in the $data array, the function iterates through each attachment, retrieves its contents via cURL, and attaches it to the email message.

Return Value: The function returns the modified $message object with all the necessary parameters set.

Example Usage:

 
Queue::push(EmailQueue::class, [
'template_name' => 'otas::mail.application.created',
'message_vars' => [
'name' => $app->student->user->name,
'studentName' => $app->student->name,
'program' => $app->program->title_en,
'university' => $app->school->name,
'creatorName' => $app->creator->name,
'creatorTitle' => $app->creator->title,
'creatorOrganization' => $app->creator->organization,
'creatorPhone' => $app->creator->phone,
'creatorEmail' => $app->creator->email,
],
'to_email' => $app->student->user->email,
'to_name' => $app->student->name,
'reply_to' => $app->creator->email,
'reply_to_name' => $app->creator->name,
'cc' => 'cc1@example.com, cc2@example.com',
'bcc' => ['bcc1@example.com', 'bcc2@example.com'],
'subject' => 'Thank You, Your Application Has Been Created',
'attachments' => [
['url' => 'https://example.com/file.pdf', 'name' => 'Attachment 1'],
['url' => 'https://example.com/image.jpg', 'name' => 'Attachment 2'],
]
 
], "emails");
 
// EmailQueue Job
 
Mail::send($data["template_name"], $data["message_vars"], function ($message) use ($data) {
$message = $this->prepareMessage($message, $data);
return $message;
});

Notes: The section related to the sender's email address (from_email) is commented out in the code due to limitations with the mail server used in the system, which only supports using a user email as the "from email" address.