From 20322589ddcfd14c46cdea4847f43e372d665b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 17 Jan 2024 11:22:08 +0000 Subject: [PATCH] Improve SMTP test email design --- .../locale/templates/email-base-styled.tpl | 105 ++++-------- app/config/locale/templates/email-base.tpl | 149 ++++++++++-------- .../locale/templates/email-smtp-test.tpl | 12 ++ app/config/locale/templates/email-test.tpl | 7 - app/controllers/api/projects.php | 13 +- src/Appwrite/Event/Mail.php | 25 +++ src/Appwrite/Platform/Workers/Mails.php | 18 ++- .../Projects/ProjectsConsoleClientTest.php | 8 +- 8 files changed, 184 insertions(+), 153 deletions(-) create mode 100644 app/config/locale/templates/email-smtp-test.tpl delete mode 100644 app/config/locale/templates/email-test.tpl diff --git a/app/config/locale/templates/email-base-styled.tpl b/app/config/locale/templates/email-base-styled.tpl index 483809cbf9..f6e55ee445 100644 --- a/app/config/locale/templates/email-base-styled.tpl +++ b/app/config/locale/templates/email-base-styled.tpl @@ -1,36 +1,41 @@ - + - + + + + - + +
@@ -169,9 +132,11 @@
- +
- +
{{message}} +{{body}} +
diff --git a/app/config/locale/templates/email-base.tpl b/app/config/locale/templates/email-base.tpl index 346f2f1589..07f86f0599 100644 --- a/app/config/locale/templates/email-base.tpl +++ b/app/config/locale/templates/email-base.tpl @@ -1,67 +1,92 @@ + + + + + + - - - - - - - - - -
- - - - -
- {{body}} -
-
- - + +
+ + + + +
+

{{subject}}

+
+ + + + +
+{{body}} +
+
+ \ No newline at end of file diff --git a/app/config/locale/templates/email-smtp-test.tpl b/app/config/locale/templates/email-smtp-test.tpl new file mode 100644 index 0000000000..e32bd72c5a --- /dev/null +++ b/app/config/locale/templates/email-smtp-test.tpl @@ -0,0 +1,12 @@ +

Hello,

+ +

This email ensures your custom SMTP configuration is working correctly. Please review your sender Information details:

+ +

From: {{from}}

+

Reply-To: {{replyTo}}

+ +

If this email landed in your spam folder, ensure your SMTP server is set up correctly. A common mistake is overlooking some DNS configuration.

+

If you have trouble with the sender's image, ensure it is set in the Gravatar database.

+ +

Best regards,

+

Appwrtite team

\ No newline at end of file diff --git a/app/config/locale/templates/email-test.tpl b/app/config/locale/templates/email-test.tpl deleted file mode 100644 index 8180f36ac8..0000000000 --- a/app/config/locale/templates/email-test.tpl +++ /dev/null @@ -1,7 +0,0 @@ -

Hello,

- -

You're good to go! If you see this email, SMTP settings you provided are valid.

- -

Thanks

- -

Appwrite Team

\ No newline at end of file diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 13409d0378..196f38f82d 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -1661,9 +1661,13 @@ App::post('/v1/projects/:projectId/smtp/tests') throw new Exception(Exception::PROJECT_NOT_FOUND); } - $subject = 'Custom SMTP email from Appwrite'; - $message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-test.tpl'); - $body = $message->render(); + $replyToEmail = !empty($replyTo) ? $replyTo : $senderEmail; + + $subject = 'Custom SMTP email sample'; + $template = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-smtp-test.tpl'); + $template + ->setParam('{{from}}', "{$senderName} ({$senderEmail})") + ->setParam('{{replyTo}}', "{$senderName} ({$replyToEmail})"); foreach ($emails as $email) { $queueForMails @@ -1677,7 +1681,8 @@ App::post('/v1/projects/:projectId/smtp/tests') ->setSmtpSenderName($senderName) ->setRecipient($email) ->setName('') - ->setBody($body) + ->setbodyTemplate(__DIR__ . '/../../config/locale/templates/email-base-styled.tpl') + ->setBody($template->render()) ->setVariables([]) ->setSubject($subject) ->trigger(); diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index c2de8023b0..6cb53d4d36 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 string $bodyTemplate = ''; public function __construct(protected Connection $connection) { @@ -115,6 +116,29 @@ class Mail extends Event return $this->name; } + /** + * Sets bodyTemplate for the mail event. + * + * @param string $bodyTemplate + * @return self + */ + public function setbodyTemplate(string $bodyTemplate): self + { + $this->bodyTemplate = $bodyTemplate; + + return $this; + } + + /** + * Returns subject for the mail event. + * + * @return string + */ + public function getbodyTemplate(): string + { + return $this->bodyTemplate; + } + /** * Set SMTP Host * @@ -327,6 +351,7 @@ class Mail extends Event 'recipient' => $this->recipient, 'name' => $this->name, 'subject' => $this->subject, + 'bodyTemplate' => $this->bodyTemplate, 'body' => $this->body, 'smtp' => $this->smtp, 'variables' => $this->variables, diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php index 86bf2004ad..a02d3d05f8 100644 --- a/src/Appwrite/Platform/Workers/Mails.php +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -63,7 +63,15 @@ class Mails extends Action $name = $payload['name']; $body = $payload['body']; - $bodyTemplate = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'); + $variables['subject'] = $subject; + $variables['year'] = date("Y"); + + $bodyTemplate = $payload['bodyTemplate']; + if (empty($bodyTemplate)) { + $bodyTemplate = __DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'; + } + + $bodyTemplate = Template::fromFile($bodyTemplate); $bodyTemplate->setParam('{{body}}', $body); foreach ($variables as $key => $value) { $bodyTemplate->setParam('{{' . $key . '}}', $value); @@ -101,12 +109,8 @@ class Mails extends Action $replyToName = \urldecode(App::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); if (!empty($smtp)) { - if (!empty($smtp['replyTo'])) { - $replyTo = $smtp['replyTo']; - } - if (!empty($smtp['senderName'])) { - $replyToName = $smtp['senderName']; - } + $replyTo = !empty($smtp['replyTo']) ? $smtp['replyTo'] : $smtp['senderEmail']; + $replyToName = $smtp['senderName']; } $mail->addReplyTo($replyTo, $replyToName); diff --git a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php index 36f9bbcad0..5260e62a9a 100644 --- a/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php +++ b/tests/e2e/Services/Projects/ProjectsConsoleClientTest.php @@ -606,9 +606,11 @@ class ProjectsConsoleClientTest extends Scope $this->assertEquals('Custom Mailer', $emails[0]['from'][0]['name']); $this->assertEquals('reply@appwrite.io', $emails[0]['replyTo'][0]['address']); $this->assertEquals('Custom Mailer', $emails[0]['replyTo'][0]['name']); - $this->assertEquals('Custom SMTP email from Appwrite', $emails[0]['subject']); - $this->assertStringContainsStringIgnoringCase('good to go', $emails[0]['text']); - $this->assertStringContainsStringIgnoringCase('good to go', $emails[0]['html']); + $this->assertEquals('Custom SMTP email sample', $emails[0]['subject']); + $this->assertStringContainsStringIgnoringCase('working correctly', $emails[0]['text']); + $this->assertStringContainsStringIgnoringCase('working correctly', $emails[0]['html']); + $this->assertStringContainsStringIgnoringCase('251 Little Falls Drive', $emails[0]['text']); + $this->assertStringContainsStringIgnoringCase('251 Little Falls Drive', $emails[0]['html']); $to = [ $emails[0]['to'][0]['address'],