ikvk / imap_tools

Work with email by IMAP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get message/delivery-status content

CyrosX opened this issue · comments

Is it possible to get the message/delivery-status content of delivery error mail?
The msg.text output is similar to this:

This is the mail system at host XXX.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to postmaster.

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

                   The mail system

<XXX>: host
    XXX[IP] said: 550 5.7.1 rejected by DMARC policy
    for XXX, servertime=Nov 11 12:48:14,
    server=XXX, client=IP (in reply to end
    of DATA command)

But it is missing the whole report, starting with:
Reporting-MTA: dns; XXX

msg.html / msg.attachment is empty.

Usually that report may coming as simple email. Or maybe not come.
I do not know about standarts for it.
Also see here #155

I've tried this small code snippet: https://stackoverflow.com/a/5300759
and was able to get this kind of mail. Maybe this can be used for the implementation?

I will explore it

Example:

from imap_tools import MailBox, AND
with MailBox('imap.mail.com').login('test@mail.com', 'pwd') as mailbox:
    for msg in mailbox.fetch():
        for att in msg.attachments:
            print(type(att.part))
            print('-- filename', att.filename)
            print('-- payload', att.payload)
            print('-- payload decode', att.payload.decode())

I've tried it out, but these type of mails don't have any attachments. I cannot access the report.

msg.headers contains:

 'content-type': ('multipart/report; report-type=delivery-status;\r\n\tboundary="**ALPHANUMERICAL_COMBINATION**/**MAILHOST**"',)

The report is separated from the text content with:

--**ALPHANUMERICAL_COMBINATION**/**MAILHOST**

The report starts like this:

Reporting-MTA: dns; [**MAILHOST**](**MAILHOST_WEBADDRESS**)
X-Postcow-Queue-ID: **ALPHANUMERICAL_COMBINATION**
X-Postcow-Sender: rfc822; [**MY_MAILADDRESS**](mailto:**MY_MAILADDRESS**)
Arrival-Date: Wed,  1 Nov 2023 05:14:30 +0100 (CET)

Final-Recipient: rfc822; [**ORIGINAL_RECIPIENT_MAILADDRESS**](mailto:**ORIGINAL_RECIPIENT_MAILADDRESS**)
....

Is there a possibility to view the whole raw message from a MailMessage object?

may you give me .eml example ?