Nextcloud has a mailer component to send email from an admin-defined account.

Basic usage

The mailer is hidden behind the \OCP\Mail\IMailer interface that can be injected:


use OCP\Mail\IMailer;

class MailService {
    private IMailer $mailer;

    public function __construct(IMailer $mailer) {
        $this->mailer = $mailer;

    public function notify(string $email): void {
        $message = $this->mailer->createMessage();
        $message->setSubject("Hello from Nextcloud");
        $message->setPlainBody("This is some text");
            "<!doctype html><html><body>This is some <b>text</b></body></html>"

Inline attachments

Inline attachments can be appended to a message with IMessage::attachInline:

/** @var IMessage $message */
    "this is a test", // Body
    "test.txt",       // Name
    "text/plain"      // Content type