(I have no idea if there are any projects out there that are named attmail. No copyright violations intended, it’s just my woking title 😉 )
Today, I had to get a backup script running on the server of my company. One part of the script should send an eMail with the backup attached every week.
Easy thing, right? I’ve got SSH access, so I can use just about anything a Linux Server has to offer.
Yeah, as it turns out, sending mails with attachments is actually not that easy if you can’t install your own stuff on the machine. The only (working) eMail solution available was Sendmail, and for that, you need to basically provide the whole eMail, encoded and everything. And, to make matters even worse, I had no access to uuencode. At least, I had Base64, so I knew that, somehow, it had to be possible.
Luckily, I found this thread, explaining how to send Base64-encoded attachments via sendmail. Bingo.
Being the computer science student that I am, I was not satisfied with the script (I am never satisfied with scripts that have everything fixed. I like my parameters). So, I set out to rework the script to make it more dynamic.
Now, about 3 hours later, I finally have a working solution (and have learned a lot about bash scripting). I know that any experienced Linux guy will probably claw his eyes out on seeing this code, but I’ll post it nontheless.
The Code on Pastebin (including usage instructions)
This Project on GitHub, in the unlikely case you want to fork and modify it.
It should be obvious that the code requires a working installation of sendmail to work correctly.
As always, feel free to comment with suggestions on how to improve the code, report any bugs (Untested usage scenarios include full path to attachment file (should work, I think), Filenames with Spaces or special characters except for .-_, and more). The code works for the scenario I intended it for, and that’s all I wanted. If it helps you, even better, but I will not offer too much in the way of support, due to time constraints and other problems.
Thanks, and have a nice day 😉