prepareReplyToMessage static method

MessageBuilder prepareReplyToMessage(
  1. MimeMessage originalMessage,
  2. MailAddress from, {
  3. bool replyAll = true,
  4. bool quoteOriginalText = false,
  5. bool preferPlainText = false,
  6. String replyHeaderTemplate = MailConventions.defaultReplyHeaderTemplate,
  7. String defaultReplyAbbreviation = MailConventions.defaultReplyAbbreviation,
  8. bool replyToSimplifyReferences = false,
  9. List<MailAddress>? aliases,
  10. bool handlePlusAliases = false,
  11. HeaderEncoding subjectEncoding = HeaderEncoding.Q,
})

Prepares to create a reply to the given originalMessage to be send by the user specifed in from.

Set replyAll to false in case the reply should only be done to the sender of the message and not to other recipients Set quoteOriginalText to true in case the original plain and html texts should be added to the generated message. Set preferPlainText and quoteOriginalText to true in case only plain text should be quoted. You can also specify a custom replyHeaderTemplate, which is only used when quoteOriginalText has been set to true. The default replyHeaderTemplate is 'On

Implementation

static MessageBuilder prepareReplyToMessage(
    MimeMessage originalMessage, MailAddress from,
    {bool replyAll = true,
    bool quoteOriginalText = false,
    bool preferPlainText = false,
    String replyHeaderTemplate = MailConventions.defaultReplyHeaderTemplate,
    String defaultReplyAbbreviation =
        MailConventions.defaultReplyAbbreviation,
    bool replyToSimplifyReferences = false,
    List<MailAddress>? aliases,
    bool handlePlusAliases = false,
    HeaderEncoding subjectEncoding = HeaderEncoding.Q}) {
  String? subject;
  final originalSubject = originalMessage.decodeSubject();
  if (originalSubject != null) {
    subject = createReplySubject(originalSubject,
        defaultReplyAbbreviation: defaultReplyAbbreviation);
  }
  var to = originalMessage.to ?? [];
  var cc = originalMessage.cc;
  final replyTo = originalMessage.decodeSender();
  List<MailAddress> senders;
  if (aliases?.isNotEmpty ?? false) {
    senders = [from, ...aliases!];
  } else {
    senders = [from];
  }
  var newSender = MailAddress.getMatch(senders, replyTo,
      handlePlusAliases: handlePlusAliases,
      removeMatch: true,
      useMatchPersonalName: true);
  newSender ??= MailAddress.getMatch(senders, to,
      handlePlusAliases: handlePlusAliases, removeMatch: true);
  newSender ??= MailAddress.getMatch(senders, cc,
      handlePlusAliases: handlePlusAliases, removeMatch: true);
  if (newSender != null) {
    from = newSender;
  }
  if (replyAll) {
    to.insertAll(0, replyTo);
  } else {
    if (replyTo.isNotEmpty) {
      to = [...replyTo];
    }
    cc = null;
  }
  final builder = MessageBuilder()
    ..subject = subject
    ..subjectEncoding = subjectEncoding
    ..originalMessage = originalMessage
    ..from = [from]
    ..to = to
    ..cc = cc
    ..replyToSimplifyReferences = replyToSimplifyReferences;

  if (quoteOriginalText) {
    final replyHeader = fillTemplate(replyHeaderTemplate, originalMessage);

    final plainText = originalMessage.decodeTextPlainPart();
    final quotedPlainText = quotePlainText(replyHeader, plainText);
    final decodedHtml = originalMessage.decodeTextHtmlPart();
    if (preferPlainText || decodedHtml == null) {
      builder.text = quotedPlainText;
    } else {
      builder.setContentType(MediaSubtype.multipartAlternative.mediaType);
      builder.addTextPlain(quotedPlainText);
      final quotedHtml = '<blockquote><br/>' +
          replyHeader +
          '<br/>' +
          decodedHtml +
          '</blockquote>';
      builder.addTextHtml(quotedHtml);
    }
  }
  return builder;
}