Chapter Contents |
Previous |
Next |
SAS Companion for the OpenVMS Operating Environment |
Using the DATA Step or SCL to Send Electronic Mail |
In general, a DATA step or SCL code that sends electronic mail has the following components:
Syntax of the FILENAME Statement for Electronic Mail |
FILENAME fileref EMAIL 'address' <email-options> |
to="joe@somplace.org"
and
to=("joe@smplc.org" "jane@diffplc.org")
are valid TO values.cc="joe@somplace.org"
and
cc=("joe@smplc.org" "jane@diffplc.org")
are valid CC values.subject=Sales
and
subject='June Report'
are valid SUBJECT values. Any subject text not enclosed in quotation
marks is converted to uppercase.You can also specify the email-options in the FILE statement inside the DATA step. Options that you specify in the FILE statement override any corresponding options that you specified in the FILENAME statement.
In your DATA step, after using the FILE statement to define your e-mail fileref as the output destination, use PUT statements to define the body of the message.
You can also use PUT statements to specify e-mail directives that changes the attributes of your electronic message or perform actions with it. Specify only one directive in each PUT statement; each PUT statement can contain only the text associated with the directive it specifies.
The directives that change the attributes of your message are the following:
The directives that perform actions are the following:
Example: Sending E-Mail from the DATA Step |
filename mymail email 'JBrown' subject='My CONFIG.SAS file'; data _null_; file mymail; put 'Jim,' put 'This is my CONFIG.SAS file.' put 'I think you might like the new options I added.' run;
The following example sends a message to multiple recipients. It specifies the email-options in the FILE statement instead of the FILENAME statement:
filename outbox email 'ron@acme.com'; data _null_; file outbox to=("ron@acme.com" "lisa@acme.com") /* Overrides the value in */ /* the filename statement. */ cc=("margaret@yourcomp.com" "lenny@laverne.abc.com") subject='My SAS output'; put 'Folks,'; put 'Take a look at my output from the SAS program I ran last night.'; put 'It worked great!'; run;
You can use conditional logic in the DATA step to send multiple messages and control which recipients get which messages. For example, suppose you want to notify members of two different departments that their customized reports are available. If your e-mail program handles alias names, your DATA step might look like the following:
filename reports email 'Jim' data _null_; file reports; infile cards eof=lastogs; length name dept $ 21; input name dept; put '!EM_TO!' name; /* Assign the TO attribute */ put '!EM_SUBJECT! Report for ' dept; /* Assign the SUBJECT attribute */ put name ','; put 'Here is the latest report for ' dept '.'; if dept='marketing'then put 'ATTN: Sales Representatives' else put 'For Your Information' put '!EM_SEND!'; /* Send the message */ put '!EM_NEWMSG!'; /* Clear the message attributes */ return; lastobs: put '!EM_ABORT!'; /* Abort the message before the */ /* Run statement causes it to */ /* be sent again. */ datalines; Susan marketing Jim marketing Rita development Herb development ; run;
The resulting e-mail message and its attachments are dependent on the department to which the recipient belongs.
Note: You must use the !EM_NEWMSG! directive to clear
the message attributes between recipients. The !EM_ABORT! directive prevents
the message from being automatically sent at the end of the DATA step.
Example: Sending E-Mail Using SCL Code |
mailto | the user ID to send mail to |
copyto | the user ID to copy (CC) the mail to |
subject | the subject of the mail message |
line1 | the text of the mail message |
The frame entry also contains a pushbutton called SEND
that causes this SCL code (marked by the
send:
label) to execute.
send: /* set up a fileref */ rc = filename('mailit','userid','email'); /* if the fileref was successfully set up */ /* open the file to write to */ if rc = 0 then do; fid = fopen('mailit','o'); if fid >0 then do; /* fput statements are used to */ /* implement writing the mail */ /* and the components such as */ /* subject, who to mail to, etc. */ fputrc1 = fput(fid,line1); rc = fwrite(fid); fputrc2 = fput(fid,'!EM_TO! '||mailto); rc = fwrite(fid); fputrc3 = fput(fid,'!EM_CC! '||copyto); rc = fwrite(fid); fputrc4 = fput(fid,'!EM_SUBJECT! '||subject); rc = fwrite(fid); closerc = fclose(fid); end; end; return; cancel: call execcmd('end'); return;
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.