Home > Mainframe, REXX > Sending e-mail from REXX

Sending e-mail from REXX

The basic SMTP protocol used for email is actually pretty simple and you can implement it quite easy using any socket interface.

I found the original version of this exec on the internet here although I have tweaked it a little. Original author is acknowledged.

Obviously you could modify it some to accept different parameters and maybe the bulk of the message data from the stack but it makes a  great starting point:

/* REXX EMAIL */
/* from http://ibmmainframes.com/about12103.html */

smtp_server      = 'mail.server.url'                 /* mail server address                   */
smtp_from        = 'someone@someplace.com'          /* Who this is from                      */
smtp_to          = 'someoneelse@somewhereelse.com'     /* Who it is to on the smtp_server       */
smtp_replyto     = 'someone@someplace.com'           /* Who to send any replies to.           */
                                                    /* Does not have to be same as smtp_form */

crlf = x2c('0d25')

/* SMTP Initialization */
str = Socket('initialize', Date(B))
Parse Var str sockrc subtaskid maxdesc tcpipuser
str = Socket('Socket', 'af_inet', 'stream', 'tcp')
Parse Var str sockrc sockid
str = Socket('SetSockOpt', sockid, 'sol_socket', 'SO_ASCII', 'on')
server_info = 'AF_INET 25 ' || smtp_server
str = Socket('Connect', sockid, server_info)
str = Socket('Recv', sockid, 10000)
say str
Parse Var str sockrc data_length smtp_response
msg= 'HELO ' || smtp_server || crlf
str = Socket('Send', sockid, msg)
str = Socket('Recv', sockid, 10000)
say str

/* Mail Message */
msg= 'MAIL FROM:<' || smtp_from || '>' || crlf
str = Socket('Send', sockid, msg)
str = Socket('Recv', sockid, 10000)
say str
/* Repeat RCPT TO section to send to multiple users */
msg= 'RCPT TO:<' || smtp_to || '>' || crlf
str = Socket('Send', sockid, msg)
str = Socket('Recv', sockid, 10000)
say str
msg= 'DATA' || crlf
str = Socket('Send', sockid, msg)
str = Socket('Recv', sockid, 10000)
say str
the_subject = 'This is a Test #1'
msg = 'To:' smtp_to || crlf ,
   || 'Reply-To:' smtp_replyto || crlf ,
   || 'Subject:' the_subject || crlf ,
   || 'X-Mailer: REXX Exec on ZOS' || crlf
str = Socket('Send', sockid, msg)
str = Socket('Send', sockid, crlf)
msg = 'This is a test (#1) from ' || Userid() || crlf
str = Socket('Send', sockid, msg)
msg = crlf || '.' || crlf
str = Socket('Send', sockid, msg)
str = Socket('Recv', sockid, 10000)
say str
/* End of Mail Message */

/* SMTP Termination */
msg= 'QUIT' || crlf
str = Socket('Send', sockid, msg)
str = socket('Close', sockid)
str = socket('Terminate', subtaskid)
Say 'Email sent to ' smtp_to
Exit

The only downside to this implementation as it stands is that you have to know the receiver’s mail server name. (shown as mail.server.url in the example). There is a mechanism (described in RFC 1035) for talking to a DNS server to obtain the MX (Mail Exchange) records for a domain so that you can find the url of the mail server(s) for a domain name (like IBM.com) but I’ve not looked into that yet. Something for the future maybe.

Advertisements
Categories: Mainframe, REXX
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: