How to add new properties for my custom EMails

Hi everyone :slight_smile:

I would like to override 2 automatics email sent by Nextcloud :

  • When I create an user
  • When I share a file or folder with an user (not by email address).

I wrote this code :

<?php

namespace OCA\MailerWrapper\Mail;

use OC\Mail\EMailTemplate as ParentTemplate;
use OCP\Defaults;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;

class EMailTemplate extends ParentTemplate {

	protected string $plainBodyAddUser = <<<EOF
		Welcome.
	EOF;

	protected string $bodyTextAddUser = <<<EOF
		body
	EOF;

	protected string $footerTextAddUser = <<<EOF
		footer
	EOF;

	public function __construct(
		protected Defaults $themingDefaults,
		protected IURLGenerator $urlGenerator,
		protected IFactory $l10nFactory,
		protected string $emailId,
		protected array $data,
		// protected string $message
		private IUserManager $userManager
	)
	{
		parent::__construct(
			$themingDefaults,
			$urlGenerator,
			$l10nFactory,
			$emailId,
			$data
		);
	}

	public function addHeading(string $title, $plainTitle = ''): void
	{
		if ($this->emailId === 'settings.Welcome') {
			parent::addHeading($this->plainBodyAddUser);
			return;
		}

		parent::addHeading($title, $plainTitle);
	}

	public function addBodyText(string $text, $plainText = ''): void {
		if ($this->emailId === 'settings.Welcome') {
			if (str_starts_with($text, 'Your username')) {
				$uid = explode(':', $text)[1];
				// $user = $this->userManager->get($uid);
				parent::addBodyText(
					sprintf(
						'Account user name %s',
						$uid
					)
				);
				return;
			}

			parent::addBodyText($this->bodyTextAddUser);

			return;
		}

		if ($this->emailId === 'files_sharing.RecipientNotification') {
			parent::addBodyText(
				'body sharing 1'
			);

			parent::addBodyText(
				'body sharing 2'
			);

			parent::addBodyText('body sharing 3');

			return;
		}

		// default email
		parent::addBodyText($text, $plainText);
	}

	public function addFooter(string $text = '', ?string $lang = null): void {
		if ($this->emailId === 'settings.Welcome') {
			parent::addFooter($this->footerTextAddUser);
			return;
		}

		if ($this->emailId === 'files_sharing.RecipientNotification') {
			parent::addFooter('footer sharing');
			return;
		}

		// default email
		parent::addBodyText($text, $lang);
	}
}

In the constructor from my EMailTemplate class, I added the IUserManager interface to add the email address of the user created.

But, when I created the user, I get an issue :

In the response tab :

<?xml version="1.0"?>
<ocs>
 <meta>
  <status>failure</status>
  <statuscode>500</statuscode>
  <message>Internal Server Error
Too few arguments to function OCA\MailerWrapper\Mail\EMailTemplate::__construct(), 5 passed in /var/www/html/lib/private/Mail/Mailer.php on line 130 and exactly 6 expected in file '/var/www/html/apps-extra/mailerwrapper/lib/Mail/EMailTemplate.php' line 32</message>
 </meta>
 <data/>
</ocs>

But, I don’t understand, I sent to parent all variables to construct it :

		parent::__construct(
			$themingDefaults,
			$urlGenerator,
			$l10nFactory,
			$emailId,
			$data
		);

So, why I cannot add the IUserManager interface to custom my emails ?

Hey,

I fear, you are out of luck here. The mail template is not instantiated via the default dependency injection methods but directly. In the corresponding server code, you see that the template class is instantiated with a fixed set of parameters.

This makes sense, as each mail needs to be generated individually (aka as different objects). Thus, DI is not working as this would imply a singleton pattern (contradiction to individual mails).

I fear, this is the wrong location to grap into the mailing system, sorry.