Honestly this probably puts this script to bed, but a bit of Frankensteining never hurt anyone. Let's see if we can't keep this script alive.
Try to preserve some backwards compatibility so old mail services still can work through this script, but focus on upgrading the script to modern standards.