aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/CalDAV
diff options
context:
space:
mode:
authorSebastianKrupinski <krupinskis05@gmail.com>2024-05-16 19:06:34 -0400
committerSebastianKrupinski <krupinskis05@gmail.com>2024-07-23 16:20:36 -0400
commitfc0b694d379efefc50ba46262bd448abd29da6ed (patch)
treee49d558e18465da8156b1705f682364e1f8e6950 /apps/dav/lib/CalDAV
parentf9d4becf60da69f272f6c5700bbdf5cb99761bc4 (diff)
downloadnextcloud-server-fc0b694d379efefc50ba46262bd448abd29da6ed.tar.gz
nextcloud-server-fc0b694d379efefc50ba46262bd448abd29da6ed.zip
feat: mail provider backend
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
Diffstat (limited to 'apps/dav/lib/CalDAV')
-rw-r--r--apps/dav/lib/CalDAV/Schedule/IMipPlugin.php82
1 files changed, 57 insertions, 25 deletions
diff --git a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
index ef506d1593c..1958531630a 100644
--- a/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
+++ b/apps/dav/lib/CalDAV/Schedule/IMipPlugin.php
@@ -15,6 +15,8 @@ use OCP\Defaults;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Mail\IMailer;
+use OCP\Mail\Provider\IManager as IMailManager;
+use OCP\Mail\Provider\IMessageSend;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin;
@@ -55,6 +57,7 @@ class IMipPlugin extends SabreIMipPlugin {
public const METHOD_CANCEL = 'cancel';
public const IMIP_INDENT = 15; // Enough for the length of all body bullet items, in all languages
private EventComparisonService $eventComparisonService;
+ private IMailManager $mailManager;
public function __construct(IConfig $config,
IMailer $mailer,
@@ -63,7 +66,8 @@ class IMipPlugin extends SabreIMipPlugin {
Defaults $defaults,
IUserSession $userSession,
IMipService $imipService,
- EventComparisonService $eventComparisonService) {
+ EventComparisonService $eventComparisonService,
+ IMailManager $mailManager) {
parent::__construct('');
$this->userSession = $userSession;
$this->config = $config;
@@ -73,6 +77,7 @@ class IMipPlugin extends SabreIMipPlugin {
$this->defaults = $defaults;
$this->imipService = $imipService;
$this->eventComparisonService = $eventComparisonService;
+ $this->mailManager = $mailManager;
}
public function initialize(DAV\Server $server): void {
@@ -212,21 +217,6 @@ class IMipPlugin extends SabreIMipPlugin {
$fromEMail = Util::getDefaultEmailAddress('invitations-noreply');
$fromName = $this->imipService->getFrom($senderName, $this->defaults->getName());
- $message = $this->mailer->createMessage()
- ->setFrom([$fromEMail => $fromName]);
-
- if ($recipientName !== null) {
- $message->setTo([$recipient => $recipientName]);
- } else {
- $message->setTo([$recipient]);
- }
-
- if ($senderName !== null) {
- $message->setReplyTo([$sender => $senderName]);
- } else {
- $message->setReplyTo([$sender]);
- }
-
$template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data);
$template->addHeader();
@@ -268,18 +258,60 @@ class IMipPlugin extends SabreIMipPlugin {
}
$template->addFooter();
-
- $message->useTemplate($template);
-
+ // convert iTip Message to string
$itip_msg = $iTipMessage->message->serialize();
- $message->attachInline(
- $itip_msg,
- 'event.ics',
- 'text/calendar; method=' . $iTipMessage->method,
- );
+
+ $user = null;
+ $mailService = null;
try {
- $failed = $this->mailer->send($message);
+ // retrieve user object
+ $user = $this->userSession->getUser();
+ // evaluate if user object exist
+ if ($user !== null) {
+ // retrieve appropriate service with the same address as sender
+ $mailService = $this->mailManager->findServiceByAddress($user->getUID(), $sender);
+ }
+ // evaluate if a mail service was found and has sending capabilities
+ if ($mailService !== null && $mailService instanceof IMessageSend) {
+ // construct mail message and set required parameters
+ $message = $mailService->initiateMessage();
+ $message->setFrom(
+ (new \OCP\Mail\Provider\Address($sender, $fromName))
+ );
+ $message->setTo(
+ (new \OCP\Mail\Provider\Address($recipient, $recipientName))
+ );
+ $message->setSubject($template->renderSubject());
+ $message->setBodyPlain($template->renderText());
+ $message->setBodyHtml($template->renderHtml());
+ $message->setAttachments((new \OCP\Mail\Provider\Attachment(
+ $itip_msg,
+ 'event.ics',
+ 'text/calendar; method=' . $iTipMessage->method,
+ true
+ )));
+ // send message
+ $mailService->sendMessage($message);
+ } else {
+ // construct symfony mailer message and set required parameters
+ $message = $this->mailer->createMessage();
+ $message->setFrom([$fromEMail => $fromName]);
+ $message->setTo(
+ (($recipientName !== null) ? [$recipient => $recipientName] : [$recipient])
+ );
+ $message->setReplyTo(
+ (($senderName !== null) ? [$sender => $senderName] : [$sender])
+ );
+ $message->useTemplate($template);
+ $message->attachInline(
+ $itip_msg,
+ 'event.ics',
+ 'text/calendar; method=' . $iTipMessage->method
+ );
+ $failed = $this->mailer->send($message);
+ }
+
$iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
if (!empty($failed)) {
$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);