Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory as L10NFactory;
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\IUser;
Expand Down Expand Up @@ -131,6 +132,7 @@ public function send(VEvent $vevent,
}
$message->setTo([$emailAddress]);
$message->useTemplate($template);
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);

try {
$failed = $this->mailer->send($message);
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/lib/Mailer/NewUserMailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\L10N\IFactory;
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Security\ICrypto;
Expand Down Expand Up @@ -181,6 +182,7 @@ public function sendMail(IUser $user,
$message->setTo([$email => $user->getDisplayName()]);
$message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]);
$message->useTemplate($emailTemplate);
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
$this->mailer->send($message);
}
}
5 changes: 5 additions & 0 deletions apps/settings/tests/Mailer/NewUserMailHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\L10N\IFactory;
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Security\ICrypto;
Expand Down Expand Up @@ -867,6 +868,10 @@ public function testSendMail() {
->expects($this->once())
->method('useTemplate')
->with($emailTemplate);
$message
->expects($this->once())
->method('setAutoSubmitted')
->with(AutoSubmitted::VALUE_AUTO_GENERATED);
$this->defaults
->expects($this->once())
->method('getName')
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@
'OCP\\Log\\IWriter' => $baseDir . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => $baseDir . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\Events\\BeforeMessageSent' => $baseDir . '/lib/public/Mail/Events/BeforeMessageSent.php',
'OCP\\Mail\\Headers\\AutoSubmitted' => $baseDir . '/lib/public/Mail/Headers/AutoSubmitted.php',
'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
Expand Down
27 changes: 10 additions & 17 deletions lib/composer/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,18 @@ public static function getLoader()

$loader->register(true);

$includeFiles = \Composer\Autoload\ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire749170dad3f5e7f9ca158f5a9f04f6a2($fileIdentifier, $file);
$filesToLoad = \Composer\Autoload\ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2::$files;
$requireFile = static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

require $file;
}
};
foreach ($filesToLoad as $fileIdentifier => $file) {
($requireFile)($fileIdentifier, $file);
}

return $loader;
}
}

/**
* @param string $fileIdentifier
* @param string $file
* @return void
*/
function composerRequire749170dad3f5e7f9ca158f5a9f04f6a2($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

require $file;
}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Log\\IWriter' => __DIR__ . '/../../..' . '/lib/public/Log/IWriter.php',
'OCP\\Log\\RotationTrait' => __DIR__ . '/../../..' . '/lib/public/Log/RotationTrait.php',
'OCP\\Mail\\Events\\BeforeMessageSent' => __DIR__ . '/../../..' . '/lib/public/Mail/Events/BeforeMessageSent.php',
'OCP\\Mail\\Headers\\AutoSubmitted' => __DIR__ . '/../../..' . '/lib/public/Mail/Headers/AutoSubmitted.php',
'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php',
'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
Expand Down
38 changes: 38 additions & 0 deletions lib/private/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
namespace OC\Mail;

use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IAttachment;
use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMessage;
Expand Down Expand Up @@ -301,4 +302,41 @@ public function useTemplate(IEMailTemplate $emailTemplate): IMessage {
}
return $this;
}

/**
* Add the Auto-Submitted header to the email, preventing most automated
* responses to automated messages.
*
* @param AutoSubmitted::VALUE_* $value (one of AutoSubmitted::VALUE_NO, AutoSubmitted::VALUE_AUTO_GENERATED, AutoSubmitted::VALUE_AUTO_REPLIED)
* @return $this
*/
public function setAutoSubmitted(string $value): IMessage {
$headers = $this->swiftMessage->getHeaders();

if ($headers->has(AutoSubmitted::HEADER)) {
// if the header already exsists, remove it.
// the value can be modified with some implementations
// of the interface \Swift_Mime_Header, however the
// interface doesn't, and this makes the static-code
// analysis unhappy.
$headers->remove(AutoSubmitted::HEADER);
}

$headers->addTextHeader(AutoSubmitted::HEADER, $value);

return $this;
}

/**
* Get the current value of the Auto-Submitted header. Defaults to "no"
* which is equivalent to the header not existing at all
*
* @return string
*/
public function getAutoSubmitted(): string {
$headers = $this->swiftMessage->getHeaders();

return $headers->has(AutoSubmitted::HEADER) ?
$headers->get(AutoSubmitted::HEADER)->toString() : AutoSubmitted::VALUE_NO;
}
}
75 changes: 75 additions & 0 deletions lib/public/Mail/Headers/AutoSubmitted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Bennet Becker <[email protected]>
*
* @author Bennet Becker <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Mail\Headers;

/**
* Keyword values for the Auto-Submitted email header, as per RFC 3834.
*
* The value "auto-notified" as per RFC 5436 is deliberately omitted as it is
* meant of notification of the sieve system.
*
* @link https://www.iana.org/assignments/auto-submitted-keywords/auto-submitted-keywords.xhtml
*
* @since 26.0.0
*/
final class AutoSubmitted {

/**
* Name of the Header as used in the final message later
*
* @var string
* @since 26.0.0
*/
public const HEADER = 'Auto-Submitted';

/**
* Indicates that a message was NOT automatically generated, but was
* created by a human (or following human interaction). It is the equivalent
* to the absence of an Auto-Submitted header altogether.
*
* @var string
* @since 26.0.0
*/
public const VALUE_NO = 'no';

/**
* Indicates that a message was generated by an automatic process, and is
* not a direct response to another message
*
* @var string
* @since 26.0.0
*/
public const VALUE_AUTO_GENERATED = 'auto-generated';

/**
* Indicates that a message was automatically generated as a direct response
* to another message.
*
* @var string
* @since 26.0.0
*/
public const VALUE_AUTO_REPLIED = 'auto-replied';
}
10 changes: 10 additions & 0 deletions lib/public/Mail/IMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ public function setBcc(array $recipients): IMessage;
* @since 13.0.0
*/
public function useTemplate(IEMailTemplate $emailTemplate): IMessage;

/**
* Add the Auto-Submitted header to the email, preventing most automated
* responses to automated messages.
*
* @param Headers\AutoSubmitted::VALUE_* $value (one of AutoSubmitted::VALUE_NO, AutoSubmitted::VALUE_AUTO_GENERATED, AutoSubmitted::VALUE_AUTO_REPLIED)
* @return IMessage
* @since 26.0.0
*/
public function setAutoSubmitted(string $value): IMessage;
}
107 changes: 107 additions & 0 deletions tests/lib/Mail/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Test\Mail;

use OC\Mail\Message;
use OCP\Mail\Headers\AutoSubmitted;
use OCP\Mail\IEMailTemplate;
use Swift_Message;
use Test\TestCase;
Expand Down Expand Up @@ -263,4 +264,110 @@ public function testBothRenderingOptions() {

$message->useTemplate($template);
}

public function testSetAutoSubmitted1() {
$swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet')
->disableOriginalConstructor()
->getMock();
$swiftMessage = $this->getMockBuilder('\Swift_Message')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();

$swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet);

$swiftMimeSimpleHeaderSet->expects($this->once())
->method('has')
->with('Auto-Submitted');
$swiftMimeSimpleHeaderSet->expects($this->never())
->method('remove');
$swiftMimeSimpleHeaderSet->expects($this->once())
->method('addTextHeader')
->with('Auto-Submitted', AutoSubmitted::VALUE_AUTO_GENERATED);

$message = new Message($swiftMessage, false);
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
}

public function testSetAutoSubmitted2() {
$swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet')
->disableOriginalConstructor()
->getMock();
$swiftMessage = $this->getMockBuilder('\Swift_Message')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();

$swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet);

$swiftMimeSimpleHeaderSet->expects($this->once())
->method('has')
->with('Auto-Submitted')
->willReturn(true);
$swiftMimeSimpleHeaderSet->expects($this->once())
->method('remove')
->with('Auto-Submitted');
$swiftMimeSimpleHeaderSet->expects($this->once())
->method('addTextHeader')
->with('Auto-Submitted', AutoSubmitted::VALUE_AUTO_GENERATED);

$message = new Message($swiftMessage, false);
$message->setAutoSubmitted(AutoSubmitted::VALUE_AUTO_GENERATED);
}

public function testGetAutoSubmitted1() {
$swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet')
->disableOriginalConstructor()
->getMock();
$swiftMessage = $this->getMockBuilder('\Swift_Message')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();

$swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet);

$swiftMimeSimpleHeaderSet->expects($this->once())
->method('has')
->with('Auto-Submitted');
$swiftMimeSimpleHeaderSet->expects($this->never())
->method('get');

$message = new Message($swiftMessage, false);
$this->assertSame("no", $message->getAutoSubmitted());
}
public function testGetAutoSubmitted2() {
$swiftMimeHeader = $this->getMockBuilder('\Swift_Mime_Header')
->disableOriginalConstructor()
->getMockForAbstractClass();
$swiftMimeSimpleHeaderSet = $this->getMockBuilder('\Swift_Mime_SimpleHeaderSet')
->disableOriginalConstructor()
->getMock();
$swiftMessage = $this->getMockBuilder('\Swift_Message')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();


$swiftMessage->method('getHeaders')->willReturn($swiftMimeSimpleHeaderSet);
$swiftMimeHeader->method('toString')->willReturn(AutoSubmitted::VALUE_AUTO_GENERATED);

$swiftMimeSimpleHeaderSet->expects($this->once())
->method('has')
->with('Auto-Submitted')
->willReturn(true);
$swiftMimeSimpleHeaderSet->expects($this->once())
->method('get')
->willReturn($swiftMimeHeader);

$message = new Message($swiftMessage, false);
$this->assertSame(AutoSubmitted::VALUE_AUTO_GENERATED, $message->getAutoSubmitted());
}
}