How to use Gmail to send a message from a SAS program

33

Previously I've described how you can use SAS Enterprise Guide to send an e-mail message using Gmail as your e-mail provider.

In the article, I mentioned that you can also write SAS programs that send e-mail messages, but at the time you could not reach Gmail using the FILENAME EMAIL method. SAS did not support the security protocols (SSL) that are necessary to use the Gmail SMTP servers.

Beginning with SAS 9.4, FILENAME EMAIL supports the SSL and STARTTLS options, which you can use SAS to communicate directly with providers such as Gmail and Outlook.com.

Note that Google requires you to allow "Less secure apps" in order to send e-mail from a non-Google application. To remedy, you first have to enable Less secure apps from your Google account: security caveats apply. Then, you might have more success by using port 587 and the STARTTLS option (instead of port 485 and SSL, as my original example showed). If you use two-factor authentication on your Google account (a best practice), then you will need to take an additional step and create an "App password" -- which will allow a third-party app to use Gmail on your behalf and bypass the verification step. As always, your Google password and any App password you generate should be managed carefully and secured.

The following example program shows the options you would use for Gmail. Of course, you must specify your own Gmail address for the ID= value. And if you're squeamish about putting your plain-text password in a program, don't forget that you can use PROC PWENCODE to scramble it from prying eyes.

options emailhost=
 (
   "smtp.gmail.com" 
   /* alternate: port=487 SSL */
   port=587 STARTTLS 
   auth=plain 
   /* your Gmail address */
   id="your_address@gmail.com"
   /* optional: encode PW with PROC PWENCODE */
   pw="your_password" 
 )
;
 
filename myemail EMAIL
  to="lucky_recipient@gmail.com" 
  subject="Read SAS blogs";
 
data _null_;
  file myemail;
  put "Dear Friend,";
  put "I recommend that you read https://blogs.sas.com ";
  put "for lots of SAS news and tips.";
run;
 
filename myemail clear;

With SAS for Windows, this program should work without additional configuration. If you run SAS on UNIX or z/OS, you also need to specify the SSLCALISTLOC= system option, which tells SAS where to find the digital certificates that are needed for the SSL connection.

Share

About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies

33 Comments

  1. Hi Chris,

    Thanks for this blog on sending email through SAS EG. I am trying to create some utility program which can send an email from SAS.

    Now, my question is if I want SAS to send email from Yahoo/Gmail, I think we will need SAS/SSL or SAS/SECURE licence. Is that right?

    Since I don't have this licence I am unable to send email with SAS using Yahoo/Gmail but able to do so with MS Outlook.Is there any different way?

    Thanks,
    Saumil

    • Chris Hemedinger
      Chris Hemedinger on

      Saumil,

      You don't need SAS/SECURE (although SAS/SECURE is part of Base SAS in the 9.4 release). However, you do need the SSL libraries. On Windows, this is built into the operating system. On Unix, you have to configure SAS to point to the SSL libraries/certificate you want to use.

      If you have SAS 9.4, you can use the technique that I documented in this post. If you have an earlier release of SAS, you can either use SAS Enterprise Guide to do the work (outside of a SAS program) or you can, as you mention, use the MAPI protocol to use Microsoft Outlook. However, do to security concerns, Microsoft Outlook does complain when another program automates it to send e-mail.

      This paper - Using Mail Functionality in SAS - contains a good summary of the options and limitations before SAS 9.4.

      • Hi Saumil,

        Can you help me with code and setting how to send sas report through outlook.
        ASAP

        Thanks & Regards
        Rohan

        • Chris Hemedinger
          Chris Hemedinger on

          Sending e-mail through Outlook -- automated -- is tricky. This is because the MAPI method (protocol used by Exchange and Outlook) requires automation of Outlook, and today's security measures usually prevent apps from sending automated e-mail in this way -- to protect against spam-producing viruses. It's much better to use SMTP. Your organization almost certainly has an SMTP server; you'll just need to learn the host/port information from your IT team so that you can reference it in code.

  2. Prashant Chegoor on

    Hi Chris,

    How can i send an email to gmail id using SAS University Edition (SUE)? Is this even Possible? I guess we would need to know the value for this option : SSLCALISTLOC= .

    How about sending it to an Outlook id instead of gmail id ? Atleast i guess the SSLCALISTLOC= is not required in this case. I tried sending to an Outlook id that i have and submitted the below code in SUE.

    filename myemail EMAIL
      to="ptest@outlook.com" 
      subject="Read SAS blogs";
     
    data _null_;
      file myemail;
      put "Dear Friend,";
      put "I recommend that you read http://blogs.sas.com ";
      put "for lots of SAS news and tips.";
    run;
     
    filename myemail clear;
    

    It ran without any Errors and also had the below Message in the log .But i never got this message in my Outlook inbox. I guess i am doing something wrong.

     NOTE: The file MYEMAIL is:
           E-Mail Access Device
     
     Message sent
           To:          "ptest@outlook.com"
           Cc:          
           Bcc:         
           Subject:     Read SAS blogs
           Attachments: 
     NOTE: 3 records were written to the file MYEMAIL.
           The minimum record length was 12.
           The maximum record length was 47.
     NOTE: DATA statement used (Total process time):
           real time           0.04 seconds
           cpu time            0.01 seconds
    

    • Chris Hemedinger
      Chris Hemedinger on

      On the SAS University Edition, the EMAILHOST is set to "LOCALHOST" and it should use the built-in SMTP facility of the Linux VM. I have found that the default settings successfully send e-mail within my corporate firewall. However, this doesn't cut it for external providers such as Outlook.com and Gmail which require more authentication to prevent e-mail spoofing. I'm not sure that the SAS University Edition can be made to work with those e-mail providers. Please remember that this is a tool for learning -- you can "practice" sending e-mail programmatically, but you shouldn't expect to be able to establish production-style e-mail notifications.

  3. Shatrughan Saxena on

    Hi Chris,

    I am trying to configure the GMAIL SMTP server inside my SASV9.cfg file to be able to send the email alerts. I tried the program you suggested and it works fine. But I dont want to send emails from SAS Program. Here is what I am writing in my MetadataServer/sasv9.cfg file:

    -emailsys SMTP
    -emailhost smtp.gmail.com SSL
    -emailport 465
    -emailauthprotocol login
    -emailid xxxxx@gmail.com | xxxxx@gmail
    -emailpw xxxxxx

    but it doesnt work and throws an error that SMTP host is not reaceable. Also I am not able to start the metadata server service. Once I remove the SSL from the emailhost option, service starts but still I cannot send email. I even tried the SAS Usage Note and made the following entry in my SASHome/nld/en/sasv9.cfg file but that also doesnt work:

    http://support.sas.com/kb/53/215.html

    -emailhost (“smtp.gmail.com” STARTTLS id=gmail-login-ID pw=gmail-password auth=LOGIN port=587);

    can you please help?

    • Chris Hemedinger
      Chris Hemedinger on

      Shatrughan, it sounds like you've tried a lot of things already. I suggest that you work with SAS Technical Support on this.

  4. Jordan Klein on

    Has anyone seen this error when trying to send using the syntax above?

    "ERROR: Unable to create cipher."

    I'm running SAS 9.4 TS Level 1M2 with only the following option in my sasv9.cfg. Without this option, it tries to use outlook to send and the security warning to allow pops up.

    -EMAILSYS SMTP

    I've tinkered for a few days and have followed a couple of different posts/documentation with no luck. My syntax seems right but it seems to have an issue actually enabling the SSL session. I find this strange since this should be native to SAS 9.4. Any help would be much appreciated.

    • Chris Hemedinger
      Chris Hemedinger on

      Some versions of SSL were put at risk with the POODLE vulnerability. SAS released a SAS Note and some hotfixes to address this. The symptom that you're seeing might be a result of this -- make sure you have the latest fixes.

      I'm assuming that since you mention Outlook, that you're running SAS for Windows. Unix users have to specify additional options for finding the digital certificates.

  5. Jordan Klein on

    Thanks, Chris. I must have been missing one of the SAS/SECURE components in the SAS 9.4 install. Yes, this was windows. Sorry for not specifying that. I ended up ripping out my SAS 9.4 install and re-installing with all of the SAS/SECURE components. It was actually the hotfix install that tipped me off as the fixes for secure weren't even showing up in deployment manager for that container. So if anyone else gets this fun "unable to create cipher" error, you likely skipped some optional SECURE components that you didn't think you needed before you needed to use SSL for email (at least that was my case).

    • Chris Hemedinger
      Chris Hemedinger on

      Glad that you got this resolved. Since SAS/SECURE is bundled with Base SAS in 9.4, there really isn't any reason to not install it. So many interactions now require SSL or TLS at the endpoint, having these components is a must.

  6. Pierre Dupuis on

    Hi Chris, I am using TLS to send secure email over SMTP and I am wondering if Windows Authentication is supported so users don't need to put password in the SAS program? Thanks.

    • Chris Hemedinger
      Chris Hemedinger on

      Pierre, I'm not sure of the answer here. I suggest tracking this question with SAS Tech Support.

  7. Now that SAS can communicate with the gmail server, Is it possible to import message text and header info (sender, timestamp, etc) directly to a SAS dataset?

    • Chris Hemedinger
      Chris Hemedinger on

      Hi Emily,

      The technique I shared is for sending e-mail via SMTP, which is the standard protocol for sending e-mail. To receive/process e-mail, you would need to fetch the messages with a different method: using POP3 or perhaps a GMail API. That would be much more complex and while it might be possible with SAS, I haven't experimented with it.

      GMail does have an API that allows 3rd party apps to access your e-mail messages. You would essentially need to register such an app (as a developer) and then write code that authenticates and pulls the e-mail messages. PROC HTTP is pretty robust and might support this, but there would be lots of handshaking required.

  8. Hi Chris,

    Thanks for the post. I have a couple of questions.
    1) Even after using the emailhost and a gmail id and password, I am receiving the text from outlook. I haven't done anything different from the example that you have given
    2) Would the gmail's 2 step authentication be a hindrance in this process?

    Thanks,
    Sid

    • Chris Hemedinger
      Chris Hemedinger on

      Sid, did you change -EMAILSYS to SMTP? That's important, otherwise the default might be MAPI, which drives e-mail to Outlook.

      • Thanks Chris. Is it sasv9.cfg in the "SASHome\SASFoundation\9.4" location? I don't think I can change this file (permission denied). Is there a work around?

  9. Pingback: How to send a text message with SAS - The SAS Dummy

  10. Dear Chris,
    I followed as your instruction. And i have got "ERROR: Unable to create cipher."
    As my EGuide version is 71.2

    options
    emailsys="smtp"
    EMAILHOST="smtp.gmail.com"
    EMAILPORT=25
    EMAILID="xxxx@gmail.com"
    EMAILAUTHPROTOCOL=LOGIN
    EMAILPW="xxxx"
    ;

    options emailhost=
    (
    "smtp.gmail.com"
    port=587
    STARTTLS auth=plain
    id="xxxx@gmail.com"
    pw="xxxx"
    )
    ;

    filename mymail email
    TO="xxxx@gmail.com"
    subject="TEST Email" ;

    data _null_;
    file mymail;
    put "Dear Friend,";
    put "I recommend that you read http://blogs.sas.com ";
    put "for lots of SAS news and tips.";
    run;

    • Chris Hemedinger
      Chris Hemedinger on

      Secure SMTP requires components of SAS/SECURE, which is included with SAS 9.4. Make sure you've installed it. However, if you're using SAS University Edition, some of these encryption libraries are not part of the package.

  11. Lace-Chantelle Rogers on

    HI This is a great article and a lifesaver in terms of getting Gmail set up.

    Just a note if anyone has 2 step verification and your SMTP protocol returned "ERROR: 535-5.7.8 Username and Password not accepted. Learn more at" and have an older version of SAS, a simple solution is to set up an "App-Password".

    Go to my account - security settings- click on app passwords and add a new app (you can name it what either you want). You then replace your Gmail password in emailpw with the code generated by the app password and you will now be able to send emails again.

  12. Mark A Hinds on

    Chris,

    I'm trying to have SAS send email as a GMAIL account it used to work in University Edition and I moved to OnDemand for Academics. Now it seems to struggle with the following setup code:
    options validvarname=v7 obs=max lrecl=1000 emailhost=("smtp.gmail.com" port=587
    STARTTLS auth=plain
    id="xxxxxxxxxx@gmail.com"
    pw="xxxxxxxxx"
    )
    I get the following error:
    NOTE: The file SENDMAIL is:
    E-Mail Access Device

    ERROR: Email: 535-5.7.8 Username and Password not accepted. Learn more at
    ERROR: Email: 535 5.7.8 https://support.google.com/mail/?p=BadCredentials x10sm28211939pfr.150 - gsmtp
    NOTE: The SAS System stopped processing this step because of errors.

    Mark
    ;

    • Chris Hemedinger
      Chris Hemedinger on

      Hi Mark,

      Do you have 2-factor authentication set up for your Google account (best practice)? If so, you might need to use the "App password" approach (also has security concerns, but can do the trick if you are careful). But I haven't tried this with SAS OnDemand for Academics.

  13. Mark A Hinds on

    Chris,

    I'm not using 2-factor authentication. I've turned on access to 'less secure 3rd party apps'. This approach worked in the University edition but since I moved to OnDemand it's been problematic

    Mark

    • Chris Hemedinger
      Chris Hemedinger on

      Mark, I have two-factor auth set up on my Google account so I cannot test exactly the same way. But I did generate an App password per these instructions, and I was able successfully send email -- using my Gmail address -- in SAS OnDemand for Academics. This tells me that the SAS environment has all of the bits necessary to allow this to work.

      The App password is a 16-character code that Google can generate for you and will work just like your password for Gmail. It must be managed carefully of course, as it does allow any client app to act "as you" on Gmail.

  14. Mark A Hinds on

    Chris,

    I've tried the phone generating the App password for Mail, my computer running the SAS job generating the App password for Mail. In each case changing my password to the App password, None of it is working. I get the following SAS error:
    NOTE: The file SENDMAIL is:
    E-Mail Access Device

    ERROR: Email: 535-5.7.8 Username and Password not accepted. Learn more at
    ERROR: Email: 535 5.7.8 https://support.google.com/mail/?p=BadCredentials 26sm91699pgx.72 - gsmtp
    NOTE: The SAS System stopped processing this step because of errors.

    Mark

    • Chris Hemedinger
      Chris Hemedinger on

      I read that App passwords apply only when you have two-factor auth enabled. I don't know if that's getting in your way. If you have access to another command-line tool like sendmail, perhaps you can try with the same settings and see if it works or if the problem is specific to SAS OnDemand for Academics.

Back to Top