This idea started a day when I sat and remembered the Good old days™, when I had a wordpress blog, and found this plugin, that could check a mailbox for new mails, and then create a post on my blog, based on that mail.
I used it to send small poems from my phone (via mms no less) when I was on the bus home from high school.
Why shouldn’t I be able to do that again?
I have tried to relive it with mail2MD, which if the name didn’t give it away, takes a mail and generates a Markdown file from it.
Then there just need to be some glue logic, that re-renders my web page and publishes it.
It was satisfyingly easy to stitch together in my mind at least.
The results can be found under Snippets.
Here are some write ups about the steps it took:
The goal First step was to get a concrete idea of what I actually wanted to do, and based on that, to choose a language. The start is a mail. The goal is a page on my web page. My web page uses markdown files for posts/content. That more or less involves these three steps:
Get mail -> Convert to format usable by web page -> Add to web page Get mail: I can create an extra account on my mail server....
Connecting I have never really worked with the inner workings of mails before. So I’m not quite sure what to expect. But the connection to the server was similar enough to any connection to an imap server (the server, username, password etc):
server = imaplib.IMAP4_SSL(s) # s is server address server.login(n, p) # n = username p = password server.select("inbox", readonly=only_read_mails) obj, data = server.search(None, '(FROM %s UNSEEN)' % f) # f = required from address The search part above gives back a list (data) with all the mails in the inbox, that matches the search query (from given address and unseen)....
Hugo formatting While Hugo can take Markdown files and use those for posts, they need to be in a specific layout. The most important is probably the front matter. The front matter contains the metadata for the post; date, title, tags etc. It can look like this (it’s the one from this post - still in “draft mode”):
--- title: "Writing the files" date: 2023-10-23T07:05:24+02:00 author: "Emil Harder" draft: false --- There should be some minus signs, title date and author segment....
Lose ends While I have explained the two biggest part of the script, I haven’t actually told about the part I’ve used the most: the function log().
While print() does work (especially for such a simple script), is it still nice to have a file. And one does not exclude the other. I have more or less the same logic for a simple log function written in the languages I use the most (C++, Python, Bash and even PowerShell (for work - I swear!...