An email message is a great way to send a notification when a SAS job completes, or to distribute a result from SAS as an attached report or spreadsheet. Watch this Ask the Expert session to learn how the SAS programming language allows you to send email as an output via the FILENAME EMAIL method.
You will learn how to:
The code and techniques from the webinar are available in this article:How to send email using SAS
The questions from the Q&A segment held at the end of the webinar are listed below and the slides from the webinar are attached.
How do you secure an attachment with Gmail?
I don’t know if there are any particular nuances for attachments with Gmail versus anything else. As long as you manage the right SMTP options to use Gmail as a sender, then attachments would work the same way as they did in other email providers. Gmail is designed to be difficult to automate email as you from a high-powered server like Google. You can imagine how much opportunity there would be for spam if people could just automate sending emails all the time. Many of us get a lot spam as it is. So, lots of rules are in place for email deliverability by design -- related to making sure that the sender is actually authorized to send from the domain that they say they are sending from. All those conditions need to be met in order for any sender or email server to actually deliver the mail that you generate from a system. And, if it’s in place, then all the features should be supported. But, just keep in mind that there’s a number of conditions that need to be met in order for it to work.
How can I add a password to the XLSX attachment that is mailed?
When SAS generates an XLSX file, there is not an ability to add a password to it from SAS. None of the export or libname methods for generating that XLSX file will support the idea of adding a password to it. That’s something that you would need to do using a different tool. It could be potentially automated from SAS if you use SAS to automate a tool that takes an XLSX file and adds a password to it. That would be external to your SAS process. But once you have that password in place on the XLSX file, you could still then attach it to the email. Just that step of adding the password would have to happen using a tool different than SAS.
Can you use SAS Enterprise Guide to send email messages?
Yes, you can use any of the programming methods I showed you to send email using SAS Enterprise Guide. And that will use SMTP from the SAS session to generate the email and send it. Enterprise Guide also has some built-in share methods for email. You can share a piece of content from the Enterprise Guide project via email either as a step in your project or just a one-off. In that case, it’s not SAS sending email, it’s SAS Enterprise Guide as your email client. And you have to set those SMTP email options within your Enterprise Guide options. Under Tools -> Options, there’s a section down there for those email settings and you would have to fill in those settings which are basically all the same ones: SMTP server, the port, password, other security type things. Once those are in place, then Enterprise Guide can send email as well on your behalf.
Does this work the same with CAS?
I don’t think there’s an email component to CAS per se. But, all the code I showed you here would work in SAS Viya the same way. So, it doesn’t really have to do with CAS which is the in-memory analytics server part of Viya, but the SAS code would work in SAS Viya just the same way. If you were doing some things in CAS and you ended up with some output that you wanted to share using email, the same code would work.
Can we use SAS to make phone calls?
Probably not. In practice, it’s probably not the tool you would pick to make phone calls. But, that would be fun to get a call from my DATA step!
When there is, for example, an insufficient workspace issue and program terminates, how do we catch this error and send an email?
In order to catch that, you would have to have some other process kind of monitoring things or is set to run to make sure that it achieves its desired end. Or, you might have a script file that launches the SAS job and if the SAS job exits, that script file could trigger something else that happens based on the return code.
By the way, it is super convenient to use SAS to send email in this way, but it is just leveraging a mechanism that is present in lots of other scripting languages or systems. There’s a sendmail command line app that you could use on most Linux systems, for example. So, you could have a script where you handle conditions like that and send mail using pretty much the same mechanism if you’re not going to rely on it or if you think that there’s a condition where SAS might not be available because your session ended prematurely. So, there are different ways to mitigate that.
Does the image need to exist in the SAS session to be an inline image?
In the example I showed with ODS and HTML5, it is “existing,” but you never see it as an actual image file. In ODS HTML5, it generates an embedded, encoded version of the image inside the HTML. That is one way to do it is using ODS HTML5 to generate something like from ODS Graphics output or a SAS Graph output. If you want to use an image from somewhere else, there’s a couple of different ways. If you want to attach it and make it part of your message, you would need to have it somewhere in the SAS session where you could pull it and it would become part of the message either as an attachment or embedded inline. If you’re building your own HTML or you’ve customized your styles such that you can use a URL reference to an image that exists out there somewhere on the internet such that when a person opens up their email and sees the “yes, it’s OK to show images from this sender” and then all of a sudden those images render, a lot of the times, those images are actually coming from a web server somewhere else.
For example, that’s how it works when you receive any of our newsletters from SAS. If you subscribe to the SAS Tech Report or the SUGA Download or the SAS Learning Report, the images that you see in there are actually on the SAS website and they are pulled in when your email is rendered for you. That can make a smaller payload in your email, but it does rely on that connection and that ability to reach out to those web servers to render the images
What's the maximum file size this can take?
SAS doesn’t have a maximum size, but your email system quite possibly would. And then there’s also things like, what size email is polite to send? So, in general, if an attachment is going to be more than a couple of MB, it’s kind of a big attachment and probably exceptional that you’d want to be sending that around in email. If you are looking to economize on the size, I would look at the file format that you’re using to attach, a more compact or efficient format, doesn’t need to be attached, those kinds of things. But, in general, if there’s a limit it’s not going to be on the SAS end of things, but it could be imposed by your SMTP server or by any of the waypoints along the way that are going to at balk at very large attachment.
Is SAS capable of setting column widths in a CSV file prior to emailing?
There is no information in that file inherently that says the width of a column. It’s just the data is the data. So, no, there is not that ability. You could send any kind of file format though. If you had column-organized data with specified widths, tab delimited or an Excel file. Each of those have implied widths for the data you’re sending, but with CSV, the data is the data and it’s as long as it is. But, as long as you create your CSV file in a way that all the data is there, however you are outputting that, where it’s PROC EXPORT or using DATA step to create a CSV file, whatever it is, you have lots of controls on the CSV output works.
Is Windows Task Scheduler the only method to schedule jobs?
No. On Windows, it’s typically the thing that’s used, so if you were using your desktop as the hub for how you’re scheduling things, the Windows Scheduler is the thing that’s built-in to do it. All the jobs that I run, I’m running on Linux. I have a SAS session available there that I can run in batch and I use cron as the method to schedule those jobs, it’s pretty standard. And then, there are other kinds of scheduling tools that can be used both on Windows and on Linux, I think. But, cron is very standard on Linux or Unix. On Windows, Task Scheduler is the one that’s built-in, but there are third-party tools that can also do it. I know one of the tensions is that sometimes end users at their desktop don’t have the ability to schedule jobs on the desktop. They don’t have the permissions or the privileges. So, that can be tricky to navigate and that’s kind of a negotiation with your IT support to figure out a solution for that. SAS Enterprise Guide, when you automate a job, by default it will put it in the Task Scheduler, but it isn’t the only way you can do it. It’s just the built-in way, so that’s what SAS Enterprise Guide does. But, you can take that same script that Enterprise Guide generates for you and schedule it or run it manually a different way if you want.
Is there a way to add CC or BCC into the emails using SAS?
Yes, all of those are supported, the standard email fields. There’s a CC= option and a BCC= option as well as a FROM option and a REPLYTO option because sometimes the REPLYTO can be different than the FROM.
Can you explain the option EMAILDLG? Do you recommend NATIVE?
I can explain and no, I don’t recommend it. This one is particular to SAS on Windows, I think, and if you are using SAS to send email there and you’re probably using the MAPI that’s default on Windows, what it’s going to want to do is it’s going to automate your local email client, often it’s Outlook, on your local machine. And, there are usually protections in place that prevent that from working. Again, going back to the spam prevention options, Microsoft Windows have built in securities to prevent a rogue application on your local PC from firing up your email and sending a bunch of email on your behalf. If you were relying on that NATIVE client, then, of course, that client would need to be there and need to be always on, and you’re going to have to go through and kind of disable or take down the different sections that currently prevent it from working the way you want it to work. So, that’s why I recommend SMTP, because that’s sort of like the headless way to do it. It doesn’t bring up user interface, it doesn’t rely on a local install client. Instead, it packages up that email and sends it directly to your SMTP server for distribution.
When using <> to add an alias to the supplied email address, is it possible to include a comma: <last name, first name>?
I haven’t tried it, but I think so. I think what appears in that angle bracket is basically just what would appear in the FROM field that you would see in most email clients. The FROM field is usually the “friendly name” and the address is the other one that’s inside the brackets however that is formatted. So, I think commas would be allowed, but I haven’t tried it. I don’t know if there are any particular special thing that we would need to do in SAS to make it work, but I think because it’s usually a quoted field, a comma would not trigger a problem for syntax.
I hand off the last step of email automation for some emails to a macro in Outlook so I can 'press the button' myself to send. Is that a capability that SAS can handle without the handoff?
If SAS is running on the same machine as your Outlook, so everything’s on Windows in this case. The way you would do that is you would have to kick off another process like a script process that would run the script that is going to trigger that Outlook automation. So, I’m going to say it’s possible to do. It’s not something I’ve ever looked at. But, to the extent that you can automate any Office client to run a script, you can definitely kick that off from SAS as an external process. You would use X command or a statement or SYSTASK function or something like that to spawn that process.
Where can we find the EMAILPORT value?
The default value is 25 for many SMTP servers. However, if relying on your internal SMTP instance it's something your IT support might change. You need to check with your IT experts. If using a public service like Gmail, the port is different -- and documented in the notes for that service.
For a system using MAPI, can we still send emails by SAS as instructed?
When using MAPI (on Windows), SAS will automate your local email client to send the messages. It's possible to use but it's not as automated for "unattended" programs that send email. SMTP is a much better protocol for email automation.
Can you include graphical output embedded in the body of your email? If you have 3 excel exports within the same job, how would you send 1 email with 1 Excel file with all 3 of those exports in as their own tabs? Essentially, 1 email, 1 Excel with 3 tabs.
For this style of output I'd look at ODS EXCEL to create the XLSX file that contains your report content, including graphs. Then use the ATTACH= option to attach that Excel file to the email message.
How can we attach a log file from the output of the SAS Code that ran in SAS Viya 3.5?
You can use PROC PRINTO to redirect log output to a specific file, then use FILENAME EMAIL with ATTACH= to send that log file as email to any desired recipient.
How do you bypass the Microsoft Outlook pop-up that states, " A program is trying to send an email message on your behalf. If this is unexpected, click Deny and verify your antivirus software is up-to-date"? I have been unable to get the SAS output sent to email without clicking on "allow" from the pop-up box.
You'll see this when using EMAILSYS=MAPI, which is the default on Windows. Change to EMAILSYS=SMTP and you'll be able to bypass this.
[off topic] Is the "PROC S3" a separate license?
No, PROC S3 part of Base SAS. But of course you'll need all of your AWS connection info and potentially additional setup with security certificates to connect.
Is there any way to pull information from Microsoft Outlook Inbox into SAS?
You can use SAS to call Microsoft APIs that access data in Microsoft 365, including email. This requires partnership with your IT support to establish the permissions to do this. Not gonna lie though -- it can get complicated.
Can a Windows EG scheduler write the script for Linux?
The "Schedule Project" function in EG will create a VB Script that runs on Windows, so no...it's not a native script for Linux. Your local VB Script can automate EG to do any SAS work including running projects and programs that connect to your Linux SAS, but that still uses your Windows PC as the conduit. See Doing More with SAS Enterprise Guide Automation.
FILENAME Statement: EMAIL (SMTP) Access Method Documentation
Hi Chris, I'm having trouble sending an email with HTML5.
The output on my computer looks like this:
However, the email I get looks like this:
Here is my code:
%macro email; data _null_; call symputx("today",put(today(),mmddyy10.)); run; options EMAILSYS=SMTP EMAILID="keegan.xxxxx@xxxxx.com" EMAILHOST="GESVR5050.xxxxx.xxxxx.com"; FILENAME output EMAIL SUBJECT="Churn by Week as of &today." FROM="keegan.xxxxx@xxxxx.com" TO=("keegan.xxxxx@xxxxx.com") BC=("keegan.xxxxx@xxxxx.com") type='text/html' CT ='text/html'; ods html5 body=OUTPUT style=htmlblue; ods escapechar='^'; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Good morning,"; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}See counts below and let me know if you have questions."; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Thanks,"; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Keegan"; ods graphics/width=1000px height=250px ; proc sgplot data=term_plot noautolegend; title 'Term Attrition'; heatmapparm y=channel_subchannel x=week_starting1 colorresponse=churn / colormodel=(green yellow red) outline; text y=channel_subchannel x=week_starting1 text=churn / textattrs=(size=11pt); yaxis label="Channel"; xaxis label="Week Beginning"; run; ods html5 close; %mend; %email;
Anything you suggest? I appreciate your help!
@Keegan See the tips in this article about sending email with an embedded image. You will need to set the image type in ODS GRAPHICS (usually png is good), and the options(bitmap_mode="inline") setting on your ODS HTML statement.
Thank you Chris!
I'm having trouble figuring out where the new options(bitmap_mod="inline") should be located.
Here is my current code:
%macro email; data _null_; call symputx("today",put(today(),mmddyy10.)); run; options EMAILSYS=SMTP EMAILID="keegan.xxxx@xxxx.com" EMAILHOST="GESVR5050.gexa.gexaenergy.com"; FILENAME output EMAIL SUBJECT="Churn by Week as of &today." FROM="keegan.xxxx@xxxx.com" TO=("keegan.xxxx@xxxx.com") BC=("keegan.xxxx@xxxx.com") type='text/html' CT ='text/html'; ods html5 (id=mail) body=OUTPUT style=htmlblue; ods escapechar='^'; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Good morning,"; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}See counts below and let me know if you have questions."; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black} "; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Thanks,"; ODS TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Keegan"; ods graphics/ imagefmt=png width=1000px height=250px options(bitmap_mode="inline") gtitle; proc sgplot data=term_plot noautolegend; title 'Term Attrition'; heatmapparm y=channel_subchannel x=week_starting1 colorresponse=churn / colormodel=(green yellow red) outline; text y=channel_subchannel x=week_starting1 text=churn / textattrs=(size=11pt); yaxis label="Channel"; xaxis label="Week Beginning"; run; ods html5 (id=mail) close; %mend; %email;
I get this error:
7 TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Thanks,"; ODS
7 ! TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Keegan"; ods
7 ! graphics/width=1000px height=250px options(bitmap_mode="inline")
-------
22
ERROR 22-322: Syntax error, expecting one of the following: ;, ANTIALIAS, ANTIALIASMAX,
ATTRPRIORITY, BORDER, BYLINE, DATASKINMAX, DISCRETEMAX, DRILLTARGET, GROUPMAX,
HEIGHT, IMAGEMAP, IMAGENAME, LABELMAX, LABELPLACEMENT, LEGENDAREAMAX,
LINEPATTERNOBSMAX, LOESSMAXOBS, LOESSOBSMAX, MAXLEGENDAREA, MAXOBS, NBINSMAX,
NOANTIALIAS, NOBORDER, NOIMAGEMAP, NOSCALE, NOSCALEMARKERS, NOSUBPIXEL, NXYBINSMAX,
OBSMAX, OUTPUTFMT, PANELCELLMAX, POP, PUSH, RESET, SCALE, SCALEMARKERS, SHOW,
STACKDEPTHMAX, SUBPIXEL, TIPMAX, TOTALCELLMAX, WIDTH.
NOTE: Line generated by the invoked macro "EMAIL".
7 TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Thanks,"; ODS
7 ! TEXT="^S={font_face='Calibri' font_size=3 fontweight=Medium foreground=black}Keegan"; ods
7 ! graphics/width=1000px height=250px options(bitmap_mode="inline")
-------
202
ERROR 202-322: The option or parameter is not recognized and will be ignored.
Can you show me the syntax for the options within the code?
On your ODS HTML5 statement:
ods html5 (id=mail) body=OUTPUT options(bitmap_mode="inline")
Also add imagefmt = PNG to your ODS GRAPHICS:
ods graphics/width=1000px height=250px imagefmt=png;
That worked perfectly!! Thank you so much Chris! I very much appreciate your help!
Hey - Do we have any limit on emails that we send from each SAS session? Our servers have no limit on number, we have removed that and it can send n number of emails in a day.
But one of our user is trying to send 1500ish emails in a session but unable to send any emails via SAS due to the ‘WARNING: Email service not available’ error.
Any suggestions would be appreciated.
@avinashginjupal It's possible that your email service has a trigger to prevent flooding -- many emails originating from the same sender/client in a short period. There is no inherent limit enforced by SAS.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Ready to level-up your skills? Choose your own adventure.
Your Home for Learning SAS
SAS Academic Software
SAS Learning Report Newsletter
SAS Tech Report Newsletter