|Using External Files and Devices|
|Advantages of Sending Electronic Mail from within SAS|
|Initializing Electronic Mail|
By default, SAS uses SMTP (Simple Mail Transfer Protocol) to send e-mail. SMTP, unlike some external scripts, supports attachments. This default is specified by the EMAILSYS system option. For information about how to change the e-mail protocol, see EMAILSYS System Option: UNIX.
Before you can send e-mail from within SAS, your system administrator might need to set the EMAILHOST system option to point to the SMTP server. For more information about the EMAILHOST system option, see SAS Language Reference: Dictionary.
|Components of the DATA Step or SCL Code Used to Send E-mail|
PUT statements that contain special e-mail directives (of the form !EM_directive!) that can override the e-mail attributes (TO, CC, BCC, SUBJECT, ATTACH) or perform actions (such as SEND, ABORT, and start a NEWMSG)
|Syntax of the FILENAME Statement for Electronic Mail|
|FILENAME fileref EMAIL 'address' <email-options>;|
specifies the primary recipients of the electronic mail. If an address contains more than one word, enclose it in quotation marks. To specify more than one address, enclose the group of addresses in parentheses, enclose each address in quotation marks, and separate each address with a space. For example, firstname.lastname@example.org' and to=("email@example.com" "firstname.lastname@example.org") are valid TO values.
Note: You can send an e-mail without specifying a recipient in the TO= option as long as you specify a recipient in either the CC= or BCC= option.
specifies the recipients you want to receive a copy of the electronic mail. If an address contains more than one word, enclose it in quotation marks. To specify more than one address, enclose the group of addresses in parentheses, enclose each address in quotation marks, and separate each address with a space. For example, email@example.com' and cc=("firstname.lastname@example.org" "email@example.com") are valid CC values.
specifies the recipients you want to receive a blind copy of the electronic mail. Individuals listed in the bcc field will receive a copy of the e-mail. The BCC field does not appear in the e-mail header, so that these e-mail addresses cannot be viewed by other recipients.
If a BCC address contains more than one word, enclose it in quotation marks. To specify more than one address, enclose the group of addresses in parentheses, enclose each address in quotation marks, and separate each address with a space. For example, firstname.lastname@example.org' and bcc=("email@example.com" "firstname.lastname@example.org") are valid BCC values.
specifies the subject of the message. If the subject text is longer than one word (that is, it contains at least one blank space), you must enclose it in quotation marks. You also must use quotation marks if the subject contains any special characters. For example, subject=Sales and subject='June Report' are valid subjects. Any subject not enclosed in quotation marks is converted to uppercase.
specifies the physical name of the files to be attached to the message and any options to modify attachment specifications. Enclose filename.ext in quotation marks. To attach more than one file, enclose the group of filenames in parentheses. For example, attach='/u/userid/opinion.txt' and attach=("june98.txt" "july98.txt") are valid file attachments.
By default, SMTP e-mail attachments are truncated at 256 characters. To send longer attachments, you can specify the LRECL= and RECFM= options from the FILENAME statement as the attachment-options. For more information about the LRECL= and RECFM= options, see FILENAME Statement: UNIX.
|Specifying E-mail Options in the FILE Statement|
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.
|Defining the Body of the Message|
|Specifying E-mail Directives in the PUT Statement|
You can also use PUT statements to specify e-mail directives that change 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.
Sends the message with the current attributes. By default, SAS sends a message when the fileref is closed. The fileref closes when the next FILE statement is encountered or the DATA step ends. If you use this directive, SAS sends the message when it encounters the directive, and again at the end of the DATA step.
|Example: Sending E-mail from the DATA Step|
Suppose that you want to share a copy of your config.sas file with your coworker Jim, whose user ID is JBrown. If your e-mail program handles alias names and attachments, you could send it by submitting the following DATA step:
filename mymail email 'JBrown' subject='My CONFIG.SAS file' attach='config.sas'; 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;
filename outbox email 'email@example.com'; data _null_; file outbox /* Overrides value in filename statement */ to=('firstname.lastname@example.org' 'email@example.com') cc=('firstname.lastname@example.org' 'email@example.com') subject='My SAS output' attach=('results.out' 'code.sas') ; put 'Folks,'; put 'Attached is 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 message. For example, suppose you want to send customized reports to members of two different departments. If your e-mail program handles alias names and attachments, your DATA step might look like the following:
filename reports email 'Jim'; data _null_; file reports; infile cards eof=lastobs; length name dept $ 21; input name dept; /* Assign the TO attribute */ put '!EM_TO!' name; /* Assign the SUBJECT attribute */ put '!EM_SUBJECT! Report for ' dept; put name ','; put 'Here is the latest report for ' dept '.'; /* ATTACH the appropriate report */ if dept='marketing' then put '!EM_ATTACH! mktrept.txt'; else put '!EM_ATTACH! devrept.txt'; /* Send the message */ put '!EM_SEND!'; /* Clear the message attributes */ put '!EM_NEWMSG!'; return; /* Abort the message before the */ /* RUN statement causes it to */ /* be sent again. */ lastobs: put '!EM_ABORT!'; datalines; Susan marketing Jim marketing Rita development Herb development ; run;
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|
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, and so on. */ 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_ATTACH! '||attach); rc = fwrite(fid); fputrc5 = fput(fid,'!EM_SUBJECT! '||subject); rc = fwrite(fid); closerc = fclose(fid); end; end; return; cancel: call execcmd('end'); return;