Change style for emails

Hi

After I updated my Nextcloud instance to Nextcloud 25, I have problems with the logo in the emails. I have another logo for the header in Nextcloud than on the login screen and this cause a problem. Now when an email is sent from Nextcloud the logo is not looking good, because it takes the background color from the header. Is it possible to change the background for the emails to #ffffff? When I searched I found this in the dokumentation, but because my php skills are very limited, I couldn´t understand how to do it. Email — Nextcloud latest Administration Manual latest documentation

Before Nextcloud 25 it was possible to disable the theming app, but now it seems to be forced to have it enabled and this cause problems. Most of it was possible to solve by enabling the custom css app, but still I can´t change the colors for the search and notification icon.

Hi

Could someone please help me with this issue? What I want to do is to have different style for the email than on the webinterface and apps.

With help off chatgpt I tried to create a file in “lib/public/Mail” called “MyClass.php” based on the documentation. The content was the following just as a test:

<?php

namespace OCA\MyApp;

use OCP\Mail\EMailTemplate;

class MyClass extends EMailTemplate
{
   protected $header = <<<EOF
      <style>
         .logo { display: none !important; }
         .p { font-weight: 700 !important; }
      </style>
   EOF;
}

The I added the following to config.php after mail_smtpport:
'mail_template_class' => '\\OCA\\MyApp\\MyClass',

When I then try to send a test email absolutely nothing has changed. What is wrong with this code? On this setup I run Nextcloud 26.0.3.

Hi

Now I found a solution, but I guess I have to manually check it after every update, because I’m unsure if the change will be permanent. This post helped me: Customize emails - #3 by Etiennehb

Hi @andreasli:

Are you saying you want a different logo for the header or are you saying when you upgraded NC made the logo different and that is now what is causing problems with the background color?

If you’re willing to post some screenshots, it may make it easier for us to troubleshoot.

My understanding is that by default the email templates inherit settings from the theming settings, but I admittedly haven’t experimented with it much. I do recall some things have been refined recently:

https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/theming.html

Also, make sure you have the dependencies installed if you want the logos to match up:

https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/theming.html#theming-of-icons

The in-depth email customization via custom coding is another path, but requires more work (as you’ve seen). And my impression is it’s supported more as an enterprise feature.

Hopefully we can get the built-in theming integration to work more cleanly with your email needs, but I want to confirm I understand the issue first.

Hi

Sorry if I was unclear.

What I want to do is to change the color for the header, but only for the emails. So I want to have the header with background-color #ffffff when sending emails. Otherwise the logo will not look good, because I have two different logos in use. One for the login screen and one for the header inside nextcloud.

I was able to solve it know by using the instruction posted above. Customize emails - #3 by Etiennehb

Hi Andreas,
is this solution still active on your machine?
I can’t believe it’s so hard to make a change to the mail template.

I have tried using the above solution on a Nextcloud Hub 7 (28.0.2)
(with a modified PHP template class in my custom app: 'mail_template_class' => '\\OCA\\MyApp\\MyClass',), but no changes are visible, just as your own results.

Is this configuration option broken in NC 28?
How can I debug this? NC doesn’t give me any hints if it tries to load this custom template.

Hi

I have several Nextcloud instances running and it kind of works, but requires manual steps after each update. I hope that there will be a better solution for this in the future.

This is how I have solved it:

  1. Go to the folder lib/private/Mail/ inside your Nextcloud
    cd lib/private/Mail/
  2. Make a copy of the file EMailTemplate.php
    cp EMailTemplate.php NewEMailTemplate.php
  3. Edit the file NewEMailTemplate.php and look for the section that starts with protected $header = <<<EOF. A few rows below there are a line that starts with <center data-parsed="" Add background:white; So the line looks like this:
    <center data-parsed="" style="background-color:%s;background:white;min-width:175px;max-height:175px; padding:35px 0px;border-radius:200px">
  4. Add this to your config.php
    'mail_template_class' => '\\OC\\Mail\\NewEMailTemplate',
  5. Send a test email from Nextcloud to make sure that it works.

This is just from my notes, so just ask if something is unclear. Remember that all this steps need to be done after each update.

1 Like

Thank you! I have also reached some success in the meantime. I was able to replace the logo.

You don’t need to copy the complete file EMailTemplate.php. Just create an extension class like mentioned in the docs.

It just can’t be used in your custom app’s namespace, that feature seems to be broken. You need to put it in the same folder like the original file:

lib/private/Mail/EMailTemplateModified.php

<?php

namespace OC\Mail;

use OC\Mail\EMailTemplate;

class EMailTemplateModified extends EMailTemplate
{
    public function addHeader(): void
    {
        if ($this->headerAdded) {
            return;
        }
        $this->headerAdded = true;

        $logoUrl = 'https://nextcloud.mydomain.com/custom_apps/myapp/img/logo.png';
        $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName()]);
    }
}

And, of course, set the new class in the config, like described above.
I used a new file for that which is included automatically:

config/additional.config.php

<?php
$CONFIG = array (
    'mail_template_class' => 'OC\Mail\EMailTemplateModified',
);

Hi

Thanks a lot. Now I got it to work. I had some issues at first, because my coding skills are quite limited. But with help of chatgpt I was able to fix the problem. Now I get the emails sent with white backgroud for the logo.