Google API Gmail LINE Notifications (7 Part Series)
1 Connecting to Gmail API with Python
2 Managing your Labels in Gmail
… 3 more parts…
3 Searching for and Getting Emails
4 Processing Email Contents
5 Processing Email – Being Selective
6 Processing Email – Single Script / Multiple Emails
7 Automation with Systemd & Code
Part 4 in a series of articles on implementing a notification system using Gmail and Line Bot
Hi. Back again for another instalment.
Today I will be working through accessing parts of an email. In part 3 we got to the stage where I had created an email.message.EmailMessage
object. You may not believe that is ready to use. Since if you played with it you might have found that it still contained a lot of non-ascii encoded characters, but thats ok. Once we start using the member methods provided by a email.message.EmailMessage
object. Things will be clear.
So a quick review:
We did our search, got our message ids, boiled it down to just the message ids minus the threadIds
We then used get_message(service, msg_id)
to return an email.message.EmailMessage
object.
<span>single_email</span> <span>=</span> <span>get_message</span><span>(</span><span>service</span><span>,</span> <span>some_id</span><span>)</span><span>single_email</span> <span>=</span> <span>get_message</span><span>(</span><span>service</span><span>,</span> <span>some_id</span><span>)</span>single_email = get_message(service, some_id)
Enter fullscreen mode Exit fullscreen mode
If you print this with print(single_email)
you will get the string representation of the entire email. If it is not in ascii you might see subject line that looks like this.
Subject: <span>=</span>?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI<span>=</span>?<span>=</span>Subject: <span>=</span>?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI<span>=</span>?<span>=</span>Subject: =?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI=?=
Enter fullscreen mode Exit fullscreen mode
And have an email body which is just as confusing. But thats ok. We will use the methods provided by email.message.EmailMessage
to get these string returned in a readable form.
Here is a list of some the methods we can use:
single_email.add_alternative<span>()</span> single_email.get_params<span>()</span>single_email.add_attachment<span>()</span> single_email.get_payload<span>()</span>single_email.add_header<span>()</span> single_email.get_unixfrom<span>()</span>single_email.add_related<span>()</span> single_email.is_attachment<span>()</span>single_email.as_bytes<span>()</span> single_email.is_multipart<span>()</span>single_email.as_string<span>()</span> single_email.items<span>()</span>single_email.attach<span>()</span> single_email.iter_attachments<span>()</span>single_email.clear<span>()</span> single_email.iter_parts<span>()</span>single_email.clear_content<span>()</span> single_email.keys<span>()</span>single_email.defects single_email.make_alternative<span>()</span>single_email.del_param<span>()</span> single_email.make_mixed<span>()</span>single_email.epilogue single_email.make_related<span>()</span>......single_email.add_alternative<span>()</span> single_email.get_params<span>()</span> single_email.add_attachment<span>()</span> single_email.get_payload<span>()</span> single_email.add_header<span>()</span> single_email.get_unixfrom<span>()</span> single_email.add_related<span>()</span> single_email.is_attachment<span>()</span> single_email.as_bytes<span>()</span> single_email.is_multipart<span>()</span> single_email.as_string<span>()</span> single_email.items<span>()</span> single_email.attach<span>()</span> single_email.iter_attachments<span>()</span> single_email.clear<span>()</span> single_email.iter_parts<span>()</span> single_email.clear_content<span>()</span> single_email.keys<span>()</span> single_email.defects single_email.make_alternative<span>()</span> single_email.del_param<span>()</span> single_email.make_mixed<span>()</span> single_email.epilogue single_email.make_related<span>()</span> ... ...single_email.add_alternative() single_email.get_params() single_email.add_attachment() single_email.get_payload() single_email.add_header() single_email.get_unixfrom() single_email.add_related() single_email.is_attachment() single_email.as_bytes() single_email.is_multipart() single_email.as_string() single_email.items() single_email.attach() single_email.iter_attachments() single_email.clear() single_email.iter_parts() single_email.clear_content() single_email.keys() single_email.defects single_email.make_alternative() single_email.del_param() single_email.make_mixed() single_email.epilogue single_email.make_related() ... ...
Enter fullscreen mode Exit fullscreen mode
Headers can simply be accessed using single_email.get('headername')
Examples:
<span>from</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>"from"</span><span>)</span><span>subject</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>"subject"</span><span>)</span><span>from</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>"from"</span><span>)</span> <span>subject</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>"subject"</span><span>)</span>from = single_email.get("from") subject = single_email.get("subject")
Enter fullscreen mode Exit fullscreen mode
To check if an email is multipart; single_email.is_multipart()
will return a True
or False
There are lots of methods to use to deconstruct an email. Fortunately for me. The emails I am dealing with are system generated and also very simple plain text non-multipart.
Let’s look at the subject of the email.
Subject: <span>=</span>?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI<span>=</span>?<span>=</span>Subject: <span>=</span>?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI<span>=</span>?<span>=</span>Subject: =?ISO-2022-JP?B?GyRCRn5CYDw8Pn//yROJCpDTiRpJDsbKEI=?=
Enter fullscreen mode Exit fullscreen mode
Using the get
method:
<span>sub</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>'subject'</span><span>)</span><span>print</span><span>(</span><span>subject</span><span>)</span><span>sub</span> <span>=</span> <span>single_email</span><span>.</span><span>get</span><span>(</span><span>'subject'</span><span>)</span> <span>print</span><span>(</span><span>subject</span><span>)</span>sub = single_email.get('subject') print(subject)
Enter fullscreen mode Exit fullscreen mode
I get:
入退室情報のお知らせ入退室情報のお知らせ入退室情報のお知らせ
Enter fullscreen mode Exit fullscreen mode
Note that I didn’t actually need to know the character encoding. This was due to the way the parser object was setup using the arguments policy=policy.default
in the previous post.
So as you can see, getting the header details is pretty easy. How about getting the body of the email? Again this is pretty simple when dealing with a single non-multipart email. I will simply use get_content()
<span>body</span> <span>=</span> <span>single_email</span><span>.</span><span>get_content</span><span>()</span><span>print</span><span>(</span><span>body</span><span>)</span><span>body</span> <span>=</span> <span>single_email</span><span>.</span><span>get_content</span><span>()</span> <span>print</span><span>(</span><span>body</span><span>)</span>body = single_email.get_content() print(body)
Enter fullscreen mode Exit fullscreen mode
Redacted Redacted�=J_] �@O5 �12K^0[0=^ 様の入退室情報をお知らせします。【セーフティメール情報】2021-02-01 19:08:26 に退室しました。※なお、このメールに返信することはできませんのでご注意くださいRedacted Redacted �=J_] �@O5 �12K^0[0=^ 様の入退室情報をお知らせします。 【セーフティメール情報】 2021-02-01 19:08:26 に退室しました。 ※なお、このメールに返信することはできませんのでご注意くださいRedacted Redacted �=J_] �@O5 �12K^0[0=^ 様の入退室情報をお知らせします。 【セーフティメール情報】 2021-02-01 19:08:26 に退室しました。 ※なお、このメールに返信することはできませんのでご注意ください
Enter fullscreen mode Exit fullscreen mode
If you are dealing with an email that is multipart, you will need to use the walk()
method. Combined with get_content_maintype()
and get_content_subtype()
to identify or find things like plain text and HTML or binary attachments.
There already exists some good Python documentation for this. So I won’t go into it here.
That’s it for this article. Next I will give some information on regex for dealing with Japanese. But you can also take a look at this earlier post.
Google API Gmail LINE Notifications (7 Part Series)
1 Connecting to Gmail API with Python
2 Managing your Labels in Gmail
… 3 more parts…
3 Searching for and Getting Emails
4 Processing Email Contents
5 Processing Email – Being Selective
6 Processing Email – Single Script / Multiple Emails
7 Automation with Systemd & Code
暂无评论内容