MessageBuilder.prepareReplyToMessage constructor

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 specified 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

Set replyToSimplifyReferences to true if the References field should not contain the references of all messages in this thread.

Specify the defaultReplyAbbreviation if not 'Re' should be used at the beginning of the subject to indicate an reply.

Specify the known aliases of the recipient, so that alias addresses are not added as recipients and a detected alias is used instead of the from address in that case.

Set handlePlusAliases to true in case plus aliases like email+alias@domain.com should be detected and used.

Implementation

factory 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;
  senders =
      aliases != null && aliases.isNotEmpty ? [from, ...aliases] : [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 (replyAll) {
    to.insertAll(0, replyTo);
  } else {
    if (replyTo.isNotEmpty) {
      to = [...replyTo];
    }
    cc = null;
  }
  final builder = MessageBuilder()
    ..subject = subject
    ..subjectEncoding = subjectEncoding
    ..originalMessage = originalMessage
    ..from = [newSender ?? 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)
        ..addTextPlain(quotedPlainText);
      final quotedHtml =
          '<blockquote><br/>$replyHeader<br/>$decodedHtml</blockquote>';
      builder.addTextHtml(quotedHtml);
    }
  }

  return builder;
}