Skip to content

Commit 719f59c

Browse files
feat: mail provider
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 4d15a99 commit 719f59c

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed

developer_manual/digging_deeper/groupware/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Groupware integration
88
calendar
99
calendar_provider
1010
contacts_menu
11+
mail_provider
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
2+
.. _mail-providers:
3+
4+
========================================
5+
Mail Provider Interface
6+
========================================
7+
8+
Nextcloud apps can use, and register the mail provider interface, to provide or consume mail service functionality to and from other apps.
9+
10+
Access to mail services from another app is provided by the Mail Manager class. Using this class your app can find available mail providers and services. Please see the appropriate section below for more details.
11+
12+
The reverse is also possible, one can register an app to supply mail services to other apps. This is accomplished by implementing a custom Mail Provider class and Mail Service class then registering your provider with the system. Please see the appropriate section below for more details.
13+
14+
.. _mail-provider-terminology:
15+
16+
Terminology
17+
-----------
18+
19+
For clarification this is a reference of terminology used.
20+
21+
1. *Provider* - this is the app that provides mail services e.g. "BigHostingApp", this can be an app for a specific protocol
22+
2. *Service* - this is a configured mail service account e.g. "Big Email Hosting Co". Each mail provider may have multiple mail services configured for a user.
23+
24+
.. _mail-provider-consume:
25+
26+
Consuming a Mail Service
27+
------------------------
28+
29+
To use mail as a service provided by another app, your app needs to instantiate the mail manager class, then use the built in methods to list or find an appropriate provider and service.
30+
31+
.. code-block:: php
32+
33+
<?php
34+
35+
use OCP\Mail\Provider\IManager as IMailManager;
36+
use OCP\Mail\Provider\Address;
37+
use OCP\Mail\Provider\Attachment;
38+
39+
class MyTestService {
40+
41+
// Instance the mail manager using dependency injection
42+
public function __construct(IMailManager $mailManager) {
43+
private IMailManager $mailManager;
44+
}
45+
46+
public function acquireMailProvider(): void {
47+
48+
// determine if any providers are available
49+
if (!$this->mailManager->has()) {
50+
return;
51+
}
52+
53+
// retrieve types of providers available (array of id's and labels)
54+
$types = $this->mailManager->types();
55+
56+
// retrieve all providers available (array of provider objects)
57+
$providers = $this->mailManager->providers();
58+
59+
// retrieve a single provider (provider objects)
60+
$provider = $this->mailManager->findProviderById('provider1');
61+
62+
}
63+
64+
public function acquireMailService(): void {
65+
66+
// determine if any providers are available
67+
if (!$this->mailManager->has()) {
68+
return;
69+
}
70+
71+
// retrieve services available for a user (array of service objects)
72+
$services = $this->mailManager->services('user1');
73+
74+
// retrieve a single service with a specific mail address (service objects)
75+
$service = $this->mailManager->findServiceByAddress('[email protected]');
76+
77+
}
78+
79+
public function sendMessage(): void {
80+
81+
// determine if any providers are available
82+
if (!$this->mailManager->has()) {
83+
return;
84+
}
85+
86+
// retrieve a single service with a specific mail address (service objects)
87+
$service = $this->mailManager->findServiceByAddress('[email protected]');
88+
89+
// construct mail message and set required parameters
90+
$message = $service->initiateMessage();
91+
$message->setFrom(new Address('[email protected]', 'User One'));
92+
$message->setTo(new Address('[email protected]', 'User Two'));
93+
$message->setSubject('Our Great Plan');
94+
$message->setBodyPlain('See the attached itinerary for our great plan');
95+
$message->setBodyHtml('<html>See the attached itinerary for our great plan</html>');
96+
$message->setAttachments(new Attachment(
97+
'Our great plan itinerary',
98+
'theplan.txt',
99+
'text/plain'
100+
));
101+
// send message
102+
$service->sendMessage($message);
103+
104+
}
105+
}
106+
107+
For more detailed information of methods available, parameters and returns please see the mail providers directory in the server repository. (lib/public/Mail/Provider)
108+
109+
.. _mail-provider-provide:
110+
111+
Providing a Mail Service
112+
------------------------
113+
114+
For your app to provide mail service to other apps, your app needs to implement two main interfaces plus interfaces for the supported functionality.
115+
116+
Step 1: Create a Mail Provider Class
117+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118+
119+
The mail provider class is the main class that the mail manager uses to retrieve available services from your app. Each mail provider can have multiple mail services configured for a user.
120+
121+
This class needs to implement the `IProvider` interface and have all the required methods defined.
122+
123+
.. code-block:: php
124+
125+
namespace OCA\BigHostingApp\Provider;
126+
127+
use OCP\Mail\Provider\IProvider;
128+
use OCP\Mail\Provider\IService;
129+
130+
class MailProvider implements IProvider {
131+
132+
public function id(): string {
133+
return 'big-hosting-app';
134+
}
135+
136+
public function label(): string {
137+
return 'Big Hosting App';
138+
}
139+
140+
public function hasServices(string $userId): bool {
141+
// app specific code to check for available services
142+
}
143+
144+
public function listServices(string $userId): array {
145+
// app specific code to list all available services
146+
}
147+
148+
public function findServiceById(string $userId, string $serviceId): IService | null {
149+
// app specific code to find a specific services
150+
}
151+
152+
public function findServiceByAddress(string $userId, string $address): IService | null {
153+
// app specific code to find a service with a specific email address
154+
}
155+
156+
}
157+
158+
Step 2: Create a Mail Service Class
159+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160+
161+
The mail service class is the main class that other apps use to access mail functionality in your app. This class is also returned by the mail provider class.
162+
163+
This class needs to implement the `IService` interface and have all the required methods defined. Because functionality varies between protocols this class also needs to be extended with the appropriate supported function interfaces like 'IMessageSend' which provides mail sending capabilities.
164+
165+
.. code-block:: php
166+
167+
namespace OCA\BigHostingApp\Provider;
168+
169+
use OCP\Mail\Provider\Address;
170+
use OCP\Mail\Provider\IAddress;
171+
use OCP\Mail\Provider\IMessage;
172+
use OCP\Mail\Provider\IMessageSend;
173+
use OCP\Mail\Provider\IService;
174+
use OCP\Mail\Provider\Message;
175+
176+
class MailService implements IService, IMessageSend {
177+
178+
public function id(): string {
179+
return '1 or service1 or anything else';
180+
}
181+
182+
public function capable(string $value): bool {
183+
// app specific code to check if a service is capable of perform a specific function e.g. Sending a Message
184+
}
185+
186+
public function capabilities(): array {
187+
// app specific code to retrieve a list of capabilities
188+
}
189+
190+
public function getLabel(): string {
191+
// app specific code to retrieve the label/description/name of the service
192+
}
193+
194+
public function getPrimaryAddress(): IAddress {
195+
// app specific code to retrieve the primary email address of the service
196+
}
197+
198+
public function getSecondaryAddresses(): array {
199+
// app specific code to retrieve the secondary email addresses (aliases) of the service
200+
}
201+
202+
public function initiateMessage(): IMessage {
203+
// app specific code to create a fresh message e.g message object to send a message or save a message in drafts
204+
}
205+
206+
// this function is the extended capabilities added to this class from IMessageSend
207+
public function sendMessage(IMessage $message, array $option = []): void {
208+
// app specific code to send a message
209+
}
210+
211+
}
212+
213+
214+
Step 3: Register the Mail Provider
215+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216+
217+
The registration is performed at the initial stages of your app being loaded by the Nextcloud system, inside the 'AppInfo/Application.php' file
218+
219+
.. code-block:: php
220+
221+
namespace OCA\BigHostingApp\AppInfo;
222+
223+
use OCA\BigHostingApp\Provider\MailProvider;
224+
use OCP\AppFramework\App;
225+
use OCP\AppFramework\Bootstrap\IRegistrationContext;
226+
227+
class Application extends App {
228+
229+
public const APP_ID = 'BigHostingApp';
230+
231+
public function __construct(array $urlParams = []) {
232+
parent::__construct(self::APP_ID, $urlParams);
233+
}
234+
235+
public function register(IRegistrationContext $context): void {
236+
237+
// Tip: If your app spans multiple version of Nextcloud, we recommend to make sure the method exists with 'method_exists()'
238+
$context->registerMailProvider(MailProvider::class);
239+
240+
}
241+
242+
}

0 commit comments

Comments
 (0)