Skip to content

Commit

Permalink
Fix #27 / message sending via SMTP
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Virkus committed Apr 15, 2020
1 parent 620e316 commit f867316
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.10

- Fix for message sending via SMTP (issue #27)

## 0.0.9

- Introducing MessageBuilder for easy mime message creation
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Add this dependency your pubspec.yaml file:

```
dependencies:
enough_mail: ^0.0.8
enough_mail: ^0.0.10
```
The latest version or `enough_mail` is [![enough_mail version](https://img.shields.io/pub/v/enough_mail.svg)](https://pub.dartlang.org/packages/enough_mail).

Expand Down
18 changes: 17 additions & 1 deletion lib/mime_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ class MimeMessage extends MimePart {
set bcc(List<MailAddress> list) => _bcc = list;

Body body;
List<String> recipients = <String>[];

// Retrieves the mail addresses of all message recipients
List<String> get recipientAddresses => _collectRecipientsAddresses();

/// Decodes the subject of this message
String decodeSubject() {
Expand Down Expand Up @@ -521,6 +523,20 @@ class MimeMessage extends MimePart {
}
return buffer.toString();
}

List<String> _collectRecipientsAddresses() {
var recipients = <String>[];
if (to != null) {
recipients.addAll(to.map((a) => a.email));
}
if (cc != null) {
recipients.addAll(cc.map((a) => a.email));
}
if (bcc != null) {
recipients.addAll(bcc.map((a) => a.email));
}
return recipients;
}
}

/// Encapsulates a MIME header
Expand Down
15 changes: 9 additions & 6 deletions lib/src/smtp/commands/smtp_sendmail_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class SmtpSendMailCommand extends SmtpCommand {
SmtpSendCommandSequence _currentStep = SmtpSendCommandSequence.mailFrom;
int _recipientIndex = 0;

List<String> _recipientAddresses;

SmtpSendMailCommand(this._message, this._use8BitEncoding)
: super('MAIL FROM');

Expand All @@ -27,18 +29,19 @@ class SmtpSendMailCommand extends SmtpCommand {
var step = _currentStep;
switch (step) {
case SmtpSendCommandSequence.mailFrom:
if (_message.recipients.isEmpty) {
return null;
}
_currentStep = SmtpSendCommandSequence.rcptTo;
_recipientIndex++;
return _getRecipientToCommand(_message.recipients[0]);
_recipientAddresses = _message.recipientAddresses;
if (_recipientAddresses == null || _recipientAddresses.isEmpty) {
throw StateError('No recipients defined in message.');
}
return _getRecipientToCommand(_recipientAddresses[0]);
break;
case SmtpSendCommandSequence.rcptTo:
var index = _recipientIndex;
if (index < _message.recipients.length) {
if (index < _recipientAddresses.length) {
_recipientIndex++;
return _getRecipientToCommand(_message.recipients[index]);
return _getRecipientToCommand(_recipientAddresses[index]);
} else if (response.type == SmtpResponseType.success) {
_currentStep = SmtpSendCommandSequence.data;
return 'DATA';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: enough_mail
description: IMAP and SMTP clients in pure Dart. Strives to be compliant with IMAP4 rev1, IMAP IDLE, IMAP METADATA Extension and SMTP.
version: 0.0.9
version: 0.0.10
homepage: https://github.com/Enough-Software/enough_mail

environment:
Expand Down
21 changes: 6 additions & 15 deletions test/smtp/smtp_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,12 @@ void main() {
});

test('SmtpClient sendMessage', () async {
var message = MimeMessage();
message.from = <MailAddress>[
MailAddress.fromEnvelope(
'Rita Levi-Montalcini', null, 'Rita.Levi-Montalcini', 'domain.com')
];
message.recipients.add('[email protected]');
message.headerRaw = 'From: [email protected]\r\n'
'To: [email protected]\r\n'
'Subject: Enough MailKit Hello 2\r\n'
'Message-ID: chat\$232.123o29892232.domain.com\r\n'
'Date: Mon, 13 Jan 2020 15:47:37 +0100\r\n'
'Conten-Type: text/plain; charset=utf-8; format=flowed\r\n'
'Content-Transfer-Encoding: 8bit\r\n';
message.bodyRaw =
'Today as well.\r\nOne more time:\r\nHello from Enough MailKit!';
var from =
MailAddress('Rita Levi-Montalcini', '[email protected]');
var to = [MailAddress('Rosalind Franklin', '[email protected]')];
var message = MessageBuilder.buildSimpleTextMessage(from, to,
'Today as well.\r\nOne more time:\r\nHello from Enough MailKit!',
subject: 'enough_mail hello');
var response = await client.sendMessage(message);
expect(response.type, SmtpResponseType.success);
expect(response.code, 250);
Expand Down

0 comments on commit f867316

Please sign in to comment.