douluogu8713 2019-03-17 13:04
浏览 160
已采纳

如何从原始电子邮件中提取信息

What I have done so far

I use POP3 package to read all my emails from my mailbox.

I received a raw email from the POP3 function which shown at the example below. (I omitted some information)

Issue

I'm facing the issue to extract the information from it.

I used the mail to extract the information, but unfortunately, this package cannot extract information from the raw email.

Seeking for help

Is there any method or package there which can help me to extract the information from the raw email?


Methods I had tried

// Retrieve all the email from your mailbox
msgs, _, error := connection.ListAll()

// Convert a chunk of integer to raw email
data, _ := connection.Retr(msgs[0])

// Extract Email Address
to, _ := mail.ParseAddress(data)

Error I am facing

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x5819ed]

Raw Email Example

From: 
To: 
Subject: 
Thread-Topic: 
Thread-Index: AdPgWzcFT3FjbSbUT1ycoU2ioB1bKAAAAHuA
X-MS-Exchange-MessageSentRepresentingType: 1
Date: 
Message-ID: 
Accept-Language: en-SG, en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource: 
X-MS-Has-Attach:
X-MS-Exchange-Organization-Network-Message-Id:
    8c1e141b-c3ba-4471-0526-08d5b3b59967
X-MS-TNEF-Correlator:
Content-Type: multipart/alternative;
    boundary="_002_b1a01aa36e1c4b3d9969a7bbb856bd3b"
MIME-Version: 1.0

--_002_b1a01aa36e1c4b3d9969a7bbb856bd3b
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: base64

PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwiIHhtbG5zOm89InVy
bjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9mZmljZSIgeG1sbnM6dz0idXJuOnNjaGVt
YXMtbWljcm9zb2Z0LWNvbTpvZmZpY2U6d29yZCIgeG1sbnM6bT0iaHR0cDovL3NjaGVtYXMubWlj
cm9zb2Z0LmNvbS9vZmZpY2U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NQOk9QQVFVRQ0KU1RBVFVTOkNPTkZJ
Uk1FRA0KU0VRVUVOQ0U6Mw0KTE9DQVRJT047TEFOR1VBR0U9ZW4tVVM6Q29ubmVjdGlvbg0KWC1N
SUNST1NPRlQtQ0RPLUFQUFQtU0VRVUVOQ0U6Mw0KWC1NSUNST1NPRlQtQ0RPLU9XTkVSQVBQVElE
OjEzMDY0MTMwMjYNClgtTUlDUk9TT0ZULUNETy1CVVNZU1RBVFVTOlRFTlRBVElWRQ0KWC1NSUNS
T1NPRlQtQ0RPLUlOVEVOREVEU1RBVFVTOkJVU1kNClgtTUlDUk9TT0ZULUNETy1BTExEQVlFVkVO
VDpGQUxTRQ0KWC1NSUNST1NPRlQtQ0RPLUlNUE9SVEFOQ0U6MQ0KWC1NSUNST1NPRlQtQ0RPLUlO
U1RUWVBFOjANClgtTUlDUk9TT0ZULURJU0FMTE9XLUNPVU5URVI6RkFMU0UNCkJFR0lOOlZBTEFS
TQ0KREVTQ1JJUFRJT046UkVNSU5ERVINClRSSUdHRVI7UkVMQVRFRD1TVEFSVDotUFQxNU0NCkFD
VElPTjpESVNQTEFZDQpFTkQ6VkFMQVJNDQpFTkQ6VkVWRU5UDQpFTkQ6VkNBTEVOREFSDQo=

--_002_b1a01aa36e1c4b3d9969a7bbb856bd3b
  • 写回答

1条回答 默认 最新

  • dongsibao8977 2019-03-17 15:41
    关注

    The theory

    E-mail messages are formatted using basically one of the following two formats.

    The oldest one is defined by RFC 5322 (originally it was RFC 822 but it has been updated since then).

    This format does not support messages using non-ASCII character encodings and neither does it support what general public calls "attachments".

    To rectify the situation, the set of standards commonly known as MIME was invented. The standards in this set define:

    • Ways to use non-ASCII character encodings.
    • Ways to compose multi-part messages.

    The two most interesting MIME standards are RFC 2045 and RFC 2046.

    The standards incorporated by MIME were specifically devised in a way to make MIME-encoded stuff still compatible with RFC 822—this, among other things, allowed not to change MTAs to support the new message format.

    The practice

    Libraries implemented in various programming lanugages to deal with various bits defined by MIME usually follow the trend of the MIME itself and are able to transparently handle "plain" RFC 822-formatted mails and MIME-formatted.

    To handle MIME-formatted messages, Go offers in its standard library the three packages:

    and another one, net/textproto, to deal with MIME-style headers (also used in HTTP, IMAP/POP3 etc).

    Together, these packages cover most (or all) of what you need to read and write MIME-formatted e-mail messages.

    The basic approach to parsing

    The basic approach to parsing an e-mail message is as follows:

    1. Create an instance of bufio.Reader from an io.Reader supplying the data of the e-mail message to be parsed.

    2. Create an instance of net/textproto.Reader from the bufio.Reader made on the previous step.

    3. Use its ReadMIMEHeader() method to read and parse the message's header block.

    4. Check to see whether it contains the MIME-Version field mandated by RFC 2045 to indicate a MIME-formatted content.

      1. If it contains one, verify its value is literally "1.0". If it isn't, this is some MIME format from the future you won't be able to cope with. Barf accordingly; otherwise proceed to the next step.
      2. If there is no such field, the message is a plain old e-mail consisting of a single part. Consider its whole contents as if it were a single MIME-message part (this is oversimplified but would mostly work).
    5. If you have verified you're dealing with a MIME 1.0 message, then

      1. Read the Content-Type header field then use mime.ParseMediaType() to parse it.
        • If the resulting mediatype value will start with the "multipart/" prefix, literally, then be prepared to deal with multiple parts, which require recursive processing as each part is formatted almost like the top-level message—that is, contains the header and the body (see below).
        • Otherwise the content type will indicate some kind of "direct" payload (such as "text/plain" or "text/html" or whatever). In this case, a special "parameter" of such media type (see below) indicates the character encoding used for the part's content, if it's textual. (Also note that it may be "message/rfc822" which actually indicates the payload is another e-mail message which might need to be parsed according to the same rules as the encloding one.)

    Dealing with a "leaf" part or non-multi-part message payloads

    An importand header field to read next is Content-Transfer-Encoding which defines how the part is physically encoded to be transferred over the wire.

    Most of the time it will be "base64" but it also may be "quoted-printable".

    Dealing with multi-part messages

    First, be prepared to properly deal with the different aspects of what "multi part" is: the parts might be either alternatives to each other—that is, the program which is to render a message to the user might pick any of them—whatever suits the user's preference best, or ask them or something else,—or they may be entities sort-of equal to each other. The former scheme is indicated by the "multipart/alternative" media type and is typically used by MUAs which allow the user to compose a mail message using markup and then encode the result so that it contains two alternative parts—one marked as "text/html" and another one marked as "text/plain" and containing the source content stripped of that markup nonsense. The latter is indicated by "multipart/mixed" and is typically used for attachments: with this scheme the first part is typically (but it's not required to be that) is the message's text in whatever format and the rest of the parts are attachments.

    To be able to pick out the individual parts from the message's encoding, the "multipart/whatever" media type most of the time contains a so-called "parameter" named "boundary" and containing a unique string which is used to delimit the parts. The mime.ParseMediaType() function returns the media type's parameters in the form of a map as its second result value.

    After extracting that "boundary" media type's parameter, you can use it to create an instance of mime/multipart.Reader from the bufio.Reader instance made on the very first step. You can then read the message parts one-by one and act on them.

    Note that, as already indicated, a message part might have the content type "message/rfc822" which means it contains another complete mail message (and it itself might be multi-part and contain other mail messages, and so on).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?