Encoding problem with outgoing messages
Steps to reproduce
1.Hello, I just realized that I have a formatting problem with sending emails via the Mail application. I am on version 2.0.3. Formatting is enabled in the app. On Nextcloud version 25.0.0 2. 3.
Expected behavior
Correct text formatting
Actual behavior
For exemple :
Je reste à votre disposition pour tout complément d'information.
Mail app version
2.0.3
Mailserver or service
IMAP
Operating system
Ubuntu 20.04
PHP engine version
PHP 7.4
Web server
Apache (supported)
Database
PostgreSQL
Additional info
Hello, I just realized that I have a formatting problem with sending emails via the Mail application. I am on version 2.0.3. Formatting is enabled in the app.
For exemple : Un exemple : Je reste à votre disposition pour tout complément d'information.
I performed a test on version 1.6.3 on Nextcloud v24.0.6 and there is no problem.
Happens to me also on Nextcloud 25.0.0 with Mail v2.0.3. It didn't happen on Nextcloud 24.x
All accents are broken. Let me know if you need someone to test potential fixes/configuration changes.
Does anyone know a way to fix this fault? Let me mention that the national characters in the subject line are correct. Only in the text of the message, the recipient gets strange letters. I will be grateful. I like to use this app to reply to email.
Hello, it's a critical issue for many people. The application is broken for a lot of non-English communities. We can just send plain text.
There is also a problem with the content preview of incoming mail (in the mails list). For instance, accent characters are replaced by '?'.
In fact, the problem seems to be appearing when the mail is in rich text mode.
Thanks for your contribution to this project and your work.
Same problem in spanish, nextcloud 25.0.1, and mail app 2.1.0, the problem is not solved in this version.
it's a critical issue.

Outgoing messages are temporarily stored in a database table oc_mail_local_messages. Could this be an issue with the utf8 support of your db?
Check https://docs.nextcloud.com/server/stable/admin_manual/configuration_database/mysql_4byte_support.html if you use mysql/mariadb.
Good morning, that does not seem to be the problem, I have additionally performed the process described in the link, but the problem continues.

Compose a new message an schedule it for tomorrow so that it isn't sent immediately. Then inspect the latest row in oc_mail_local_message. Is the message body encoding correct?
Hello, The mail is correctly formatting in oc_mail_local_messages table but the error is still present after sending the message.
Compose a new message an schedule it for tomorrow so that it isn't sent immediately. Then inspect the latest row in oc_mail_local_message. Is the message body encoding correct?
Thanks, I will try it after hours, I have reverted the snapshot to make everything work correctly in version 24.0.7.
Tried to reproduce after I had this issue myself while testing something else.
DB:
)
Message:

DB entry (I think the question marks are down to PHPStroms table inspector, not due to an error):
10,0,31,,1667832900,Testing ÄÖÜ,"<p style=""margin:0;"">ÄÜÜÜÜ ÜÖÖÖÖÖ LMAO</p><p style=""margin:0;""> </p><p style=""margin:0;""><span style=""background-color:rgb(255,255,255);color:rgb(51,51,51);"">????????????????????</span></p>",1,,0,"<p style=""margin:0;"">ÄÜÜÜÜ ÜÖÖÖÖÖ LMAO</p><p style=""margin:0;""> </p><p style=""margin:0;""><span style=""background-color:rgb(255,255,255);color:rgb(51,51,51);"">????????????????????</span></p>"
Message in OutboxController::send:

Same in OutboxService::sendMessage, MailTransmission::send, etc... right until \Horde_Mime_Headers::toArray.
Final send headers:
From: admin <[email protected]>
To: [email protected]
Cc:
Subject: Testing =?utf-8?b?w4TDlsOc?=
Message-ID: <[email protected]>
User-Agent: Horde Application Framework 5
Date: Mon, 07 Nov 2022 13:37:33 +0000
Content-Type: multipart/alternative; boundary="=_s7DfCJ4VzDrqlwfw0yHJqbw"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
The problem is visible when reading the stream in \Horde_Smtp_Connection::write. It's also recorded with the broken UTF-8 chars in the Horde Log.
Sent chunk:
From: admin <[email protected]>
To: "Alice ([email protected])" <[email protected]>
Cc:
Subject: Testing =?utf-8?b?w4TDlsOc?=
Message-ID: <[email protected]>
User-Agent: Horde Application Framework 5
Date: Mon, 07 Nov 2022 13:54:00 +0000
Content-Type: multipart/alternative; boundary="=_rremQ8vRS8oMsnrOYm3UVxj"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
This message is in MIME format.
--=_rremQ8vRS8oMsnrOYm3UVxj
Content-Type: text/plain; charset=utf-8
Content-Description: Plaintext Version of Message
Content-Transfer-Encoding: 8bit
ÃÃÃÃÃ ÃÃÃÃÃÃ LMAO
ððððððððððððððððððð
--=_rremQ8vRS8oMsnrOYm3UVxj
Content-Type: text/html; charset=utf-8
Content-Description: HTML Version of Message
<p style="margin:0;">ÄÜÜÜÜ ÜÖÖÖÖÖ LMAO<p style="margin:0;"> </p><p style="margin:0;"><span style="background-color:rgb(255,255,255);color:rgb(51,51,51);">😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊</span></p></p>
--=_rremQ8vRS8oMsnrOYm3UVxj--
Final result (in the Web Client for GMX):

Stacktrace (from cron, but underlying trace is the same from Controller directly):
Connection.php:41, Horde_Smtp_Connection->write()
Smtp.php:835, Horde_Smtp->_hello()
Smtp.php:421, Horde_Smtp->login()
Smtphorde.php:160, Horde_Mail_Transport_Smtphorde->getSMTPObject()
Part.php:1581, Horde_Mime_Part->send()
Mail.php:479, Horde_Mime_Mail->send()
MailTransmission.php:207, OCA\Mail\Service\MailTransmission->sendMessage()
MailTransmission.php:275, OCA\Mail\Service\MailTransmission->sendLocalMessage()
OutboxService.php:131, OCA\Mail\Service\OutboxService->sendMessage()
OutboxService.php:224, OCA\Mail\Service\OutboxService->flush()
OutboxWorkerJob.php:53, OCA\Mail\BackgroundJob\OutboxWorkerJob->run()
Job.php:78, OCP\BackgroundJob\Job->start()
TimedJob.php:103, OCP\BackgroundJob\TimedJob->start()
TimedJob.php:93, OCP\BackgroundJob\TimedJob->execute()
cron.php:152, {main}()
Can y'all please check your writing mode?

Ok, debug finished:
For everyone needing a quick fix, please check your account settings and set your writing mode. That will fix the issue.
As a code fix, looks like the default assumption when the writing mode is not set is $isHtmlMessage = true which then mangles your messages through the Html2text filter:

The default writing mode should probably be set to false as a default.
set your writing mode. That will fix the issue.
Can't confirm... The mode was already set to Rich Text. I switched it to Plain Text and back to Rich Text, but this didn't fix the issue. In Plain Text mode, everything is correctly encoded.
Good to know, so the issue is with rich text and possibly CKEditor. @kesselb you're the expert there :)
Compose a new message an schedule it for tomorrow so that it isn't sent immediately. Then inspect the latest row in oc_mail_local_message. Is the message body encoding correct?
I would like to confirm what @d911 said. The message body encoding in oc_mail_local_messages table is correct, but after the message is sent the body encoding in oc_mail_messages is wrong. See the screenshots attached (an example with Greek characters).
Please note that this issue occurs only in Rich Text mode and not in Plain Text mode.
At least we can safely say that we have pinpointed the problem.

I have looked into it.
Sounds this problem happens right before sending via SMTP (some 4 bytes encoding getting lost somehow).
But also for all previews of incoming html only messages...

Note this is really showstopper for us, as rich text is enabled by default when you answer to html message (whatever the option chosen in the config). You have to think to remove formatting for virtually any answer.... I would rank this as high priority
might be linked to https://github.com/nextcloud/mail/issues/7408 but. i am not sure if the fix works for this issue too
Thanks for your reports :+1:
Would you mind trying https://github.com/nextcloud/mail/pull/7575 and tell us if it fixes the encoding issues?