Skip to content

Commit

Permalink
fix(mail): Fix big logos in mail templates for Outlook
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 10, 2024
1 parent e31f474 commit ec3f2a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
9 changes: 9 additions & 0 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ public function updateImage(string $key, string $tmpFile): string {

$target->putContent(file_get_contents($tmpFile));

if ($key === 'logo') {
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
if ($newImage === false) {
throw new \Exception('Could not read background image, possibly corrupted.');
}

$this->config->setAppValue('theming', 'logoDimensions', imagesx($newImage) . 'x' . imagesy($newImage));

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::setAppValue has been marked as deprecated
}

return $detectedMimeType;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EMailTemplate implements IEMailTemplate {
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
<center data-parsed="" style="background-color:%s;min-width:175px;max-height:175px; padding:35px 0px;border-radius:200px">
<img class="logo float-center" src="%s" alt="%s" align="center" style="-ms-interpolation-mode:bicubic;clear:both;display:block;float:none;margin:0 auto;outline:0;text-align:center;text-decoration:none;max-height:105px;max-width:105px;width:auto;height:auto">
<img class="logo float-center" src="%s" alt="%s" align="center" style="-ms-interpolation-mode:bicubic;clear:both;display:block;float:none;margin:0 auto;outline:0;text-align:center;text-decoration:none;max-height:105px;max-width:105px;width:auto;height:auto" width="%s" height="%s">
</center>
</tr>
</tbody>
Expand Down Expand Up @@ -308,6 +308,8 @@ public function __construct(
protected Defaults $themingDefaults,
protected IURLGenerator $urlGenerator,
protected IFactory $l10nFactory,
protected int $logoWidth,
protected int $logoHeight,
protected string $emailId,
protected array $data,
) {
Expand All @@ -331,7 +333,7 @@ public function addHeader(): void {
$this->headerAdded = true;

$logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false));
$this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName()]);
$this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName(), $this->logoWidth, $this->logoHeight]);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
* @package OC\Mail
*/
class Mailer implements IMailer {
// Do not touch these lines to not break patching :)
public const DEFAULT_DIMENSIONS = '252x120';
public const MAX_LOGO_SIZE = 105;
// Do not touch these lines to not break patching :)

private ?MailerInterface $instance = null;

public function __construct(
Expand Down Expand Up @@ -109,10 +114,33 @@ public function createEMailTemplate(string $emailId, array $data = []): IEMailTe
);
}

$logoDimensions = $this->config->getAppValue('theming', 'logoDimensions', self::DEFAULT_DIMENSIONS);
[$width, $height] = explode('x', $logoDimensions);
$width = (int) $width;
$height = (int) $height;

if ($width > self::MAX_LOGO_SIZE || $height > self::MAX_LOGO_SIZE) {
if ($width === $height) {
$logoWidth = self::MAX_LOGO_SIZE;
$logoHeight = self::MAX_LOGO_SIZE;
} elseif ($width > $height) {
$logoWidth = self::MAX_LOGO_SIZE;
$logoHeight = (int) (($height / $width) * self::MAX_LOGO_SIZE);
} else {
$logoWidth = (int) (($width / $height) * self::MAX_LOGO_SIZE);
$logoHeight = self::MAX_LOGO_SIZE;
}
} else {
$logoWidth = $width;
$logoHeight = $height;
}

return new EMailTemplate(
$this->defaults,
$this->urlGenerator,
$this->l10nFactory,
$logoWidth,
$logoHeight,
$emailId,
$data
);
Expand Down

0 comments on commit ec3f2a7

Please sign in to comment.