/*
Send email through
SAS
Dr. Songlin
Zhu Start:
2/24/06
To enable SAS to email, insert the code below into your
SAS config file, which is usually
in
c:\sas8.2\sasv8.cfg or
C:\SAS9.1\nls\en\SASV9.CFG
-EMAILSYS SMTP
-EMAILDLG
NATIVE
-EMAILHOST
'AAA.AAA.AAA'
-EMAILPORT
25
-EMAILID
'BBB@BBB.BBB.BBB'
Insert this code just after the "Set initial values for
symbol tables"
section.
Change the email id in the last line to your email
id.
CCCC - email server type: example:
SMTP
AAA.AAA.AAA - Email server host
name
BBB@BBB.BBB.BBB - Email address in the
server
Example:
%let attach_file1 =
c:\temp\at_1.doc;
%let attach_file2 =
c:\temp\at_2.doc;
%let data_name =
email_contents;
%let var_str =
str_;
data
&data_name.;
infile
cards linesize=150 pad
TRUNCOVER;
line_length=150;
input
&var_str. $varying200.
line_length;
datalines;
Hi,
This is an
example to send email through
SAS.
If you
have any question, let me
know.
XXXXXXX
;
run;
%include
"C:\_tools\send_email.pgm";
%send_email(to
= XXXXX@XXXX.XXX
XXX@XXXX.XXX.XXX.XXX,
subject
= Counts of
forms,
from
=
XXX@XXXX.XXX.XXX.XXX,
cc
=
,
attach
= &attach_file1.
&attach_file2.,
email_contents_data =
&data_name.,
contents_var_name =
&var_str.
);
----------------------------------------------------------------------------------------------------------
%send_email( ... ) can be written as
follows:
%send_email(to =
XXXXX@XXXX.XXX
XXX@XXXX.XXX.XXX.XXX,
subject = Counts of
forms,
from =
XXX@XXXX.XXX.XXX.XXX,
cc
=
,
email_contents_data =
&data_name.,
contents_var_name =
&var_str.
);
%send_email(to =
XXXXX@XXXX.XXX,
subject = Counts of
forms,
from =
XXX@XXXX.XXX.XXX.XXX,
cc
= XXXXX@XXXXX.XXX.XXX
XXX@XXXX.XXX.XXX.XXX,
attach
= &attach_file1.
&attach_file2.,
email_contents_data =
&data_name.,
contents_var_name =
&var_str.
);
*/
%macro send_email(to = ,
subject
= ,
from
= ,
cc
= ,
attach
= ,
email_contents_data = ,
contents_var_name =
);
%macro exist_attach(str=, output_m_name=);
%let &output_m_name. = ;
%if %qcmpres(&str.) ne %str() %then
%do;
%let str_1 = %qscan(&str,1,%str( ));
%let ii_ = 1;
%if %qcmpres(&str_1.) ne %str() %then
%do;
%do %while (%qcmpres(&str_1.) ne %str());
filename fref1 "&str_1.";
%let fileex_1 = %sysfunc(fexist(fref1));
%if &fileex_1. ne 1 %then
%do;
%let &output_m_name. = &&&output_m_name..
%str("&str_1.");
%end;
%let ii_ = %eval(&ii_. + 1);
%let str_1 = %qscan(&str,&ii_.,%str( ));
%end;
%end;
%end;
%mend exist_attach;
%macro separate_(str=, output_m_name=);
%let &output_m_name. = ;
%let str_1 = %qscan(&str,1,%str( ));
%let ii_ = 1;
%if %qcmpres(&str_1.) ne %str() %then
%do;
%do %while (%qcmpres(&str_1.) ne %str());
%let &output_m_name. = &&&output_m_name..
%str("&str_1.");
%let ii_ = %eval(&ii_. + 1);
%let str_1 = %qscan(&str,&ii_.,%str( ));
%end;
%let
&output_m_name. = %str((&&&output_m_name..));
%end;
%mend separate_;
%let exist_file = ;
%exist_attach(str=&attach., output_m_name=exist_file);
%put >> &exist_file.;
%if %qcmpres(&exist_file.) = %str() %then
%do;
%let to_to = ;
%separate_(str=&to., output_m_name=to_to);
%let cc_to = ;
%separate_(str=&cc., output_m_name=cc_to);
%let attach_file = ;
%separate_(str=&attach., output_m_name=attach_file);
%let to_to = %trim(%left(&to_to.));
%let cc_to = %trim(%left(&cc_to.));
%let attach_file = %trim(%left(&attach_file.));
%let subject_ = %trim(%left(&subject.));
%let from_ = %trim(%left(&from.));
%if %qcmpres(&cc_to.) = %str() %then
%do;
%if %qcmpres(&attach_file.) = %str() %then
%do;
filename mail email &to_to.
subject="&subject_."
from="&from_."
;
%end;
%else
%do;
filename mail email &to_to.
subject="&subject_."
from="&from_."
attach = &attach_file.
;
%end;
%end;
%else
%do;
%if %qcmpres(&attach_file.) = %str() %then
%do;
filename mail email &to_to.
subject="&subject_."
from="&from_."
cc = &cc_to.
;
%end;
%else
%do;
filename mail email &to_to.
subject="&subject_."
from="&from_."
cc = &cc_to.
attach = &attach_file.
;
%end;
filename mail email &to_to.
subject = "&subject_."
from = "&from_."
cc = &cc_to.
attach = &attach_file.
;
%end;
%let count_ = 0;
data _null_;
set &email_contents_data.;
call symput("str_"||compress(put(_n_,best.)), 'put "' ||
trim(left(&contents_var_name.)) || '"');
call symput("count_",put(_n_,best.));
run;
%put >> &count_.;
%macro ttt_1;
%do ii_ = 1 %to &count_;
&&&&str_&ii_..;
%end;
%mend ttt_1;
data _null_;
file mail;
%ttt_1;
run;
%end;
%else
%do;
%put Email did not
send out because file(s) &exist_file. does(do) not exist. Please
check path and name and send email again.;
%end;
%mend send_email;
To
enable SAS to email, insert the code below into your SAS config file,
which is usually
in
c:\sas8.2\sasv8.cfg or
C:\SAS9.1\nls\en\SASV9.CFG
-EMAILSYS SMTP
-EMAILDLG
NATIVE
-EMAILHOST
'AAA.AAA.AAA'
-EMAILPORT
25
-EMAILID 'BBB@BBB.BBB.BBB'
SMTP: e-mail interface to use.
NATIVE: Use the native e-mail dialog provided by
your e-mail application
AAA.AAA.AAA: SMTP server that
supports e-mail access for your site
25: Port to which the SMTP server is attached
BBB@BBB.BBB.BBB: Identity of the individual sending e-mail from
within SAS
If you have any questions, feel free to send me feedback.
|