buildReadReceipt method Null safety

MimeMessage buildReadReceipt(
  1. MimeMessage originalMessage,
  2. MailAddress finalRecipient,
  3. {String reportingUa = 'enough_mail',
  4. bool isAutomaticReport = false,
  5. String subject = 'read receipt',
  6. String textTemplate = MailConventions.defaultReadReceiptTemplate}
)

Builds a disposition notification report for the given originalMessage that has been received by the finalRecipient.

Optionally specify the reporting user agent, ie your apps name with the reportingUa parameter, e.g. 'My Mail App 1.0'. Optionally specify that the report is generated automatically by setting isAutomaticReport to true - this defaults to false. Optionally specify a subject, this defaults to 'read receipt'. Optionally specify your own textTemplate in which you can use the fields <subject>, <date>, <recipient> and <sender>. This defaults to MailConventions.defaultReadReceiptTemplate. Throws a StateError when the originalMessage has no valid Disposition-Notification-To or Return-Receipt-To header. Use requestReadReceipt to request a read receipt when building a message.

Implementation

static MimeMessage buildReadReceipt(
  MimeMessage originalMessage,
  MailAddress finalRecipient, {
  String reportingUa = 'enough_mail',
  bool isAutomaticReport = false,
  String subject = 'read receipt',
  String textTemplate = MailConventions.defaultReadReceiptTemplate,
}) {
  final builder = MessageBuilder();
  var recipient = originalMessage.decodeHeaderMailAddressValue(
      MailConventions.headerDispositionNotificationTo);
  if (recipient == null || recipient.isEmpty) {
    recipient =
        originalMessage.decodeHeaderMailAddressValue('Return-Receipt-To');
    if (recipient == null || recipient.isEmpty) {
      throw StateError(
          'Invalid header ${MailConventions.headerDispositionNotificationTo} in message: ${originalMessage.getHeaderValue(MailConventions.headerDispositionNotificationTo)}');
    }
  }
  builder.subject = subject;
  builder.to = recipient;
  builder.setContentType(MediaSubtype.multipartReport.mediaType);
  final parameters = <String, String>{
    'recipient': finalRecipient.toString(),
    'sender': originalMessage.fromEmail ?? '<unknown>',
  };
  builder.setHeader(MailConventions.headerMimeVersion, '1.0');
  final plainText =
      fillTemplate(textTemplate, originalMessage, parameters: parameters);
  builder.addTextPlain(plainText);
  final mdnPart = builder.addPart(
      mediaSubtype: MediaSubtype.messageDispositionNotification);
  mdnPart.transferEncoding = TransferEncoding.sevenBit;
  mdnPart.contentDisposition = ContentDispositionHeader.inline();
  final buffer = StringBuffer()
    ..write('Reporting-UA: ')
    ..write(reportingUa)
    ..write('\r\n');
  if (originalMessage.findRecipient(finalRecipient) != null) {
    buffer
      ..write('Original-Recipient: rfc822;')
      ..write(finalRecipient.email)
      ..write('\r\n');
  }
  buffer
    ..write('Final-Recipient: rfc822;')
    ..write(finalRecipient.email)
    ..write('\r\n');
  buffer
    ..write('Original-Message-ID: ')
    ..write(originalMessage.getHeaderValue(MailConventions.headerMessageId))
    ..write('\r\n');
  if (isAutomaticReport) {
    buffer.write(
        'Disposition: automatic-action/MDN-sent-automatically; displayed\r\n');
  } else {
    buffer
        .write('Disposition: manual-action/MDN-sent-manually; displayed\r\n');
  }
  mdnPart.text = buffer.toString();
  builder.from = [finalRecipient];
  return builder.buildMimeMessage();
}