Python Requires Chilkat v11.0.0+
Python
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 Python Downloads
import sys
import chilkat2
success = False
# Assumes Chilkat is unlocked. See Global Unlock Sample.
email = chilkat2.Email()
success = email.LoadEml("message.eml")
if (success != True):
print(email.LastErrorText)
sys.exit()
fields = chilkat2.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 = chilkat2.Email()
success = email.ToDsn("The destination mailbox does not exist.",fields.GetXml(),True,report)
if (success != True):
print(email.LastErrorText)
sys.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 = chilkat2.BinData()
success = report.GetMimeBd(mimeBytes)
if (success != True):
print(report.LastErrorText)
sys.exit()
mime = chilkat2.Mime()
success = mime.LoadMimeBd(mimeBytes)
if (success != True):
print(mime.LastErrorText)
sys.exit()
statusPart = chilkat2.Mime()
success = mime.PartAt(1,statusPart)
if (success != True):
print(mime.LastErrorText)
sys.exit()
status = chilkat2.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 != True):
print(mime.LastErrorText)
sys.exit()
print("Saved delivery-status.eml.")