Simple email server tricks

By Cameron Laird and Kathryn Soraiz, Unix Insider |   Add a new comment

Let's automate email.

We are continually surprised at how many moderately experienced computer users and developers don't know the basic facts about email. Throughout the coming year, we'll explain a few of the essentials and show working scripts that solve common problems.

Automating the original "killer app"

We start with the essentials. Email items travel across the Internet as plain-text byte streams, formatted according to RFC 822 (see Resources, below). This specifies a simple structure of a header followed by content, with a blank line separating the two. A minimal message might then look something like


     From someone@somewhere.net Tue Mar  6 16:16:00 2001
     
     This is a message.

Items typically have considerably more elaborate headers, including elements such as To:, From:, Subject:, and so on.

This is enough background to understand one of the questions we come across most often: "How can I automatically send out an email item with a Subject (or Cc, or Bcc, or ...) that says X?" In a typical Unix environment, all it takes is a command line invocation


       sendmail -t << END
       From: myaccount@myhost.com
       Subject: This is the subject.
       To: intended_recipient@somewhere.com
       Cc: other_person@somewhere.com
       Bcc: my_records@myhost.com

       Hello.  This is the message.  Goodbye.
       END
   

You can also emit email messages directly from most languages, without an apparent need to invoke an external process such as the sendmail used here. We use this most often when testing email service, and especially in architecting unusual sites; by coding at a lower level, it's easy to access alternative network ports, request unusual relay topologies, and so on. Through the Resources below, you can locate ways to code email transmissions in such other common scripting languages as KornShell, Perl, Python, Rexx, Ruby, and Tcl.

However, email agents including sendmail add a great deal of value that's not immediately apparent. They will, for example, retry transmissions so that temporary breaks in connectivity don't thwart your attempts to get through. Therefore, our usual advice to developers automating most email operations is just to "shell out" and invoke a specialized, external email agent. All the most popular scripting languages make that easy.

Do you want to attach a file to your outbound message? Many correspondents do. While that takes only a few keystrokes more than a simple message, the concepts behind attachments are widely misunderstood. Let's take a few moments to be clear on the subject.

MIME packages payloads

We've already mentioned that as it travels around the Net from sender to recipient, all email traffic looks the same: header, blank line, body, all expressed in simple ASCII characters. A message with attachments follows the same rules. It just happens to have a body (plus, most likely, a small piece of the header) that can be interpreted so as to reconstitute the original contents of the attached files.

Standards documents such as RFC 1521 define how the Multipurpose Internet Mail Extensions (MIME) wrap up external files to fit them inside an email message body. This packaging is so successful, in fact, that MIME is also used for many applications, including the World Wide Web, that have no direct connection to email.

Therefore, your first instinct in automating messages with attachments should be as in the first section above: find a special-purpose mailing agent that knows the language. Sendmail doesn't, and none of the applications that do are as ubiquitous as sendmail. Most hosts, though, have at least one MIME-savvy processor. With a client like mutt installed, it's easy to


      mutt -a $ATTACHMENT_FILE -i $CONTENT_FILE \
                         -s $SUBJECT $ADDRESS
   

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    Ask a question

    Ask a Question