7.2.3 Using a Filehandle and a Pipe to Send Email
7.2.3 Using a Filehandle and a Pipe to Send Email
In this section, we have seen an example of a pipe used to produce an electronic mail message. There is an example of using a pipe to produce an automatic e-mail message from a CGI program in Section 8.3.11.
In Perl, we can create filehandles that are associated not with files but with processes. System (e.g., Unix) processes can accept inputs or send outputs to Perl programs by using filehandles that are associated with the processes. A system process here means a system command. A command takes time, however small, to execute and hence is called a process in computer terminology. For example, if we want to write some text in our Perl program and use the text to compose a mail message that we want to send out using the Unix mail program, we can open a filehandle that sends output to this filehandle. We write out to this filehandle
before closing it. Closing of the filehandle sends out the mail message.
Let us look at a program that generates the letter somewhat like the one that we saw in the beginning of the chapter and actually e-mails it. The program is given below.
Program 7.8
#!/usr/bin/perl $you = $ARGV[0]; $me = $ARGV[1]; $high = $ARGV[2]; $low = $ARGV[3]; $space = " "; $date = `date`; open (MAIL, '| /usr/ucb/mail -s "Today\'s and Tomorrow\'s Weather" larsen\@pikespeak.uccs.edu'); print MAIL "\t" x 5, $date, "\n"; print MAIL "Dearest \u$you,\n\n"; print MAIL $space x 5; print MAIL "How are you doing? "; print MAIL "Today's high and low temperatures here in Colorado Springs were"; print MAIL " $high and $low degrees F. "; print MAIL "Tomorrow will be hotter!\n"; print MAIL "\n\u$me\n"; close (MAIL);
This program expects to get four command line arguments: the person receiving the message, the person sending it, the high temperature and the low temperature. It reads the four arguments from the @ARGV list and stores them in four scalars. Then, it calls the system’s date command to obtain the current date and time. This date is printed on top of the message that is created and e-mailed automatically. Next, it opens a filehandle called MAIL. This filehandle is not associated with a file, but with a process. The process is
/usr/ucb/mail which is where the Unix mail program is normally stored. This mail program takes an argument that is given by preceding it with -s. In this case, this argument gives us the subject of the mail message to be sent. The last argument to the mail program is the address of the person to whom the mail message is sent. The @ in the e-mail address has to be escaped with a backslash.
As we can see, when we open the filehandle MAIL, we need to state the second argument to it in string format. This string is single-quoted. The first character in the string is the vertical line (|). The vertical line is called a pipe in Unix terminology. This vertical line states that the contents of the filehandle will be used as input to (or, piped into) the following process, the mail command in this case. So, when we write to the filehandle, the stuff that
we write will be kept in the filehandle, and when the filehandle is closed, this stuff will be sent to (or, piped into) the mail process as its input. In this case, we write a brief letter to the MAIL filehandle. Once the complete text of the letter is written, we close the filehandle.
Closing the MAIL filehandle has the effect of sending all the contents of the filehandle (i.e., the letter we have composed) to the mail process. The mail process will create a subject based on the value of the first argument given to it. The mail program then sends the electronic message to the address given as its second argument.
As an example, the program is invoked as
%mailpipe mia jugal 80 40
The output of the program for this invocation is that it sends a mail message out to
larsen@pikespeak.uccs.edu. The mail message sent out is given below.
Date: Tue, 17 Jun 1997 15:29:49 -0600 From: "J. Kalita"To: larsen@pikespeak.uccs.edu Subject: Today's and Tomorrow's Weather Tue Jun 17 15:29:48 MDT 1997 Dearest Mia, How are you doing? Today's high and low temperatures here in Colorado Springs were 80 and 40 degrees F. Tomorrow will be hotter! Jugal
