Skip to content

Commit

Permalink
Merge pull request #10477 from nextcloud/fix/issue-10415-send-plain-a…
Browse files Browse the repository at this point in the history
…nd-html

fix: add ability to send alternet text (html and plain)
  • Loading branch information
ChristophWurst authored Jan 15, 2025
2 parents 4db1560 + 3165f07 commit 789ddfe
Show file tree
Hide file tree
Showing 32 changed files with 484 additions and 253 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The rating depends on the installed text processing backend. See [the rating ove
Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud.com/blog/nextcloud-ethical-ai-rating/).
]]></description>
<version>4.2.0-beta.1</version>
<version>4.2.0-beta.2</version>
<licence>agpl</licence>
<author homepage="https://github.com/ChristophWurst">Christoph Wurst</author>
<author homepage="https://github.com/GretaD">GretaD</author>
Expand Down
20 changes: 12 additions & 8 deletions lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public function __construct(string $appName,
public function create(
int $accountId,
string $subject,
string $body,
string $editorBody,
?string $bodyPlain,
?string $bodyHtml,
?string $editorBody,
bool $isHtml,
?bool $smimeSign,
?bool $smimeEncrypt,
Expand All @@ -101,9 +102,10 @@ public function create(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setBodyPlain($bodyPlain);
$message->setBodyHtml($bodyHtml);
$message->setHtml($isHtml);
$message->setEditorBody($editorBody);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setUpdatedAt($this->timeFactory->getTime());
$message->setSendAt($sendAt);
Expand Down Expand Up @@ -146,8 +148,9 @@ public function create(
public function update(int $id,
int $accountId,
string $subject,
string $body,
string $editorBody,
?string $bodyPlain,
?string $bodyHtml,
?string $editorBody,
bool $isHtml,
?bool $smimeSign,
?bool $smimeEncrypt,
Expand All @@ -169,9 +172,10 @@ public function update(int $id,
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setBodyPlain($bodyPlain);
$message->setBodyHtml($bodyHtml);
$message->setHtml($isHtml);
$message->setEditorBody($editorBody);
$message->setFailed($failed);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
Expand Down
11 changes: 9 additions & 2 deletions lib/Controller/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,16 @@ public function send(
$message->setType(LocalMessage::TYPE_OUTGOING);
$message->setAccountId($accountId);
$message->setSubject($subject);
$message->setBody($body);
if ($isHtml) {
$message->setBodyPlain(null);
$message->setBodyHtml($body);
$message->setHtml(true);
} else {
$message->setBodyPlain($body);
$message->setBodyHtml(null);
$message->setHtml(false);
}
$message->setEditorBody($body);
$message->setHtml($isHtml);
$message->setSendAt($this->time->getTime());
$message->setType(LocalMessage::TYPE_OUTGOING);

Expand Down
18 changes: 11 additions & 7 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public function show(int $id): JsonResponse {
public function create(
int $accountId,
string $subject,
string $body,
string $editorBody,
?string $bodyPlain,
?string $bodyHtml,
?string $editorBody,
bool $isHtml,
bool $smimeSign,
bool $smimeEncrypt,
Expand All @@ -118,9 +119,10 @@ public function create(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setBodyPlain($bodyPlain);
$message->setBodyHtml($bodyHtml);
$message->setHtml($isHtml);
$message->setEditorBody($editorBody);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
Expand Down Expand Up @@ -181,7 +183,8 @@ public function update(
int $id,
int $accountId,
string $subject,
string $body,
?string $bodyPlain,
?string $bodyHtml,
?string $editorBody,
bool $isHtml,
bool $smimeSign,
Expand All @@ -207,9 +210,10 @@ public function update(
$message->setAccountId($accountId);
$message->setAliasId($aliasId);
$message->setSubject($subject);
$message->setBody($body);
$message->setEditorBody($editorBody);
$message->setBodyPlain($bodyPlain);
$message->setBodyHtml($bodyHtml);
$message->setHtml($isHtml);
$message->setEditorBody($editorBody);
$message->setInReplyToMessageId($inReplyToMessageId);
$message->setSendAt($sendAt);
$message->setPgpMime($isPgpMime);
Expand Down
18 changes: 12 additions & 6 deletions lib/Db/LocalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
* @method void setSendAt(?int $sendAt)
* @method string getSubject()
* @method void setSubject(string $subject)
* @method string getBody()
* @method void setBody(?string $body)
* @method string getBodyPlain()
* @method void setBodyPlain(?string $bodyPlain)
* @method string getBodyHtml()
* @method void setBodyHtml(?string $bodyHtml)
* @method string|null getEditorBody()
* @method void setEditorBody(string $body)
* @method void setEditorBody(?string $body)
* @method bool isHtml()
* @method void setHtml(bool $html)
* @method bool|null isFailed()
Expand Down Expand Up @@ -88,8 +90,11 @@ class LocalMessage extends Entity implements JsonSerializable {
/** @var string */
protected $subject;

/** @var string */
protected $body;
/** @var string|null */
protected $bodyPlain;

/** @var string|null */
protected $bodyHtml;

/** @var string|null */
protected $editorBody;
Expand Down Expand Up @@ -163,7 +168,8 @@ public function jsonSerialize() {
'sendAt' => $this->getSendAt(),
'updatedAt' => $this->getUpdatedAt(),
'subject' => $this->getSubject(),
'body' => $this->getBody(),
'bodyPlain' => $this->getBodyPlain(),
'bodyHtml' => $this->getBodyHtml(),
'editorBody' => $this->getEditorBody(),
'isHtml' => ($this->isHtml() === true),
'isPgpMime' => ($this->isPgpMime() === true),
Expand Down
42 changes: 42 additions & 0 deletions lib/Migration/Version4200Date20241210000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version4200Date20241210000000 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

$outboxTable = $schema->getTable('mail_local_messages');
if (!$outboxTable->hasColumn('body_plain')) {
$outboxTable->addColumn('body_plain', Types::TEXT, [
'notnull' => false,
]);
}
if (!$outboxTable->hasColumn('body_html')) {
$outboxTable->addColumn('body_html', Types::TEXT, [
'notnull' => false,
]);
}
return $schema;
}
}
75 changes: 75 additions & 0 deletions lib/Migration/Version4200Date20241210000001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Migration;

use Closure;
use Exception;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version4200Date20241210000001 extends SimpleMigrationStep {

/**
* Version4200Date20241210000000 constructor.
*
* @param IDBConnection $connection
*/
public function __construct(
private IDBConnection $db,
) {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @psalm-param Closure():ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
$schema = $schemaClosure();

$outboxTable = $schema->getTable('mail_local_messages');
if ($outboxTable->hasColumn('body') && $outboxTable->hasColumn('body_plain') && $outboxTable->hasColumn('body_html')) {
// copy plain type content to proper column
$qb = $this->db->getQueryBuilder();
$qb->update('mail_local_messages')
->set('body_plain', 'body')
->where($qb->expr()->eq('html', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->executeStatement();
// copy html type content to proper column
$qb = $this->db->getQueryBuilder();
$qb->update('mail_local_messages')
->set('body_html', 'body')
->where($qb->expr()->eq('html', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
->executeStatement();
} else {
throw new Exception('Can not perform migration step, one of the following columns is missing body, body_plain, body_html', 1);
}
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
$schema = $schemaClosure();

$outboxTable = $schema->getTable('mail_local_messages');
if ($outboxTable->hasColumn('body')) {
$outboxTable->dropColumn('body');
}
return $schema;
}
}
11 changes: 7 additions & 4 deletions lib/Provider/Command/MessageSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar
$localMessage->setType($localMessage::TYPE_OUTGOING);
$localMessage->setAccountId($account->getId());
$localMessage->setSubject((string)$message->getSubject());
$localMessage->setBody((string)$message->getBody());
// disabled due to issues caused by opening these messages in gui
//$localMessage->setEditorBody($message->getBody());
$localMessage->setHtml(true);
$localMessage->setBodyPlain($message->getBodyPlain());
$localMessage->setBodyHtml($message->getBodyHtml());
if (!empty($message->getBodyHtml())) {
$localMessage->setHtml(true);
} else {
$localMessage->setHtml(false);
}
$localMessage->setSendAt($this->time->getTime());
// convert mail provider addresses to recipient addresses
$to = $this->convertAddressArray($message->getTo());
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/AntiSpamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function sendReportEmail(Account $account, Mailbox $mailbox, int $uid, st
new DataUriParser()
);
$mimePart = $mimeMessage->build(
true,
null,
$message->getContent(),
$message->getAttachments()
);
Expand Down
10 changes: 7 additions & 3 deletions lib/Service/MailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function sendMessage(Account $account, LocalMessage $localMessage): void
new DataUriParser()
);
$mimePart = $mimeMessage->build(
$localMessage->isHtml(),
$localMessage->getBody(),
$localMessage->getBodyPlain(),
$localMessage->getBodyHtml(),
$attachmentParts,
$localMessage->isPgpMime() === true
);
Expand Down Expand Up @@ -183,7 +183,11 @@ public function saveLocalDraft(Account $account, LocalMessage $message): void {
$imapMessage->setFrom($from);
$imapMessage->setCC($cc);
$imapMessage->setBcc($bcc);
$imapMessage->setContent($message->getBody());
if ($message->isHtml() === true) {
$imapMessage->setContent($message->getBodyHtml());
} else {
$imapMessage->setContent($message->getBodyPlain());
}

foreach ($attachments as $attachment) {
$this->transmissionService->handleAttachment($account, $attachment);
Expand Down
Loading

0 comments on commit 789ddfe

Please sign in to comment.