From 386cee5137a9ffb2c5dfd65ba109592b9ee15179 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 8 Dec 2023 19:57:15 +0000 Subject: [PATCH 1/3] mail support string as attachment --- src/Appwrite/Event/Mail.php | 17 +++++++++++++++++ src/Appwrite/Platform/Workers/Mails.php | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index c2de8023b0..95fffa90d8 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -13,6 +13,7 @@ class Mail extends Event protected string $body = ''; protected array $smtp = []; protected array $variables = []; + protected array $attachment = []; public function __construct(protected Connection $connection) { @@ -313,6 +314,21 @@ class Mail extends Event return $this; } + public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text') + { + $this->attachment = [ + 'content' => $content, + 'filename' => $filename, + 'encoding' => $encoding, + 'type' => $type, + ]; + } + + public function getAttachment(): array + { + return $this->attachment; + } + /** * Executes the event and sends it to the mails worker. * @@ -330,6 +346,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 94a2a46087..f07fd7e60c 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 = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'); $bodyTemplate->setParam('{{body}}', $body); @@ -89,6 +90,14 @@ class Mails extends Action $mail->Subject = $subject; $mail->Body = $body; $mail->AltBody = \strip_tags($body); + if(!empty($attachment['content'] ?? '')) { + $mail->AddStringAttachment( + $attachment['content'], + $attachment['filename'] ?? 'unknown.file', + $attachment['encoding'] ?? PHPMailer::ENCODING_BASE64, + $attachment['type'] ?? 'plain/text' + ); + } try { $mail->send(); From f553576d0e3b6817930d94eb437d93ceea343b75 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 8 Dec 2023 20:06:37 +0000 Subject: [PATCH 2/3] fix linter --- src/Appwrite/Platform/Workers/Mails.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php index f07fd7e60c..bb55133c0b 100644 --- a/src/Appwrite/Platform/Workers/Mails.php +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -90,7 +90,7 @@ class Mails extends Action $mail->Subject = $subject; $mail->Body = $body; $mail->AltBody = \strip_tags($body); - if(!empty($attachment['content'] ?? '')) { + if (!empty($attachment['content'] ?? '')) { $mail->AddStringAttachment( $attachment['content'], $attachment['filename'] ?? 'unknown.file', From e3bab623f1c4993891547fa8d3a90c49cabc4daf Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 8 Dec 2023 21:34:39 +0000 Subject: [PATCH 3/3] fix redis issue by encoding content --- src/Appwrite/Event/Mail.php | 3 ++- src/Appwrite/Platform/Workers/Mails.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index 95fffa90d8..47c9125c01 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -317,11 +317,12 @@ class Mail extends Event public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text') { $this->attachment = [ - 'content' => $content, + 'content' => base64_encode($content), 'filename' => $filename, 'encoding' => $encoding, 'type' => $type, ]; + return $this; } public function getAttachment(): array diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php index bb55133c0b..9018f219ae 100644 --- a/src/Appwrite/Platform/Workers/Mails.php +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -92,7 +92,7 @@ class Mails extends Action $mail->AltBody = \strip_tags($body); if (!empty($attachment['content'] ?? '')) { $mail->AddStringAttachment( - $attachment['content'], + base64_decode($attachment['content']), $attachment['filename'] ?? 'unknown.file', $attachment['encoding'] ?? PHPMailer::ENCODING_BASE64, $attachment['type'] ?? 'plain/text'