DEV Community

Nivethan
Nivethan

Posted on • Edited on

A Mail Command in Python - 01 A Command Line App

We're going to write a tiny mail utility in python. The goal is to mimic the mail command with the same set of flags. The reason was that trying to get the mail command to handle html was a pain in the ass and involved messing with the mail headers manually. It would be much better to use python and have it do the header manipulation. I however also don't want to re-write any of the existing scripts so we want to keep the same exact interface!

The first step is to figure out which flags we'll need.

Most of my mail commands look like this:

> echo "Body" | mail -s "Subject" -c cc@address.com -a /path/to/file/Intrcall.pdf -r from@address.com to@address.com
Enter fullscreen mode Exit fullscreen mode

I pipe the e-mail body to the mail command and then set the subject, any cc addresses, add attachments, set the from address and finally set the destination.

The goal is to write a python version that could read in the above command and do the same thing as the mail command.

Let's get started!

argparse

Python has a builtin library to handle command line arguments that works quite well. There is quite a bit of magic in this library which I don't like but the results speak for themselves. You can get a pretty command line program up and running very easily with python.

The first snippet of code to run is this:

#!/usr/bin/env python3

import argparse

def main():
    parser = argparse.ArgumentParser()
    args = parser_args()

main()
Enter fullscreen mode Exit fullscreen mode

You can save this without the .py extension as this will be an executable we run. I named the program pymail. (Very creative).

I have also set the shebang(#!) to reference python3 using the env. This is because I don't know where this script will run and which version of python I might have or where it would be. This makes it so that whatever is linked python3 will be used to run this script.

Now we need to mark the program as executable:

> chmod +x pymail
Enter fullscreen mode Exit fullscreen mode

Now you can execute the program:

> ./pymail
>
Enter fullscreen mode Exit fullscreen mode

Voila! We get nothing! A bit underwhelming but! so much magic has happened.

Try the following:

> ./pymail -h
Enter fullscreen mode Exit fullscreen mode

You should see the below screen. By using the argparse library we get some pretty help text and it sets up the first flag for us.

usage: pymail [-h]

optional arguments:
  -h, --help  show this help message and exit
Enter fullscreen mode Exit fullscreen mode

Let's add a more descriptive help text.

    parser = argparse.ArgumentParser(description="Mail replacement in python.")
Enter fullscreen mode Exit fullscreen mode

This will now print our description when someone looks for help.

> ./pymail -h
usage: pymail [-h]

Mail replacement in python.

optional arguments:
  -h, --help  show this help message and exit
Enter fullscreen mode Exit fullscreen mode

We can also add an epilog to print some text after all of our flags as well.

    parser = argparse.ArgumentParser(description="Mail replacement in python", epilog="* not a meal replacement *")
Enter fullscreen mode Exit fullscreen mode

This would print out:

> ./pymail -h
usage: pymail [-h]

Mail replacement in python.

optional arguments:
  -h, --help  show this help message and exit

* not a meal replacement *
Enter fullscreen mode Exit fullscreen mode

Now we have argparse set up and ready to be used. In the next chapter we'll add all the flags that make a mail command useful!

Top comments (0)