prepareForwardMessage method Null safety

MessageBuilder prepareForwardMessage(
  1. MimeMessage originalMessage,
  2. {MailAddress? from,
  3. String forwardHeaderTemplate = MailConventions.defaultForwardHeaderTemplate,
  4. String defaultForwardAbbreviation = MailConventions.defaultForwardAbbreviation,
  5. bool quoteMessage = true,
  6. HeaderEncoding subjectEncoding = HeaderEncoding.Q,
  7. bool forwardAttachments = true}
)

Prepares to forward the given originalMessage. Optionallyspecify the sending user with from. You can also specify a custom forwardHeaderTemplate. The default MailConventions.defaultForwardHeaderTemplate contains the metadata information about the original message including subject, to, cc, date. Specify the defaultForwardAbbreviation if not Fwd should be used at the beginning of the subject to indicate an reply. Set quoteMessage to false when you plan to quote text yourself, e.g. using the enough_mail_html's package quoteToHtml() method. Set forwardAttachments to false when parts with a content-disposition of attachment should not be forwarded.

Implementation

static MessageBuilder prepareForwardMessage(
  MimeMessage originalMessage, {
  MailAddress? from,
  String forwardHeaderTemplate = MailConventions.defaultForwardHeaderTemplate,
  String defaultForwardAbbreviation =
      MailConventions.defaultForwardAbbreviation,
  bool quoteMessage = true,
  HeaderEncoding subjectEncoding = HeaderEncoding.Q,
  bool forwardAttachments = true,
}) {
  String subject;
  var originalSubject = originalMessage.decodeSubject();
  if (originalSubject != null) {
    subject = createForwardSubject(originalSubject,
        defaultForwardAbbreviation: defaultForwardAbbreviation);
  } else {
    subject = defaultForwardAbbreviation;
  }

  final builder = MessageBuilder()
    ..subject = subject
    ..subjectEncoding = subjectEncoding
    ..contentType = originalMessage.getHeaderContentType()
    ..transferEncoding = _getTransferEncoding(originalMessage)
    ..originalMessage = originalMessage;
  if (from != null) {
    builder.from = [from];
  }
  if (quoteMessage) {
    final forwardHeader =
        fillTemplate(forwardHeaderTemplate, originalMessage);
    if (originalMessage.parts?.isNotEmpty ?? false) {
      var processedTextPlainPart = false;
      var processedTextHtmlPart = false;
      for (final part in originalMessage.parts!) {
        if (part.isTextMediaType()) {
          if (!processedTextPlainPart &&
              part.mediaType.sub == MediaSubtype.textPlain) {
            final plainText = part.decodeContentText();
            final quotedPlainText = quotePlainText(forwardHeader, plainText);
            builder.addTextPlain(quotedPlainText);
            processedTextPlainPart = true;
            continue;
          }
          if (!processedTextHtmlPart &&
              part.mediaType.sub == MediaSubtype.textHtml) {
            final decodedHtml = part.decodeContentText() ?? '';
            final quotedHtml = '<br/><blockquote>' +
                forwardHeader.split('\r\n').join('<br/>\r\n') +
                '<br/>\r\n' +
                decodedHtml +
                '</blockquote>';
            builder.addTextHtml(quotedHtml);
            processedTextHtmlPart = true;
            continue;
          }
        }
        if (forwardAttachments ||
            part.getHeaderContentDisposition()?.disposition !=
                ContentDisposition.attachment) {
          builder.addPart(mimePart: part);
        }
      }
    } else {
      // no parts, this is most likely a plain text message:
      if (originalMessage.isTextPlainMessage()) {
        final plainText = originalMessage.decodeContentText();
        final quotedPlainText = quotePlainText(forwardHeader, plainText);
        builder.text = quotedPlainText;
      } else {
        //TODO check if there is anything else to quote
      }
    }
  } else if (forwardAttachments) {
    // do not quote message but forward attachments
    final infos = originalMessage.findContentInfo();
    for (final info in infos) {
      final part = originalMessage.getPart(info.fetchId);
      if (part != null) {
        builder.addPart(mimePart: part);
      }
    }
  }

  return builder;
}