Desktop productivity for business analysts and programmers

sending emails from sas

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

sending emails from sas

Hi,

My mails data set looks as follows

1abc123@some.com
2def@some.com
3ghi@some.com
4jkl@some.com
5abc123@some.com
6def@some.com
7def@some.com

consider abc123@some.com is a wrong email

As of now my code looks as follows;;;

STEP=1: Sorting the data set.

--------------------------------------------------------------------------------------------------------------------

PROC SORT DATA=mails;

BY mail_id;

RUN;

-----------------------------------------------------------------------------------------------------------------------------------

STEP=2 : Sending mails for every three hundred users after holding the triggering mails process for 2 mnts..

---------------------------------------------------------------------------------------------------------------------------------------


%macro send_email;

filename hm_mail email lrecl=32000 type='text/html';


data _null_;   
set mails END=EOF;
by mail_id;
file hm_mail;


retain numofusers 0;

if first.mail_id then do; 

     numofusers=numofusers+1;
  If mod(numofusers,300) = 0 then Timer=Sleep(120,1);

  put '!EM_TO!' mail_id;
  put '!EM_SUBJECT!' 'Test Plans have been created.';
  put 'The following test plans have been created with you as a tester.';
  put '<br>';
end;

id_link = '<a href="' ||left(strip(id)) || '">' || left(strip(id)) || '</a>' ;
put '<br>';
put id_link;
put '<br>';

if last.mail_id then do;
  put '<br>';
  put 'This is a system-generated message. Do not reply to this e-mail.';
  put '!EM_SEND!';
  put '!EM_NEWMSG!';
end;

if EOF then put '!EM_ABORT!';


run;

%mend send_email;
%send_email;

Here I am getting an error message as below when %send_email is excecuted

***********Below is the error message in email logs******************************************************

WARNING: Email: 550 5.1.1 <abc123@some.com Recipient address rejected: User unknown

WARNING: Bad email address: abc123@some.com

ERROR: No email addresses specified.

FATAL: Unrecoverable I/O error detected in the execution of the DATA step program.  Aborted during the EXECUTION phase.

**************************************************************************************************************

After this its not sending emails to the remaining users..

Could you please suggest me how to add an exception to handle this error at the time of exection..

Many thanks in advance..


Accepted Solutions
Solution
‎12-15-2014 08:43 AM
Esteemed Advisor
Posts: 6,636

Re: sending emails from sas

Modify your macro so that it sends a single mail:

%macro send_email(recipient);

.....

set mails (where=(mail_id="&recipient")) end=EOF;

(use if _n_ = 1 instead of if first.mail_id)

......

run;

%let syscc=0;

options obs=max;

%mend;

Then do;

proc sort data=mails (keep=mail_id) out=mails_help nodupkey;

by mail_id;

run;

data _null_;

set mails_help;

comstr = '%send_email('!!trim(mail_id)!!');';

call execute(comstr);

run;

(untested, so you may have to do some corrections)

The %let syscc=0; and options obs=max; reset any condition caused by a data step error, may be unnecessary.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Esteemed Advisor
Posts: 6,636

Re: sending emails from sas

Unless you want to split the process so that every mail address is handled in a single data step, you will have to weed out the invalid mail ID's. The I/O error caused by the faulty email address stops the data step for good.

With separate data steps, you could reset %let syscc=0; and options obs=max; in between.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 36

Re: sending emails from sas

Thanks for your reply.

We are unable to find the invalid email ids before sending the emails. While execution time only we are coming to know that the mail id is incorrect and it is causing to terminate the data step.

As you replied, Could you please write the code by including

%let syscc=0; and options obs=max;

Solution
‎12-15-2014 08:43 AM
Esteemed Advisor
Posts: 6,636

Re: sending emails from sas

Modify your macro so that it sends a single mail:

%macro send_email(recipient);

.....

set mails (where=(mail_id="&recipient")) end=EOF;

(use if _n_ = 1 instead of if first.mail_id)

......

run;

%let syscc=0;

options obs=max;

%mend;

Then do;

proc sort data=mails (keep=mail_id) out=mails_help nodupkey;

by mail_id;

run;

data _null_;

set mails_help;

comstr = '%send_email('!!trim(mail_id)!!');';

call execute(comstr);

run;

(untested, so you may have to do some corrections)

The %let syscc=0; and options obs=max; reset any condition caused by a data step error, may be unnecessary.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 36

Re: sending emails from sas

Thanks Smiley Happy

Contributor
Posts: 25

Re: sending emails from sas

I don't think it is the Bad or unknown email address that is causing your program to terminate the data step - these are only Warnings.  The real problem is indicated by "ERROR: No email addresses specified" - i.e. the email process thinks it doesn't have an email address to process, not just a bad email address.

I think this may be caused by:

if last.mail_id then do;

  ...

  put '!EM_NEWMSG!';

end;

which I think would initiate an email message without an EM_TO when the final row in the mails dataset is reached.

I've often had Bad or unknown email addresses, and they've never caused my SAS programs to crash.  But, in my case, the email process has always been asynchronous - usually through SMTP.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 1109 views
  • 0 likes
  • 3 in conversation