Skip to content

Instantly share code, notes, and snippets.

@Cesar-Urteaga
Last active March 16, 2023 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cesar-Urteaga/89dcac7a55c7fd73ad52ffbb300ee044 to your computer and use it in GitHub Desktop.
Save Cesar-Urteaga/89dcac7a55c7fd73ad52ffbb300ee044 to your computer and use it in GitHub Desktop.
Sends an email from SAS with the possibility of attach files.
/*
In order to send an e-mail, you must have access to an SMTP server. If this
has not happened yet, you can enable the SMTP e-mail interface with the
following statement:
OPTIONS EMAILSYS = SMTP;
On the other hand, so as to specify a single SMPT server:
OPTIONS EMAILHOST = <Here goes the email host>;
You can get the above information along with the email port with the following
statement:
%PUT %SYSFUNC(GETOPTION(EMAILSYS))
%SYSFUNC(GETOPTION(EMAILHOST))
%SYSFUNC(GETOPTION(EMAILPORT));
N.B.: When I tested the macro, I had to set up the EMAILID option once:
OPTIONS EMAILID = <myemailaddress@no-reply.com>;
*/
/*-----------------------------------------------------------------------------|
| Description : Sends an email from SAS with the possibility of attach files. |
| Assumptions : This macro is based on the code described in the paper "SMTP |
| E-Mail Access Method: Hints, Tips, and Tricks" by Chuck Hunley.|
| http://support.sas.com/resources/papers/proceedings10/060-2010.pdf|
| Parameters : From - Email address of the sender. It must be just one |
| address. |
| To - Email address(es) of the primary recipient(s). |
| CC - Email address(es) of the recipient(s) for which |
| you want to send a copy. |
| Subject - Subject of the message. |
| Message - Message's body text. |
| Attachment - File name(s) of the attached file(s). |
| BCC - Email address(es) of the recipient(s) for which |
| you want to send a blind copy (Default: Sender's |
| email address). |
| N.B.: All the parameters must be enclosed in quotation marks. |
| Except for the first parameter, they can be more than |
| one, in which case they must be separated by a blank |
| space (do not use a comma) or a new line (please see the |
| example below). |
| Output : None. Nevertheless, in case of an error, it returns an error |
| message into the log. |
|-----------------------------------------------------------------------------*/
/* Example:
* Creates the macro variable and sample files so as to execute the examples. ;
* Defines the path of the WORK library and defines two email addresses (you
should change them to check that the example works). ;
%LET _MGT_WORK_PATH = %SYSFUNC(GETOPTION(WORK));
%LET _MGT_EMAIL_ADDRESS_01 = fakeemailaddress01@no-reply.com;
%LET _MGT_EMAIL_ADDRESS_02 = fakeemailaddress02@no-reply.com;
* Creates to sample text files that will be attached. ;
X echo Text File 01 > &_MGT_WORK_PATH./TextFile01.txt;
X echo Text File 02 > &_MGT_WORK_PATH./TextFile02.txt;
* Ex_01;
%MSendEmail(From = "&_MGT_EMAIL_ADDRESS_01.",
To = "&_MGT_EMAIL_ADDRESS_01."
"&_MGT_EMAIL_ADDRESS_02.",
CC = "&_MGT_EMAIL_ADDRESS_01." "&_MGT_EMAIL_ADDRESS_02.",
Subject = "Subject's text",
Message = "Email body message",
Attachment = "&_MGT_WORK_PATH./TextFile01.txt"
"&_MGT_WORK_PATH./TextFile02.txt",
BCC = "&_MGT_EMAIL_ADDRESS_01." "&_MGT_EMAIL_ADDRESS_02."
)
* Output: None, but it sends an email to the specified email addresses and
attaches the created sample text files. ;
*/
/*-----------------------------------------------------------------------------|
| Date Author Description |
|------------------------------------------------------------------------------|
| July 25, 2017 Cesar R. Urteaga-Reyesvera Creation. |
|-----------------------------------------------------------------------------*/
%MACRO MSendEmail(From = /* Sender's address. */,
To = /* Recipient's address(es). */,
CC = /* Copied recipient's address(es). */,
Subject = /* Email's subject. */,
Message = /* Message's body text. */,
Attachment = /* Attached file name(s). */,
BCC = &From. /* Blind copied recipient's address(es).*/
);
/*
Specifies the email attributes with a fileref. Please check the following
link to review the available options:
http://support.sas.com/documentation/cdl/en/hostunx/69602/HTML/default/viewer.htm#p1hl3t66coao7bn18vrmhx2gte1q.htm
*/
FILENAME TRAITS
EMAIL
FROM = &From.
TO = (&To.)
CC = (&CC.)
SUBJECT = &Subject.
IMPORTANCE = "HIGH"
%IF &Attachment. ~= %THEN ATTACH = (&Attachment.);
BCC = (&BCC.)
;
* Sends the email with the indicated characteristics. ;
DATA _NULL_;
FILE TRAITS;
PUT &Message.;
RUN;
* Disassociates the created fileref. ;
FILENAME TRAITS CLEAR;
* Sends a message in case of an error. ;
%IF &SYSERR. %THEN %DO;
%PUT ERROR: "Email could not be sent due to a system error";
%END;
%MEND MSendEmail;
@aldermore-stewartjardine

saved me from a google hole I was falling down! thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment