How to get an event for my custom Email?

Hi everyone ! :slight_smile:

I would like to custom an activity email.
I caught the emailId of activity.Notification.
But, I would like to add a new sentence when if this event is emitted : server/apps/files_sharing/lib/Activity/Providers/Users.php at fc560d8ec986402587ae506bbff58e242eb269f7 · nextcloud/server · GitHub .

If I get this part of code correctly, it corresponds to this email :

I tried to inject the OCP\Activity\IEvent namespace in my function :

<?php

use OCP\Activity\IEvent;

class EMailTemplate extends ParentTemplate
{
   public function addBodyText(string $text, $plainText = '', ?IEvent $event = null): void    
   {
       // code
    }
}

But, I cannot catch it, I just get the null value.

Do you have advice or tips for me? Or another solution ?

Hello,

I stumbled upon another topic here on the forum, recently. Maybe, you can adopt to the suggestion there?

Christian

Hi @christianlupus :slight_smile:

Thanks for your answer :slight_smile:

I don’t think your suggestion can help me :thinking:

In fact, it’s easy to customise an email.

From the app, you can create a EMailTemplate class which extends OC\Mail\EMailTemplate.php.

Next, you customise your different fields by overwriting the properties of the parent class.

Then, you can catch the changes by overwriting the functions.

For each Nextcloud email, they have an emailId.

To custom one specifc email, inside a function overwritten you write a conditional to check if the emailid correspond to email catched.
For example, to catch the welcome email, you must check with a conditional on settings.Welcome.

For example, to customise Nextcloud emails from my app :

/lib/Mail/EMailTemplate.php

<?php

namespace OCA\EmailCustomize\Mail;

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

class EMailTemplate extends ParentTemplate {

	protected Defaults $themingDefaults;

	protected IURLGenerator $urlGenerator;

	protected IFactory $l10nFactory;

	protected string $emailId;

	protected array $data;

	/**
	 *
	 *	Properties for the email when an user is created.
	 *
	 */

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

	protected string $bodyTextAddUser = <<<EOF
		A body text to add a new user.
	EOF;

	protected string $bodyTextWelcomeAddUser = <<<EOF
		Account user name %s
	EOF;

	protected string $footerHTMLAddUser = <<<EOF
		My <b>personal</b> footer.
	EOF;

	protected string $footerTextAddUser = <<<EOF
		My personnal footer.
	EOF;

	/**
	 *
	 *	Properties for the email when a file or folder is
	 *	shared to an user (not email).
	 *
	 */

	protected string $firstBodyTextFileShare = <<<EOF
		My first body text when a file is shared.
	EOF;

	protected string $secondBodyTextFileShare = <<<EOF
		My second body text when a file is shared.
	EOF;

	protected string $thirdBodyHTMLFileShare = <<<EOF
		My third body text when a file is shared.
	EOF;

	protected string $thirdBodyTextFileShare = <<<EOF
		My <b>third</b> body text when a file is shared.
	EOF;

	protected string $footerTextFileShare = <<<EOF
		My footer when a file is shared.
	EOF;

	protected string $footerHTMLFileShare = <<<EOF
		My <b>footer</b> when a file is shared.
	EOF;

	/**
	 *
	 *	Properties for the email when the activity app
	 *	sent an email.
	 *
	 */

	protected string $firstBodyTextActivity = <<<EOF
		My first body text for activity.
	EOF;

	protected string $secondBodyTextActivity = <<<EOF
		My second body text for activity.
	EOF;

	protected string $secondBodyHtmlActivity = <<<EOF
		My <b>second</b> body text for activity.
	EOF;

	protected string $footerTextActivity = <<<EOF
		My footer for activity.
	EOF;

	protected string $footerHtmlActivity = <<<EOF
		My <b>footer</b>ff for activity.
	EOF;

	public function __construct(
		Defaults $themingDefaults,
		IURLGenerator $urlGenerator,
		IFactory $l10nFactory,
		string $emailId,
		array $data) {
		parent::__construct(
			$themingDefaults,
			$urlGenerator,
			$l10nFactory,
			$emailId,
			$data
		);
	}

	/**
	 * Change the header text when addHeading from
	 * OC\Mail\EMailTemplate is called.
	 */
	public function addHeading(string $title, $plainTitle = ''): void {
		/**
		 * Custom header text when an user is created.
		 */
		if ($this->emailId === 'settings.Welcome') {
			parent::addHeading($this->headerTextAddUser);
			return;
		}

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

	/**
	 * Change the body text when the addBodyText from
	 * OC\Mail\EMailTemplate is called.
	 */
	public function addBodyText(string $text, $plainText = ''): void {
		/**
		 * Custom body text when an user is created.
		 */
		if ($this->emailId === 'settings.Welcome') {
			/**
			 * Catch the text that contains the username
			 * and change it by another wording.
			 */
			if (str_starts_with($text, 'Your username')) {
				$uid = explode(':', $text)[1];

				parent::addBodyText(
					sprintf($this->bodyTextWelcomeAddUser, $uid)
				);

				return;
			}

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

		/**
		 * Custom body text when a file or folder is
		 * shared for an user (not email).
		 */
		if ($this->emailId === 'files_sharing.RecipientNotification') {
			parent::addBodyText($this->firstBodyTextFileShare);

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

			$this->htmlBody .= vsprintf($this->bodyText, [ $this->thirdBodyHTMLFileShare ]);
			$this->plainBody .= $this->thirdBodyTextFileShare;

			return;
		}

		/**
		 * Custom body text when the activity app send an email.
		 */
		if ($this->emailId === 'activity.Notification') {
			parent::addBodyText($this->firstBodyTextActivity);

			$this->htmlBody .= vsprintf(
				$this->bodyText,
				[
					$this->secondBodyHtmlActivity
				]
			);

			$this->plainBody .= $this->secondBodyTextActivity;

			return;
		}

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

	/**
	 * Change the footer text when the addFooter from
	 * OC\Mail\EMailTemplate is called.
	 */
	public function addFooter(string $text = '', ?string $lang = null): void {
		/**
		 * Custom body text when an user is created.
		 */
		if ($this->emailId === 'settings.Welcome') {
			$this->htmlBody .= vsprintf(
				$this->bodyText,
				[ $this->footerHTMLAddUser ]
			);

			$this->plainBody .= $this->footerTextAddUser;

			return;
		}

		/**
		 * Custom body text when a file or folder is
		 * shared for an user (not email).
		 */
		if ($this->emailId === 'files_sharing.RecipientNotification') {
			$this->htmlBody .= vsprintf(
				$this->bodyText,
				[ $this->footerHTMLFileShare ]
			);
			$this->plainBody .= $this->footerTextFileShare;

			return;
		}

		/**
		 * Custom footer text when the Activity app sent an email.
		 */
		if ($this->emailId === 'activity.Notification') {
			$this->htmlBody .= vsprintf(
				$this->bodyText,
				[ $this->footerHtmlActivity ]
			);

			$this->plainBody .= $this->footerTextActivity;

			return;
		}

		/**
		 * Default email
		 */
		parent::addFooter($text, $lang);
	}
}

Now, my problem is that I would like to customise an Activity email only when a user unshares a file or folder with this mention “userA removed userB from fileA” : server/apps/files_sharing/lib/Activity/Providers/Users.php at aff3e65806082e85765630dcdf292d54a7b40277 · nextcloud/server · GitHub .

But, I think the Activity emails are sent with a single emailId and I cannot catch the email unsharing.

I thought of using OCP\Activity\IEvent, but I cannot use it because the parent functions don’t inject the IEvent.

I’m blocked in developping for my email customization :confused:

I fear, you are right. I wanted to show you there is a rudimentary way to affect the mails but only on a global level.
All you could do (to my understanding) was to file a PR with an update to the appropriate app that created the unshare action and define a custom event.

Not the simplest solution, though.
Christian

Yes, I think so too.

However, on reflection, for each function we have the text and plainText.

Normally, when you want to add a body, header or footer, you can parse the content and check it.
That’s what I’ve done here :

	public function addBodyText(string $text, $plainText = ''): void {
		/**
		 * Custom body text when an user is created.
		 */
		if ($this->emailId === 'settings.Welcome') {
			/**
			 * Catch the text that contains the username
			 * and change it by another wording.
			 */
			if (str_starts_with($text, 'Your username')) {
				$uid = explode(':', $text)[1];

				parent::addBodyText(
					sprintf($this->bodyTextWelcomeAddUser, $uid)
				);

				return;
			}

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

But, with Activity.Notification is not possible. I cannot get this text : https://github.com/nextcloud/server/blob/aff3e65806082e85765630dcdf292d54a7b40277/apps/files_sharing/lib/Activity/Providers/Users.php#L108 .

And I don’t why ?

Honestly, I am failing to find the location in the server to send the actual sharing notifications out. Did you find them yet?