Sending Mail with REXX
Written by Dave Briccetti
[ This article was taken from Dave Briccetti's OS/2 Warp Programming Tips and Samples web site with permission. - Editor ]
This tip demonstrates sending email to a list of addresses using REXX, SMTP, and TCP Sockets.
If you are a REXX, SMTP, or Sockets expert and you see any errors or possible improvements to this tip, please share your knowledge with me.
When the OS/2 Bay Area User Group lost its automated email mailing list server in November, 1995, I was forced to find another way to notify our members about our meetings. I had the email addresses in an ASCII file, dumped from our member database.
First I wrote a simple REXX command to call SENDMAIL for each address in the list:
arg MailingList do while lines(MailingList) rec = linein(MailingList) 'sendmail -Ce:\mptn\etc\sendmail.uml -af msg -f email@example.com "', || rec || '"' endFigure 1) Simple, but inefficient, REXX script to mail announcements.
This method was very slow, as long as a minute or more for each address because SENDMAIL was doing a great deal of domain name service activity; this generated a lot of traffic over my 28.8K bps modem to the domain name server at my Internet service provider. SENDMAIL actually connected with each recipient's mail system, and often there were delays in connecting. OS/2 email programs such as Post Road Mailer and PMMail very quickly send the mail to an intermediary which actually delivers the mail.
To send mail these OS/2 email programs use the Simple Mail Transfer Protocol (SMTP). To create a communication channel, these programs use TCP Sockets, probably from C or C++ language.
I decided to use the same approach, but since I had recently discovered the REXX sockets interface I thought I'd give it a go with REXX.
The Why's and How's
Here's the result of running the program with only a singler user email address in the mailing list file, with commentary interspersed. The actual SMTP commands we are sending are shown as well.
220-blob.best.net Sendmail 8.6.12/8.6.5 ready at Thu, 16 Nov 1995 02:57:09 -0800 220 ESMTP spoken here
This is what the mail server says when we connect to it. The 220 code tells us that all is well and we can continue.
This is the extended SMTP Hello command which starts the protocol and identifies us.
250-blob.best.net Hello daveb.vip.best.com [188.8.131.52], pleased to meet you 250-SIZE 250 HELP
This response (code 250) tells us that the server supports extended SMTP, including the two extensions, SIZE and HELP.
MAIL FROM:firstname.lastname@example.org SIZE=776
We begin a mail message, identifying the sender, and providing the size of the message. The SIZE parameter is an SMTP extension, and we are supporting it because the server supports it.
250 email@example.com... Sender ok
Again the 250 tells us things are going well.
We specify the recipient.
250 firstname.lastname@example.org... Recipient ok
All still ok.
We indicate we're ready to send the body of the message.
354 Enter mail, end with "." on a line by itself
The 354 tells us to send the data.
250 CAA19942 Message accepted for delivery
We're done with the SMTP conversation.
221 blob.best.net closing connection
And so is the server.
You can also get the completed REXX program in RXMAIL.ZIP.