Sample code for 30+ languages & platforms
PHP ActiveX Requires Chilkat v11.0.0+

Create a delivery failure report

See more Email Object Examples

A delivery agent can report a failure for an actual envelope recipient. Supply its observed SMTP outcome and enhanced status code, and return only the original headers when that is sufficient. This example saves a report for inspection. DSN format.

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?php

$success = 0;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

$email = new COM("Chilkat.Email");
$success = $email->LoadEml('message.eml');
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

$fields = new COM("Chilkat.Xml");
$fields->Tag = 'DeliveryStatusFields';
$fields->NewChild2('Final-Recipient','rfc822; alex@example.net');
$fields->NewChild2('Action','failed');
$fields->NewChild2('Status','5.1.1');
$fields->NewChild2('Diagnostic-Code','smtp; 550 5.1.1 Mailbox does not exist');
$report = new COM("Chilkat.Email");
$success = $email->ToDsn('The destination mailbox does not exist.',$fields->getXml(),1,$report);
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

$report->From = 'Mail delivery agent <postmaster@example.net>';
//  RFC 3464 puts the reporting MTA in its own block, ahead of the recipient fields.
$mimeBytes = new COM("Chilkat.BinData");
$success = $report->GetMimeBd($mimeBytes);
if ($success != 1) {
    print $report->LastErrorText . "\n";
    exit;
}

$mime = new COM("Chilkat.Mime");
$success = $mime->LoadMimeBd($mimeBytes);
if ($success != 1) {
    print $mime->LastErrorText . "\n";
    exit;
}

$statusPart = new COM("Chilkat.Mime");
$success = $mime->PartAt(1,$statusPart);
if ($success != 1) {
    print $mime->LastErrorText . "\n";
    exit;
}

$status = new COM("Chilkat.StringBuilder");
$status->Append('Reporting-MTA: dns; mx.example.net\r\n\r\n');
$status->Append($statusPart->getBodyDecoded());
$statusPart->SetBody($status->getAsString());
$success = $mime->SaveMime('delivery-status.eml');
if ($success != 1) {
    print $mime->LastErrorText . "\n";
    exit;
}

print 'Saved delivery-status.eml.' . "\n";

?>