From f8673161dddf1e9bf602f081484ff63d2fbbd88b Mon Sep 17 00:00:00 2001 From: Robert Virkus Date: Wed, 15 Apr 2020 14:55:37 +0200 Subject: [PATCH] Fix #27 / message sending via SMTP --- CHANGELOG.md | 4 ++++ README.md | 2 +- lib/mime_message.dart | 18 +++++++++++++++- .../smtp/commands/smtp_sendmail_command.dart | 15 +++++++------ pubspec.yaml | 2 +- test/smtp/smtp_client_test.dart | 21 ++++++------------- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d443613..e204d297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 19ef9d91..24b03c1c 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/lib/mime_message.dart b/lib/mime_message.dart index 6acff8f4..a2af26f9 100644 --- a/lib/mime_message.dart +++ b/lib/mime_message.dart @@ -390,7 +390,9 @@ class MimeMessage extends MimePart { set bcc(List list) => _bcc = list; Body body; - List recipients = []; + + // Retrieves the mail addresses of all message recipients + List get recipientAddresses => _collectRecipientsAddresses(); /// Decodes the subject of this message String decodeSubject() { @@ -521,6 +523,20 @@ class MimeMessage extends MimePart { } return buffer.toString(); } + + List _collectRecipientsAddresses() { + var recipients = []; + 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 diff --git a/lib/src/smtp/commands/smtp_sendmail_command.dart b/lib/src/smtp/commands/smtp_sendmail_command.dart index 9cecc9c0..2888b84e 100644 --- a/lib/src/smtp/commands/smtp_sendmail_command.dart +++ b/lib/src/smtp/commands/smtp_sendmail_command.dart @@ -11,6 +11,8 @@ class SmtpSendMailCommand extends SmtpCommand { SmtpSendCommandSequence _currentStep = SmtpSendCommandSequence.mailFrom; int _recipientIndex = 0; + List _recipientAddresses; + SmtpSendMailCommand(this._message, this._use8BitEncoding) : super('MAIL FROM'); @@ -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'; diff --git a/pubspec.yaml b/pubspec.yaml index edc505c1..db223593 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: diff --git a/test/smtp/smtp_client_test.dart b/test/smtp/smtp_client_test.dart index 81035af2..93661faa 100644 --- a/test/smtp/smtp_client_test.dart +++ b/test/smtp/smtp_client_test.dart @@ -100,21 +100,12 @@ void main() { }); test('SmtpClient sendMessage', () async { - var message = MimeMessage(); - message.from = [ - MailAddress.fromEnvelope( - 'Rita Levi-Montalcini', null, 'Rita.Levi-Montalcini', 'domain.com') - ]; - message.recipients.add('Rosalind.Franklin@domain.com'); - message.headerRaw = 'From: Rita.Levi-Montalcini@domain.com\r\n' - 'To: Rosalind.Franklin@domain.com\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', 'Rita.Levi-Montalcini@domain.com'); + var to = [MailAddress('Rosalind Franklin', 'Rosalind.Franklin@domain.com')]; + 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);