diff --git a/src/api/wishlists.php b/src/api/wishlists.php index 7eb4f933..b0d344ff 100644 --- a/src/api/wishlists.php +++ b/src/api/wishlists.php @@ -60,30 +60,17 @@ switch ($_SERVER['REQUEST_METHOD']) { /** Send email */ $user = new User($wishlist['user']); $email = new Email($user->email, __('Wish request'), 'default', 'wishlist-request-wishes'); - $email->mjml = str_replace( - 'TEXT_HELLO', - __('Hello,'), - $email->mjml - ); - $email->mjml = str_replace( + $email->setPlaceholder('TEXT_HELLO', __('Hello,')); + $email->setPlaceholder( 'TEXT_WISHLIST_REQUEST_WISHES', sprintf( /** TRANSLATORS: %s: Wishlist name */ __('somebody has requested that you add more wishes to your wishlist %s.'), '' . $wishlist['name'] . '' - ), - $email->mjml - ); - $email->mjml = str_replace( - 'TEXT_WISH_ADD', - __('Add wish'), - $email->mjml - ); - $email->mjml = str_replace( - 'LINK_WISH_ADD', - $href . '&wish_add=true', - $email->mjml + ) ); + $email->setPlaceholder('TEXT_WISH_ADD', __('Add wish')); + $email->setPlaceholder('LINK_WISH_ADD', $href . '&wish_add=true'); $success = $email->send(); diff --git a/src/classes/email.php b/src/classes/email.php index 90b6694b..1925182e 100644 --- a/src/classes/email.php +++ b/src/classes/email.php @@ -16,7 +16,9 @@ use Qferrer\Mjml\Renderer\ApiRenderer; class Email { - public string $mjml; + private string $mjml = ''; + private string $contentsTemplate; + private string $contentsPart; public function __construct( private string $to, @@ -24,8 +26,15 @@ class Email private string $template, private string $part ) { - $this->mjml = file_get_contents(ROOT . '/src/mjml/' . $this->template . '.mjml'); - $this->mjml = str_replace('MJML_PART', 'parts/' . $this->part . '.mjml', $this->mjml); + $this->contentsTemplate = file_get_contents(ROOT . '/src/mjml/' . $this->template . '.mjml'); + $this->contentsPart = file_get_contents(ROOT . '/src/mjml/parts/' . $this->part . '.mjml'); + + $this->mjml = str_replace('', $this->contentsPart, $this->contentsTemplate); + } + + public function setPlaceholder(string $placeholder, string $replacement): void + { + $this->mjml = str_replace($placeholder, $replacement, $this->mjml); } public function send(): bool @@ -62,6 +71,8 @@ class Email $success = mail($to, $subject, $message, $headers); + error_log($html); + return $success; } } diff --git a/src/pages/login.php b/src/pages/login.php index 61515842..63c0708d 100644 --- a/src/pages/login.php +++ b/src/pages/login.php @@ -69,32 +69,18 @@ if (isset($_POST['reset'], $_POST['email'])) { ;'); $emailReset = new Email($_POST['email'], __('Password reset link'), 'default', 'password-reset'); - $emailReset->mjml = str_replace( - 'TEXT_HELLO', - __('Hello,'), - $emailReset->mjml - ); - $emailReset->mjml = str_replace( + $emailReset->setPlaceholder('TEXT_HELLO', __('Hello,')); + $emailReset->setPlaceholder( 'TEXT_PASSWORD_RESET', - __('somebody has requested a password reset for this email address from wishthis.online. If this was you, click the button below to invalidate your current password and set a new one.'), - $emailReset->mjml + __('somebody has requested a password reset for this email address from wishthis.online. If this was you, click the button below to invalidate your current password and set a new one.') ); - $emailReset->mjml = str_replace( - 'TEXT_SET_NEW_PASSWORD', - __('Set new password'), - $emailReset->mjml - ); - $emailReset->mjml = str_replace( - 'wishthis.online', - $_SERVER['HTTP_HOST'], - $mjml - ); - $emailReset->mjml = str_replace( + $emailReset->setPlaceholder('TEXT_SET_NEW_PASSWORD', __('Set new password')); + $emailReset->setPlaceholder('wishthis.online', $_SERVER['HTTP_HOST']); + $emailReset->setPlaceholder( 'password-reset-link', $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . - Page::PAGE_REGISTER . '&password-reset=' . $_POST['email'] . '&token=' . $token, - $mjml + Page::PAGE_REGISTER . '&password-reset=' . $_POST['email'] . '&token=' . $token ); $emailReset->send();