SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3

Allows you to send electronic mail programmatically from SAS using the SMTP (Simple Mail Transfer Protocol) e-mail interface

Valid: Anywhere

Category: Data Access

Syntax

FILENAME fileref EMAIL < address >< email-options >;

Arguments

fileref

EMAIL

address

E-mail Options

You can use any of the following email options in the FILENAME statement to specify attributes for the electronic message.

Note: You can also specify these options in the FILE statement. E-mail options that you specify in the FILE statement override any corresponding e-mail options that you specified in the FILENAME statement.

PUT Statement Syntax for EMAIL (SMTP) Access Method

In the 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. For example,

filename mymail email 'martin@site.com' subject='Sending Email'; data _null_; file mymail; put 'Hi'; put 'This message is sent from SAS '; run;

You can also use PUT statements to specify e-mail directives that override the attributes of your message (the e-mail options like TO=, CC=, SUBJECT=, CONTENT_TYPE=, ATTACH=), or to perform actions such as send, abort, or start a new message. Specify only one directive in each PUT statement; each PUT statement can contain only the text that is associated with the directive that it specifies.

The directives that change the attributes of a message are as follows :

Details

Overview You can send electronic mail programmatically from SAS using the EMAIL (SMTP) access method. To send e-mail to an SMTP server, you first specify the SMTP e-mail interface with the EMAILSYS system option, use the FILENAME statement to specify the EMAIL device type, then submit SAS statements in a DATA step or in SCL code. This has several advantages:

In general, DATA step or SCL code that sends e-mail has the following components :

Examples

Example 1: Sending E-mail with an Attachment Using a DATA Step

In order to share a copy of your SAS configuration file with another user, you could send it by submitting the following program. The e-mail options are specified in the FILENAME statement:

filename mymail email "JBrown@site.com" subject="My SAS Configuration File" attach="/u/sas/sasv8.cfg"; data _null_; file mymail; put 'Jim,'; put 'This is my SAS configuration file.'; put 'I think you might like the'; put 'new options I added.'; run;

The following program sends a message and two file attachments to multiple recipients. For this example, the e-mail options are specified in the FILE statement instead of the FILENAME statement.

filename outbox email "ron@acme.com"; data _null_; file outbox to=("ron@acme.com" "humberto@acme.com") /* Overrides value in */ /* filename statement */ cc=("miguel@acme.com" "loren@acme.com") subject="My SAS Output" attach=("C:\sas\results.out" "C:\sas\code.sas") ; put 'Folks,'; put 'Attached is my output from the SAS'; put 'program I ran last night.'; put 'It worked great!'; run;

Example 2: Using Conditional Logic in a DATA Step

You can use conditional logic in a DATA step in order to send multiple messages and control which recipients get which message. For example, in order to send customized reports to members of two different departments, the following program produces an e-mail message and attachments that are dependent on the department to which the recipient belongs. In the program, the following occurs:

  1. In the first PUT statement, the !EM_TO! directive assigns the TO attribute.

  2. The second PUT statement assigns the SUBJECT attribute using the !EM_SUBJECT! directive.

  3. The !EM_SEND! directive sends the message.

  4. The !EM_NEWMSG! directive clears the message attributes, which must be used to clear message attributes between recipients.

  5. The !EM_ABORT! directive aborts the message before the RUN statement causes it to be sent again. The !EM_ABORT! directive prevents the message from being automatically sent at the end of the DATA step.

filename reports email "Jim.Smith@work.com"; data _null_; file reports; length name dept $ 21; input name dept; put '!EM_TO! ' name; put '!EM_SUBJECT! Report for ' dept; put name ,; put 'Here is the latest report for ' dept '.' ; if dept='marketing' then put '!EM_ATTACH! c:\mktrept.txt'; else /* ATTACH the appropriate report */ put '!EM_ATTACH! c:\devrept.txt'; put '!EM_SEND!'; put '!EM_NEWMSG!'; put '!EM_ABORT!'; datalines; Susan marketing Peter marketing Alma development Andre development ; run;

Example 3: Sending Procedure Output in E-mail

You can use e-mail to send procedure output. This example illustrates how to send ODS HTML in the body of an e-mail message. Note that ODS HTML procedure output must be sent with the RECORD_SEPARATOR (RS) option set to NONE.

filename outbox email to='susan@site.com' type='text/html' subject='Temperature Conversions'; data temperatures; do centigrade = 40 to 100 by 10; fahrenheit = centigrade*9/5+32; output; end; run; ods html body=outbox /* Mail it! */ rs=none; title 'Centigrade to Fahrenheit Conversion Table'; proc print; id centigrade; var fahrenheit; run; ods html close;

Example 4: Creating and E-mailing an Image

The following example illustrates how to create a GIF image and send it from SAS in an e-mail message:

filename gsasfile email to='Jim@acme.com' type='image/gif' subject="SAS/GRAPH Output"; goptions dev=gif gsfname=gsasfile; proc gtestit pic=1; run;

See Also

Statements:

Категории