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.


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(
  if (recipient == null || recipient.isEmpty) {
    recipient =
    if (recipient == null || recipient.isEmpty) {
      throw StateError(
          'Invalid header ${MailConventions.headerDispositionNotificationTo} in message: ${originalMessage.getHeaderValue(MailConventions.headerDispositionNotificationTo)}');
  builder.subject = subject; = recipient;
  final parameters = <String, String>{
    'recipient': finalRecipient.toString(),
    'sender': originalMessage.fromEmail ?? '<unknown>',
  builder.setHeader(MailConventions.headerMimeVersion, '1.0');
  final plainText =
      fillTemplate(textTemplate, originalMessage, parameters: parameters);
  final mdnPart = builder.addPart(
      mediaSubtype: MediaSubtype.messageDispositionNotification);
  mdnPart.transferEncoding = TransferEncoding.sevenBit;
  mdnPart.contentDisposition = ContentDispositionHeader.inline();
  final buffer = StringBuffer()
    ..write('Reporting-UA: ')
  if (originalMessage.findRecipient(finalRecipient) != null) {
      ..write('Original-Recipient: rfc822;')
    ..write('Final-Recipient: rfc822;')
    ..write('Original-Message-ID: ')
  if (isAutomaticReport) {
        'Disposition: automatic-action/MDN-sent-automatically; displayed\r\n');
  } else {
        .write('Disposition: manual-action/MDN-sent-manually; displayed\r\n');
  mdnPart.text = buffer.toString();
  builder.from = [finalRecipient];
  return builder.buildMimeMessage();