I'm using Spark as an email app on iOS, but sometimes the forwarded email isn't displayed in the body and is attached as a .eml
file. (This seems to be caused by settings on the Thunderbird user side.-> Mozilla Question Box)
This .eml
file cannot be opened directly on your iphone. You can see it by using a paid application or an online conversion page, but it is a little troublesome. This is solved with a discord bot.
The source of this discord bot is written in Python.
I used this content for the process of extracting the content from the .eml
file. -> Qiita
--When you send an .eml file from your smartphone to the channel or DM where the discord bot is, the text file with the contents will be sent to discord. --Attachments are also sent. (** This feature is unconfirmed **)
Since the body of the email tends to be long, I sent it to discord as a text file instead of the content of the message.
source
Except for MailParser
, the contents are not so relevant this time.
main.py
from MailParser import MailParser
import discord
from discord.ext import commands
description = ""
bot = commands.Bot(command_prefix="?", description=description)
@bot.event
async def on_ready():
print("Logged in as")
print(bot.user.name)
print(bot.user.id)
print("------")
Also add it to the end of on_message
in @ bot.event
.
main.py
@bot.event
async def on_message(message):
#If there are attachments
if (len(message.attachments) > 0):
for att in message.attachments:
#The extension is.Determine if eml
file_extension = att.filename.split(".")[-1]
if file_extension == ".eml":
await read_eml(message.channel, await att.read())
To avoid garbled characters, it is encoded in Shift-JIS.
main.py
async def read_eml(discord_channel, file_content: bytes):
"""Read eml file,Display the contents"""
#eml read
eml_result = MailParser(file_content).get_attr_data2()
#
file_path = f"{os.path.dirname(__file__)}/../out/eml.txt"
# shift-Characters are garbled if it is not jis->Chrome-kun
with open(file=file_path, mode="w", encoding="shift-jis", errors="replace") as f:
f.write(eml_result["content"])
await discord_channel.send(file=discord.File(file_path))
for att in eml_result["attach"]:
with open(f"{os.path.dirname(__file__)}/../out/{att['name']}", "wb") as f:
f.write(att["data"])
await discord_channel.send(file=discord.File(att["name"]))
```
### Read the contents of eml (change MailParser a little)
Add subject to content and add a method that also returns attachments.
#### **`main.py`**
```python
def get_attr_data2(self):
"""
Get email data
"""
mail_content = f"""\
SUBJECT: {self.subject}
----
FROM: {self.from_address}
TO: {self.to_address}
CC: {self.cc_address}
----
{self.body}
----
ATTACH_FILE_NAME:
{",".join([ s["name"] for s in self.attach_file_list])}
"""
return {"content":mail_content, "attach":self.attach_file_list}
setattr(MailParser, get_attr_data2.__name__, get_attr_data2)
```
## how to use
1. Send the `.eml` file from the share to Discord.
2. The bot sends back the text file.
3. Open it in your browser and you will see the contents.
![EML_view_ss.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/306461/abad2fc7-a035-62e2-9fb1-caaf279a7f6d.png)
Recommended Posts