In my last blog post, I discussed how to use SAS options to send unattended email using an SMTP server. In this segment, I’ll tackle creating “pretty” email (using HTML) and sending out emails with attachments. First, I produced a basic report using ODS. I liked the look, and set out to replicate it in the body of my email.
Building on the basic email code we used last time, I added adding the report code and ODS HTML statements. Here is the code:
/** Set system email options **/ options emailsys=SMTP EMAILID="SASJedi@example.com" EMAILHOST="smtp.mydomain.net" EMAILPORT=25; /** Put message, summary and detail all results in the email body **/ filename mymail email "SASJedi@example.com" subject="Report for sashelp.class" lrecl=256 type="TEXT/HTML"; ods _all_ close; ods html body=mymail style=sasweb; options nocenter; /** Start writing the email in a DATA _NULL_ step **/ data _null_; file print; put "Here is the information you asked for."; put "Stay SASy!"; put "Mark"; run; /** Insert query results **/ PROC SQL; title "Stats for sashelp.class where age > 10"; SELECT COUNT(*) 'Head Count' , Avg(age) 'Average Age' format=5.1 , Avg(height) 'Average Height' format=5.1 , Avg(weight) 'Average Weight' format=5.1 from sashelp.class where age > 10 ; quit; proc sql; title "Details from sashelp.class where age > 10"; SELECT from sashelp.class where age > 10; quit; ods _all_ close; ods listing;
And, to my surprise, the email body looked different from the report:
Well, I didn’t care for the look of the email, and as I rummaged through the support.sas.com site I found a suggestion to use HTML3 instead of the default HTML destination (which produces HTML4 markup), due to a change in the way the MS Outlook renders Cascading Style Sheets. So I modified the program to use HTML3, and the results were just as I wanted.
Next I tried just writing the note and summary to the email body, with the detail report attached as a PDF file. This video shows how:
As usual, the SAS code I used in the video is included at the bottom of this post.
Until next time, may the SAS be with you!