Skip to content

Commit

Permalink
support unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed1226 committed Mar 6, 2023
1 parent 1a156cd commit 9268ccb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 47 deletions.
29 changes: 14 additions & 15 deletions lib/src/mail/mail_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,12 @@ class MailClient {
final use8Bit = builderEncoding == TransferEncoding.eightBit;

final futures = <Future>[
_sendMessageViaOutgoing(message, from, use8Bit, recipients,),
_sendMessageViaOutgoing(
message,
from,
use8Bit,
recipients,
),
];
if (appendToSent && _incomingMailClient.supportsAppendingMessages) {
sentMailbox ??= getMailbox(MailboxFlag.sent);
Expand Down Expand Up @@ -835,39 +840,35 @@ class MailClient {
MimeMessage message, {
MailAddress? from,
bool appendToSent = true,
bool isUnicode = false,
bool supportUnicode = false,
Mailbox? sentMailbox,
bool use8BitEncoding = false,
List<MailAddress>? recipients,
}) {
print("dola d");
final futures = <Future>[
_sendMessageViaOutgoing(message, from, use8BitEncoding, recipients,
isUnicode: isUnicode),
supportUnicode: supportUnicode),
];
if (appendToSent && _incomingMailClient.supportsAppendingMessages) {
print("dola dd");
sentMailbox ??= getMailbox(MailboxFlag.sent);
if (sentMailbox == null) {
_incomingMailClient
.log('Error: unable to append sent message: no no mailbox with '
'flag sent found in $mailboxes');
} else {
print("dola ddd");
futures.add(
appendMessage(message, sentMailbox, flags: [MessageFlags.seen]));
}
}
print("dola dddd");

return Future.wait(futures);
}

Future _sendMessageViaOutgoing(MimeMessage message, MailAddress? from,
bool use8BitEncoding, List<MailAddress>? recipients,
{bool isUnicode = false}) async {
print("dola _sendMessageViaOutgoing isUnicode $isUnicode");
{bool supportUnicode = false}) async {
await _outgoingMailClient.sendMessage(message,
isUnicode: isUnicode,
supportUnicode: supportUnicode,
from: from,
use8BitEncoding: use8BitEncoding,
recipients: recipients);
Expand Down Expand Up @@ -3093,7 +3094,7 @@ abstract class _OutgoingMailClient {
Future<bool> supports8BitEncoding();

Future<void> sendMessage(MimeMessage message,
{required bool isUnicode,
{required bool supportUnicode,
MailAddress? from,
bool use8BitEncoding = false,
List<MailAddress>? recipients});
Expand Down Expand Up @@ -3162,24 +3163,22 @@ class _OutgoingSmtpClient extends _OutgoingMailClient {
@override
Future<void> sendMessage(
MimeMessage message, {
required bool isUnicode,
required bool supportUnicode,
MailAddress? from,
bool use8BitEncoding = false,
List<MailAddress>? recipients,
}) async {
await _connectOutgoingIfRequired();
try {
if (_smtpClient.serverInfo.supportsChunking) {
print("dola supportsChunking");
await _smtpClient.sendChunkedMessage(
message,
from: from,
isUnicode: isUnicode,
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
recipients: recipients,
);
} else {
print("dola not supportsChunking");
await _smtpClient.sendMessage(
message,
from: from,
Expand Down
29 changes: 13 additions & 16 deletions lib/src/private/smtp/commands/smtp_send_bdat_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class _SmtpSendBdatCommand extends SmtpCommand {
this.fromEmail,
this.recipientEmails, {
required this.use8BitEncoding,
required this.isUnicode,
required this.supportUnicode,
}) : super('MAIL FROM') {
final binaryData = _codec.encode(getData());
_chunks = chunkData(binaryData);
Expand All @@ -25,15 +25,14 @@ class _SmtpSendBdatCommand extends SmtpCommand {
final String? fromEmail;
final List<String> recipientEmails;
final bool use8BitEncoding;
final bool isUnicode;
final bool supportUnicode;
_BdatSequence _currentStep = _BdatSequence.mailFrom;
int _recipientIndex = 0;
late List<Uint8List> _chunks;
int _chunkIndex = 0;
static const Utf8Codec _codec = Utf8Codec(allowMalformed: true);

static List<Uint8List> chunkData(List<int> binaryData) {
print("dola chunkData");
const chunkSize = 512 * 1024;
final result = <Uint8List>[];
var startIndex = 0;
Expand All @@ -57,14 +56,14 @@ class _SmtpSendBdatCommand extends SmtpCommand {

@override
String get command {
if (isUnicode) {
print('dola dola isUnicode $isUnicode');
return 'MAIL FROM:<$fromEmail> BODY=SMTPUTF8';
if (supportUnicode) {
print('supportUnicode $supportUnicode');
return 'MAIL FROM:<$fromEmail> SMTPUTF8';
}
if (use8BitEncoding) {
return 'MAIL FROM:<$fromEmail> BODY=8BITMIME';
}
return 'MAIL FROM:<$fromEmail> SMTPUTF8';
return 'MAIL FROM:<$fromEmail>';
}

@override
Expand Down Expand Up @@ -123,18 +122,16 @@ class SmtpSendBdatMailCommand extends _SmtpSendBdatCommand {
MailAddress? from,
List<String> recipientEmails, {
required bool use8BitEncoding,
required bool isUnicode,
required bool supportUnicode,
}) : super(
() => message
.renderMessage()
.replaceAll(RegExp('^Bcc:.*\r\n', multiLine: true), ''),
from?.email ?? message.fromEmail,
recipientEmails,
use8BitEncoding: use8BitEncoding,
isUnicode: isUnicode,
) {
print("dola SmtpSendBdatMailCommand");
}
supportUnicode: supportUnicode,
);

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

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

/// The message text data
final String data;
Expand Down
5 changes: 0 additions & 5 deletions lib/src/private/smtp/commands/smtp_sendmail_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ class _SmtpSendCommand extends SmtpCommand {
@override
String get command {
if (use8BitEncoding) {
print('dola use8BitEncoding:1 SmtpCommand');
log('dola use8BitEncoding:1 SmtpCommand');
//logApp('use8BitEncoding');
return 'MAIL FROM:<$fromEmail> BODY=8BITMIME';
}
print(' dola Not use8BitEncoding1 SmtpCommand');
log(' dola Not use8BitEncoding1 SmtpCommand');
return 'MAIL FROM:<$fromEmail>';
}

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

/// Sends the specified message [data] [from] to the [recipients]
Expand All @@ -327,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,
{ required bool isUnicode,bool use8BitEncoding = false}) {
{ required bool supportUnicode,bool use8BitEncoding = false}) {
if (recipients.isEmpty) {
throw SmtpException(this, SmtpResponse(['500 no recipients']));
}
Expand All @@ -336,7 +335,7 @@ class SmtpClient extends ClientBase {
data,
from,
recipients.map((r) => r.email).toList(),
isUnicode: isUnicode,
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
),
);
Expand All @@ -355,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,
{required bool isUnicode,bool use8BitEncoding = false}) {
{required bool supportUnicode,bool use8BitEncoding = false}) {
if (recipients.isEmpty) {
throw SmtpException(this, SmtpResponse(['500 no recipients']));
}
Expand All @@ -364,7 +363,7 @@ class SmtpClient extends ClientBase {
text,
from,
recipients.map((r) => r.email).toList(),
isUnicode: isUnicode,
supportUnicode: supportUnicode,
use8BitEncoding: use8BitEncoding,
),
);
Expand Down Expand Up @@ -403,7 +402,6 @@ class SmtpClient extends ClientBase {

/// Sends the command to the server
Future<SmtpResponse> sendCommand(SmtpCommand command) {
print("dola sendCommand");
_currentCommand = command;
writeText(command.command, command);
return command.completer.future;
Expand All @@ -427,7 +425,6 @@ class SmtpClient extends ClientBase {
writeData(next!.data!);
} else if (cmd.isCommandDone(response)) {
if (response.isFailedStatus) {
log('dola Error isFailedStatus proceeding to nextCommand');
cmd.completer.completeError(SmtpException(this, response));
} else {
cmd.completer.complete(response);
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,isUnicode: false);
final response = await client.sendChunkedMessage(message,supportUnicode: false);
expect(response.type, SmtpResponseType.success);
expect(response.code, 250);
});
Expand Down

0 comments on commit 9268ccb

Please sign in to comment.