mailing from unix

This is more work related material, and is posted as an article here for easy retrieval if and when I forget the syntax.

The background:
My SQL code runs on a remote AIX Unix box. A typical bit of code will generate some tables on the database, a log file, and a spooled file designed to be the body of an email message. After a bit of code completes, these files are an example of what may be present:

  • some_code.email
  • some_code.logfile

My first order of business is to send email about the code that has just run to the people who need to know. Inside my SQL scripts, the “!” symbol lets my SQL code know to run the next line as a shell command, then take control again. The some_code.email file has already spooled on and off, and contains a summary of the code that is has just ran- counts, table names, that sort of thing. A combinaton of pg and mailx are used to have some_code.email inserted as the body of an email message:

! pg some_code.email |mailx -s "some_code has run!" somebody_important@acxiom.com;

pg is similar to cat in that it displays the target (some_code.email). In this case the display doesn’t go to the screen, but it is piped to the mailx program. Doing so effectively inserts the contents of the .email file as the body of a new message. The -s flag signals that the next bit, in quotes in the above example, is the subject line of the message. The last part is self explanatory, it is the email address of the recipient.

If one wants to email to multiple recipients, each email address is seperated by a space:

! pg some_code.email |mailx -s "some_code has run!" somebody_important@acxiom.com also_important@acxiom.com;

This works just fine. It isn’t the best solution for all cases. For instance, I was using this method for both the email notificiations and sending myself the logfiles from the code. This method did work, but it meant that I would get the lengthy logfile as the body of an email message. To save this to my computer, I would cut and paste the body of a message into a text editor an save the logfile from there.

This method worked until I started generating very large reports. For one thing, the reports are being output in comma seperated value format. It seemed like extra effort to cut and paste the csv into a text editor, save it as a .csv text file, then open it with Excel. The other is that some of these reports are very big for being .csv- 5mb and bigger for the most complex set of queries. Try cutting and pasting that!

What I want to do is to receive the file as an attachment. An attachment that doesn’t max out my inbox quota would be nice. To accomplish this, two more tools come into play: uuencode and gzip.

In this example, instead of the files mentioned prevously, I only have 1 to deal with:

  • myfile.audit

The first step is to compress it to reduce file size:

gzip -c myfile.audit > myfile.audit.gz

This step leaves the original file alone and creates a gzipped copy (myfile.audit.gz) as well.

The next step is to transform the archive into something attachable, and stick it to an email. This is where uuencode comes into play:

uuencode myfile.audit.gz myfile.audit.gz |mailx -s "unknown dog" unknown@dog.com

Update (12/20):
I figured out how to take all my reports and getting them email to myself as a single compressed archive. In this example, my audit reports are integrated into my campaign code. Each report is generated as a comma seperated value (.csv) file.

!tar -cf my_reports.tar *.csv;
!gzip -c my_reports.tar > my_reports.tar.gz ;
!uuencode my_reports.tar.gz my_reports.tar.gz | mailx -s "My Reports!" unknown@dog.com;

This entry was posted in computers/programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *