diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index 212cdabb5a..c283092e69 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -14,6 +14,7 @@ class Mail extends Event protected array $smtp = []; protected array $variables = []; protected string $bodyTemplate = ''; + protected array $attachment = []; public function __construct(protected Connection $connection) { @@ -337,6 +338,22 @@ class Mail extends Event return $this; } + public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text') + { + $this->attachment = [ + 'content' => base64_encode($content), + 'filename' => $filename, + 'encoding' => $encoding, + 'type' => $type, + ]; + return $this; + } + + public function getAttachment(): array + { + return $this->attachment; + } + /** * Executes the event and sends it to the mails worker. * @@ -355,6 +372,7 @@ class Mail extends Event 'body' => $this->body, 'smtp' => $this->smtp, 'variables' => $this->variables, + 'attachment' => $this->attachment, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) ]); } diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php index 3bcc9e7699..e8d96f098d 100644 --- a/src/Appwrite/Platform/Workers/Mails.php +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -59,6 +59,7 @@ class Mails extends Action $variables = $payload['variables']; $name = $payload['name']; $body = $payload['body']; + $attachment = $payload['attachment'] ?? []; $bodyTemplate = $payload['bodyTemplate']; if(empty($bodyTemplate)) { $bodyTemplate = __DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'; @@ -92,6 +93,14 @@ class Mails extends Action $mail->Subject = $subject; $mail->Body = $body; $mail->AltBody = \strip_tags($body); + if (!empty($attachment['content'] ?? '')) { + $mail->AddStringAttachment( + base64_decode($attachment['content']), + $attachment['filename'] ?? 'unknown.file', + $attachment['encoding'] ?? PHPMailer::ENCODING_BASE64, + $attachment['type'] ?? 'plain/text' + ); + } try { $mail->send();