writeData method

Future<void> writeData(
  1. SecureSocket socket,
  2. Email email
)

Implementation

Future<void> writeData(SecureSocket socket, Email email) async {
  await SocketManager.write(
    socket,
    data: "MAIL FROM:<${email.sender.address}>",
  );
  await _notify(socket);
  await SocketManager.write(socket, data: "DATA");

  final headers = [
    "From: ${email.sender}",
    "To: ${_formatRecipients(RecipientType.to)}",
    if (email.cc.isNotEmpty) "Cc: ${_formatRecipients(RecipientType.cc)}",
    if (email.replyTo != null) "Reply-To: ${email.replyTo}",
    "Subject: ${email.subject}",
    "Content-Type: text/plain; charset=\"utf-8\"",
  ].join("\r\n");

  final body = "$headers\r\n\r\n${email.message}\r\n.";

  await SocketManager.write(socket, data: body);
  await SocketManager.write(socket, data: "QUIT");
}