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!
4 Comments
Just to let you know, the technology used to produce the example was:
- SAS 9.2 on Windows (tested in both 32 bit and 64 bit).
- The email was read and rendered OK using
- Outlook 2010 (Microsoft Exchange back end)
- Android Mail app on HTC Inspire
- Gmail web app
Possible SAS code issues:
The most common SAS coding error with HTML formatted email which causes the effect you are describing is to leave off the TYPE= option in the FILENAME statement. TYPE must be set to "TEXT/HTML" or the email does not display properly HTML in the email client.
An example of correct FILENAME syntax is included here:
filename mymail email
"MyRecipient@example.com"
subject="Report for sashelp.class"
lrecl=256 type="TEXT/HTML";
The next most common problem it to use an ODS HTML statement instead of ODS HTML3 in SAS9. The results won't display properly in Outlook if you don't specify HTML3.
Let me know if this information was enough to get you rolling, or if you are still having difficulty with your code.
Regards,
Mark
no luck for me either :(
i've done everything above and i got plain text :(
maybe the fact that my outlook uses exchange server instead of smtp causes the problem :(
regards
henryk
This post was written about 7 years ago, and email clients such as Outlook have changed significantly since then. Email generated with the ODS HTML destination doesn't render, and some HTML3 tags are improperly rendered as text. The HTML5 ODS destination is the most successfully rendered by both Outlook 2016 and Gmail, so that's the destination I would recommend for now. Otherwise, the code works as-is (as long as you properly authenticate to your SMTP server).
May the SAS be with you!
Mark
ITs not working for me. In the mail I see only html texts. HTML is not redereed as HTML instead all the html codes are displayed in the body of the email. I used the exact code and tried it. Please let me know if any other settings required.