Here's my first attempt at a script to store my SMS incomings and outgoings in a MySQL/MariaDB database. I use Perl and this code is likely to go through a tidy-up. It doesn't use formail.
I wanted to store as much information as possible in the same table for both SENT and RECEIVED entries. I also wanted to store the actual message text for later processing by a separate script: hence the "processed" column. New entries are set to N(o) by default, and would be updated to P(rocessing) when being processed by the other script. When processing is complete it'll be set to Y(es).
The message text itself is stored with line-ends replaced by the " | " (pipe) symbol so that I can easily parse the message as a single string: I did this before when I used a commercial email-to-SMS gateway and it works well.
Anyway, have fun, and ignore my coding "style" - it works for me but YMMV
..and here's an extract from the table view: