Goal:Send email from linux command line using sendmail with a monospaced font

Difficulty: Easy

Prerequisites: Sendmail Installed, Access to a Linux Console

There are many times when formatting an email is not only a nice to have, but necessary to make it useful to the recipient. In this tutorial I will cover how to send an email from linux, using the sendmail program, with fixed font, also known as monospaced font.

This example contains the following files:

mailheader.txt - A text file that contains our static header data, things like the html starting tags, content type, etc

mailbody.txt - A text fiel that contains the body / text of the email we want to send in fixed font / monospace font

mailfooter.txt - A text file that contains our static footer data, basically just there to close out tags for our mailheader.txt, most likely with simple examples this won't be required but since i'm slightly anal about closing my html tags I'll leave it in here even though most things will render the email fine without them.

mailsender.sh - The shell script that runs our sendmail command and ties all of our formatting files together with our content file


MIME-Version: 1.0
Content-Type: text/html
Content-Disposition: inline
<pre style="font: monospace">

So breaking down mailheader.txt. We are setting the MIME version so that we can set a content type of html and content disposition of inline. Basically what this means is that from here on, the email will be an HTML formatted email, and the content should be inline in the email, as opposed to an attached html file. We then use the

<pre> tag to indicate that the content after this should use html's pre-formatting option which basically makes it so that we can include a simple blurb of text after it and we don't have to worry about our line endings or extra whitespace that html would typically ignore. The other thing we'll note is that we are setting an HTML font styling of monospaced. Now, there are a large number of fonts to choose from but in this example we are going for our typewriter style font so I chose monospace, you could just as easily have made this times new roman or your favorite web font.


Fruit     Number    Color     
Apple     100       Green     
Apple     2000      Red       
Orange    50        Orange

This is our text file that we want to include in a fixed/monospaced font in our email. Typically in my mind this means a fixed font table of some sort but there it certainly works for anything you might want.



And as I said in the file descriptions above, mailfooter.txt isn't the most difficult of files to read. It's just here to close our html tags out from the mailheader.txt file. Additionally, if you were using a multi-boundary email to say include some attachments, you could close off your html section here and start the necessary headers for your attachment.... But I digress.


        echo "From: John Doe <john.doe@example.com>";
        echo "To: jane.doe@example.com";
        echo "Subject: My Awesome Typewriter Email";
        cat mailheader.txt mailbody.txt mailfooter.txt
) | /usr/sbin/sendmail -t

And now the script that ties them all together. In this file we set the from address, to address, subject, and we tie all of our text files together into one big happy family.


Leave a Reply

(Your email will not be publicly displayed.)