Hi @christianlupus
Thanks for your answer
I don’t think your suggestion can help me
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