[SASTechies] Sending email from SAS...

383 views
Skip to first unread message

LearnSAS

unread,
Feb 12, 2010, 11:19:09 AM2/12/10
to saste...@googlegroups.com
FILENAME Statement EMAIL (SMTP) Access Method allows you to send electronic mail programmatically from SAS using the SMTP (Simple Mail Transfer Protocol) e-mail interface available at your site.

But before you process the email code below check the values for the system options using Proc options for EMAILAUTHPROTOCOL, EMAILHOST, EMAILPORT, EMAILID, EMAILPW for your site…They have to have appropriate values for your code to work. Read more about them @ System Options That Control SMTP E-Mail

proc options group=email; run;

The Log generated...

59   proc options group=email; run;

    SAS (r) Proprietary Software Release 9.1  TS1M3

 EMAILAUTHPROTOCOL=LOGIN
                   Identifies the SMTP e-mail authentication protocol
 EMAILHOST=xxx.xx.xx.xxx
                   SMTP server host for email access method
 EMAILID=xxxxxx    From E-mail address, log in id, or profile for use with underlying e-mail
                   system
 EMAILPORT=25      Port number for SMTP server for email access method
 EMAILPW=xxxxxxxx  Used by the E-mail Access Method and Send menu item to set the email session
                   login password for the underlying e-mail system
 EMAILDLG=native   Used by Send menu item to set the email dialog interface.
 EMAILSYS=smtp     Used by E-mail Access Method and Send menu item to set the interface type with
                   underlying e-mail system.


Try the following email example for sending an email using SAS data step…replace emails and the attachments as you wish…This example has the most common options that you might use....Please look at the SAS examples in the References (5-7) below for some more advanced methods....


filename outbox email "sastech...@gmail.com";

data _null_;   
   file outbox   
      to=("sastech...@gmail.com" "in...@sastechies.com")   
         /* Overrides value in filename statement */   
      cc=("in...@sastechies.com" "som...@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 'It worked great!';
run;



Here is another way of sending emails using SAS X command in a Unix Environment that has the mailx utility…Sometimes it might just better to use the native operating system utilities rather than using SAS Filename Email Statement...

 Here's a macro that does that for you...

%macro SendEmail;

/*Write the contents to a file */
 data _null_;
   file "&emailfile" lrecl=256;
   %emailbody;
 run;
       
   /* %put to=&to cc=&cc subject=&subj attach="&attach"; */
  
   /*use the X command and invoke the Unix mailx command */
  
   X "(cat &emailfile;) | mailx -s ""&subj"" &to –c &cc";
   X " if [[ $? -ne 0 ]] then echo `date`" "Failed to send email to &to" "else rm ~/&emailfile fi";

%mend SendEmail;

%let subj=Hey SASTechies;
%let attach=;
%let cc=;
%let emailfile=~/email.dat; /* path to the temporary file at home directory (ie. ~) */

%macro emailbody;
  PUT "This is a test message from SASTechies";
  PUT "This test mail has been generated from SAS Data Step using Native Unix Mail command";
%mend emailbody;

%SendEmail;


A brief explanation here…Enter your email/attachment/subject in the macro variables…the &emailfile  is a temporary file that SAS writes to compile the email body…This is later deleted if SAS was successful in sending the email…

X "(cat &emailfile;) | mailx -s ""&subj"" &to –c &cc";

The unix command cat &emailfile writes the body for the mailx unix command that takes –s option for subject followed by the to and cc options. Check the screenshot below for more options...



Other References



--
Posted By LearnSAS to SASTechies at 2/12/2010 11:19:00 AM

Deepti

unread,
Feb 12, 2010, 12:25:29 PM2/12/10
to saste...@googlegroups.com
I have been using this alot since I am working on mainframe SAS. But when I use remote connection from PC SAS, I can't use the filename statement to send the email. I found out the reason. It's because Port 25 is not open. Is there any other way to send the email from PC SAS remote connection ?  
--
Deepti Naik.

Sharad

unread,
Feb 12, 2010, 12:58:34 PM2/12/10
to saste...@googlegroups.com
Hey Deepti,

Long time no c...
Anywayz to answer ur question...if u r using PC remote then the email takes the MF's parameters...try to set emailhost=MF ipaddress...then it shouldn't b a problem.. 

Thanks,

Deepti

unread,
Feb 12, 2010, 1:03:25 PM2/12/10
to saste...@googlegroups.com
Thanks Sharad, I will try that and let you know.
 
Regarding long time ...I think my son Aadi is very handful :)

--
Deepti Naik.

Reply all
Reply to author
Forward
0 new messages