Preskoči na glavno vsebino

Receiving emails with Go's smtpd and Rye

This goes a while back. At some project for user support, we needed to receive emails and save them to appropriate databases. The best option back in the day seemed project Lamson. And it worked well ever since. It was written in Python by then quite known programmer Zed Shaw.

It worked like a Python based SMTP server, that called your handlers when emails arrived. It was sort of Ruby on Rails for email. We were using this ever since.

Now our system needs to be improved, there are still some emails or attachments that don't get parsed correctly. That isn't the problem of Lamson, but of our code that parses the emails. But Lamson development has been passive for more than 10 years.

And I am already moving smaller utilities to Rye. 

Rye uses Go, and Go has this nice library smtpd, which seems like made for this task. I integrated it and parsemail into Rye and tested it in the Rye console first.

Interesting function here is enter-console, that can put you into Rye console anywhere in the code - like here, directly in the email handler. You can look around with function ls, that lists the current context and evaluate expressions. The last value returned is also a returning value of the enter-console function, so like everything else in Rye this can be used as part of a (coder in the middle) expression. :)

 

Next step was to create the minimal script that accepts the emails and saves them to the SQLite database. Rye has a dialect for SQL that generates prepared SQL statements from blocks of Rye code/data (not strings). Other code is similar than in the console example.

Now I can start rewriting the old, a little quick and dirty Python/Lamson script I wrote all those years back. Here we use MySQL to get the ID of the project based on the "to" email address, and then open the SQLite database in that folder.

The Rye part is quite shorter, mostly because Python's database handling (mysql and sqlite) that I used at that time is quite low-level. I'm not sure if they have something more elegant today. Email parsing library in Python also seems lower-level than parsemail that I got from Go.



A library being higher level isn't always bad, sometimes you need or want lower level access, but it can result in more code.

click to zoom-in

I went through all 3 steps more in detail in 3 separate reddit posts:

  1. Simple SMTP app-server (receiver) with Ryelang
  2. Incoming email to Sqlite with Ryelang
  3. Translating older Python + Lamson receiver script to Rye

Next thing to add would be handling of attachments and embedded files which parsemail library already does.

 

If you found any of this interesting you can also visit Rye's reddit group r/ryelang or Rye's github repo.

Komentarji

Priljubljene objave iz tega spletnega dnevnika

Less variables, more flows example vs Python

In the last blogpost ( Less variables, more flows ) I wrote a quick practical script I needed. It was an uncommon combination of CGI, two GET requests with Cookies and a POST request with Authorization header. I really like practical random/dirty problems, rather than ideal - made up problems to test the language. To get a sense of comparison I rewrote the example 2 times while removing specific Rye features. But that comparison is meaningless to a person that doesn't know Rye or at least Rebol already. So I went on fiverr and made a request for a Python script with these requirements. I got a nicely written Python script that uses functions for each step. To be more comparable, I rewrote the Rye code to a similar structure. Below is the result ... For a next step, it would be interesting, to extract a little simpler example out and add error handling. With Rye-s specific failure handling, I think the difference would become even greater. You can find Rye on github .

Ryelang - controlled file serving example and comparison to Python

This is as anecdotal as it gets, but basic HTTP serving functions in Rye seem to be working quite OK. They do directly use the extremely solid Go 's HTTP functions, so that should be somewhat expected. I made a ryelang.org web-server with few lines of Rye code 3 months ago and the process was running ever since and served more than 30.000 pages. If not else, it  seems there are no inherent memory leaks in Rye interpreter. Those would probably show up in a 3 month long running process? And now I got another simple project. I needed to make a HTTP API for some mobile app. API should accept a key, and return / download a binary file in response if the key is correct. Otherwise it should return a HTTP error. So I strapped in and created Rye code below. I think I only needed to add generic methods stat and size? , all other were already implemented, which is a good sign. Of course, we are in an age of ChatGPT, so I used it to generate the equivalent  Python code. It used the elegant