Skip to content

Commit

Permalink
Merge pull request #230 from Mohamed1226/support_non_ascii_chars
Browse files Browse the repository at this point in the history
Support non ascii chars
  • Loading branch information
robert-virkus authored Jan 20, 2024
2 parents 74fda6e + 28e2a81 commit d4c1e8c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 27 deletions.
41 changes: 33 additions & 8 deletions lib/src/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -925,14 +925,17 @@ class MailClient {
final message = messageBuilder.buildMimeMessage();
final use8Bit = builderEncoding == TransferEncoding.eightBit;


return sendMessage(
message,
from: from,
appendToSent: appendToSent,
supportUnicode: supportUnicode
sentMailbox: sentMailbox,
use8BitEncoding: use8Bit,
recipients: recipients,
);

}

/// Sends the specified [message].
Expand All @@ -956,20 +959,24 @@ class MailClient {
///
/// Optionally specify the [sentMailbox] when the mail system does not
/// support mailbox flags.
/// first
Future<void> sendMessage(
MimeMessage message, {
MailAddress? from,
bool appendToSent = true,
bool supportUnicode = false,
Mailbox? sentMailbox,
bool use8BitEncoding = false,
List<MailAddress>? recipients,
}) async {
await _prepareConnect();
final futures = <Future>[

_outgoingLock.synchronized(
() =>
_sendMessageViaOutgoing(message, from, use8BitEncoding, recipients),
_sendMessageViaOutgoing(message, from, use8BitEncoding, recipients, supportUnicode: supportUnicode),
),

];
if (appendToSent && _incomingMailClient.supportsAppendingMessages) {
sentMailbox ??= getMailbox(MailboxFlag.sent);
Expand All @@ -988,21 +995,26 @@ class MailClient {
}
}

await Future.wait(futures);

return Future.wait(futures);
}


Future _sendMessageViaOutgoing(
MimeMessage message,
MailAddress? from,
bool use8BitEncoding,
List<MailAddress>? recipients,
{bool supportUnicode = false}
) async {
await _outgoingMailClient.sendMessage(
message,
from: from,
use8BitEncoding: use8BitEncoding,
supportUnicode: supportUnicode,
recipients: recipients,
);

await _outgoingMailClient.disconnect();
}

Expand Down Expand Up @@ -1676,9 +1688,13 @@ enum ThreadPreference {

abstract class _IncomingMailClient {
_IncomingMailClient(this.downloadSizeLimit, this._config, this.mailClient);

final MailClient mailClient;

ClientBase get client;

ServerType get clientType;

int? downloadSizeLimit;
MailServerConfig _config;
Mailbox? _selectedMailbox;
Expand Down Expand Up @@ -1851,6 +1867,7 @@ class _IncomingImapClient extends _IncomingMailClient {

@override
ClientBase get client => _imapClient;

@override
ServerType get clientType => ServerType.imap;
final ImapClient _imapClient;
Expand All @@ -1863,9 +1880,11 @@ class _IncomingImapClient extends _IncomingMailClient {
int _reconnectCounter = 0;
bool _isIdlePaused = false;
ThreadDataResult? _threadData;

@override
bool get supportsMailboxes => true;
Id? _serverId;

@override
Id? get serverId => _serverId;

Expand Down Expand Up @@ -3257,6 +3276,7 @@ class _IncomingPopClient extends _IncomingMailClient {

@override
ClientBase get client => _popClient;

@override
ServerType get clientType => ServerType.pop;

Expand Down Expand Up @@ -3551,6 +3571,7 @@ abstract class _OutgoingMailClient {
: _mailConfig = mailConfig;

ClientBase get client;

ServerType get clientType;
MailServerConfig _mailConfig;

Expand All @@ -3559,12 +3580,13 @@ abstract class _OutgoingMailClient {
/// Is only correct after authorizing.
Future<bool> supports8BitEncoding();

Future<void> sendMessage(
MimeMessage message, {
MailAddress? from,
bool use8BitEncoding = false,
List<MailAddress>? recipients,
});

Future<void> sendMessage(MimeMessage message,
{required bool supportUnicode,
MailAddress? from,
bool use8BitEncoding = false,
List<MailAddress>? recipients});


Future<void> disconnect();
}
Expand All @@ -3590,6 +3612,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient {

@override
ClientBase get client => _smtpClient;

@override
ServerType get clientType => ServerType.smtp;
final MailClient mailClient;
Expand Down Expand Up @@ -3635,6 +3658,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient {
@override
Future<void> sendMessage(
MimeMessage message, {
required bool supportUnicode,
MailAddress? from,
bool use8BitEncoding = false,
List<MailAddress>? recipients,
Expand All @@ -3645,6 +3669,7 @@ class _OutgoingSmtpClient extends _OutgoingMailClient {
await _smtpClient.sendChunkedMessage(
message,
from: from,
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
recipients: recipients,
);
Expand Down
34 changes: 20 additions & 14 deletions lib/src/private/smtp/commands/smtp_send_bdat_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class _SmtpSendBdatCommand extends SmtpCommand {
this.fromEmail,
this.recipientEmails, {
required this.use8BitEncoding,
required this.supportUnicode,
}) : super('MAIL FROM') {
final binaryData = _codec.encode(getData());
_chunks = chunkData(binaryData);
Expand All @@ -24,6 +25,7 @@ class _SmtpSendBdatCommand extends SmtpCommand {
final String? fromEmail;
final List<String> recipientEmails;
final bool use8BitEncoding;
final bool supportUnicode;
_BdatSequence _currentStep = _BdatSequence.mailFrom;
int _recipientIndex = 0;
late List<Uint8List> _chunks;
Expand Down Expand Up @@ -54,6 +56,10 @@ class _SmtpSendBdatCommand extends SmtpCommand {

@override
String get command {
if (supportUnicode) {
print('supportUnicode $supportUnicode');
return 'MAIL FROM:<$fromEmail> SMTPUTF8';
}
if (use8BitEncoding) {
return 'MAIL FROM:<$fromEmail> BODY=8BITMIME';
}
Expand Down Expand Up @@ -116,14 +122,16 @@ class SmtpSendBdatMailCommand extends _SmtpSendBdatCommand {
MailAddress? from,
List<String> recipientEmails, {
required bool use8BitEncoding,
required bool supportUnicode,
}) : super(
() => message
.renderMessage()
.replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''),
from?.email ?? message.fromEmail,
recipientEmails,
use8BitEncoding: use8BitEncoding,
);
supportUnicode: supportUnicode,
);

/// The message to be sent
final MimeMessage message;
Expand All @@ -137,14 +145,15 @@ class SmtpSendBdatMailDataCommand extends _SmtpSendBdatCommand {
MailAddress from,
List<String> recipientEmails, {
required bool use8BitEncoding,
required bool supportUnicode,
}) : super(
() => data
.toString()
.replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''),
from.email,
recipientEmails,
use8BitEncoding: use8BitEncoding,
);
() => data
.toString()
.replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''),
from.email,
recipientEmails,
use8BitEncoding: use8BitEncoding,
supportUnicode: supportUnicode);

/// The message data to be sent
final MimeData data;
Expand All @@ -158,12 +167,9 @@ class SmtpSendBdatMailTextCommand extends _SmtpSendBdatCommand {
MailAddress from,
List<String> recipientEmails, {
required bool use8BitEncoding,
}) : super(
() => data,
from.email,
recipientEmails,
use8BitEncoding: use8BitEncoding,
);
required bool supportUnicode,
}) : super(() => data, from.email, recipientEmails,
use8BitEncoding: use8BitEncoding, supportUnicode: supportUnicode);

/// The message text data
final String data;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/private/smtp/commands/smtp_sendmail_command.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import '../../../../enough_mail.dart';
import '../smtp_command.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/src/private/util/client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ abstract class ClientBase {
/// Disconnects from the service
Future<void> disconnect() async {
if (_isConnected) {
logApp('disconnecting');
isLoggedIn = false;
_isConnected = false;
isSocketClosingExpected = true;
Expand Down
9 changes: 6 additions & 3 deletions lib/src/smtp/smtp_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class SmtpClient extends ClientBase {
/// defined in the message are ignored.
Future<SmtpResponse> sendChunkedMessage(
MimeMessage message, {
required bool supportUnicode,
bool use8BitEncoding = false,
MailAddress? from,
List<MailAddress>? recipients,
Expand All @@ -312,7 +313,7 @@ class SmtpClient extends ClientBase {
throw SmtpException(this, SmtpResponse(['500 no recipients']));
}
return sendCommand(SmtpSendBdatMailCommand(message, from, recipientEmails,
use8BitEncoding: use8BitEncoding));
use8BitEncoding: use8BitEncoding, supportUnicode: supportUnicode));
}

/// Sends the specified message [data] [from] to the [recipients]
Expand All @@ -325,7 +326,7 @@ class SmtpClient extends ClientBase {
/// Set [use8BitEncoding] to `true` for sending a UTF-8 encoded message body.
Future<SmtpResponse> sendChunkedMessageData(
MimeData data, MailAddress from, List<MailAddress> recipients,
{bool use8BitEncoding = false}) {
{ required bool supportUnicode,bool use8BitEncoding = false}) {
if (recipients.isEmpty) {
throw SmtpException(this, SmtpResponse(['500 no recipients']));
}
Expand All @@ -334,6 +335,7 @@ class SmtpClient extends ClientBase {
data,
from,
recipients.map((r) => r.email).toList(),
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
),
);
Expand All @@ -352,7 +354,7 @@ class SmtpClient extends ClientBase {
/// Set [use8BitEncoding] to `true` for sending a UTF-8 encoded message body.
Future<SmtpResponse> sendChunkedMessageText(
String text, MailAddress from, List<MailAddress> recipients,
{bool use8BitEncoding = false}) {
{required bool supportUnicode,bool use8BitEncoding = false}) {
if (recipients.isEmpty) {
throw SmtpException(this, SmtpResponse(['500 no recipients']));
}
Expand All @@ -361,6 +363,7 @@ class SmtpClient extends ClientBase {
text,
from,
recipients.map((r) => r.email).toList(),
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
),
);
Expand Down
2 changes: 1 addition & 1 deletion test/smtp/smtp_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void main() {
final message = MessageBuilder.buildSimpleTextMessage(
from, to, 'Today as well.\r\nOne more time:\r\nHello from enough_mail!',
subject: 'enough_mail hello');
final response = await client.sendChunkedMessage(message);
final response = await client.sendChunkedMessage(message,supportUnicode: false);
expect(response.type, SmtpResponseType.success);
expect(response.code, 250);
});
Expand Down

0 comments on commit d4c1e8c

Please sign in to comment.