BookmarkSubscribeRSS Feed
laharts
Fluorite | Level 6

Hello,

I was trying to use the approach described on this page: https://blogs.sas.com/content/sgf/2017/04/21/use-sas-send-email-embeds-graph-body-email

 

It worked fine when I ran the sample code:

%let workdir=%trim(%sysfunc(pathname(work)));
ods _ALL_ close; 
ods listing gpath="&workdir";
ods graphics / reset=index outputfmt=PNG imagename='email';  
title1 'Graph output emailed using SAS';
proc sgplot data=sashelp.cars; 
  bubble x=horsepower y=mpg_city size=cylinders;
run;
filename sendmail email to=("first.last@company.com") from=("first.last@company.com")
     attach=("&workdir./email.png" inlined='sgplot')
     type='text/html' subject="Emailing graphics output";
      data _null_;
 file sendmail;  
 put '<html>';
 put '<body>';
 put '<img src=cid:sgplot>';
 put '</body>';
 put '</html>';
run; 
      filename sendmail clear;

But doesn't work if I try it with any other data- the log doesn't show any errors, it seems to run the same way, but no email ever arrives. For example:

data work.class (label='Student Data');
infile datalines dsd dlm='|' truncover;
input Name :$8. Sex :$1. Age Height Weight ;
datalines4;
Alfred|M|14|69|112.5
Alice|F|13|56.5|84
Barbara|F|13|65.3|98
Carol|F|14|62.8|102.5
Henry|M|14|63.5|102.5
James|M|12|57.3|83
Jane|F|12|59.8|84.5
Janet|F|15|62.5|112.5
Jeffrey|M|13|62.5|84
John|M|12|59|99.5
Joyce|F|11|51.3|50.5
Judy|F|14|64.3|90
Louise|F|12|56.3|77
Mary|F|15|66.5|112
Philip|M|16|72|150
Robert|M|12|64.8|128
Ronald|M|15|67|133
Thomas|M|11|57.5|85
William|M|15|66.5|112
;;;;
%let
workdir=%trim(%sysfunc(pathname(work))); ods _ALL_ close; ods listing gpath="&workdir"; ods graphics / reset=index outputfmt=PNG imagename='email'; title1 'Graph output emailed using SAS'; proc sgplot data=work.class; bubble x=weight y=height size=age run; filename sendmail email to=("first.last@company.com") from=("first.last@company.com") attach=("&workdir./email.png" inlined='sgplot') type='text/html' subject="Emailing graphics output"; data _null_; file sendmail; put '<html>'; put '<body>'; put '<img src=cid:sgplot>'; put '</body>'; put '</html>'; run; filename sendmail clear;

 

Is there any reason it would only work with sashelp data? Please let me know if I can provide any additional information. 

14 REPLIES 14
Patrick
Opal | Level 21

Does your code as posted execute without error?

Patrick_0-1690509299343.png

 

laharts
Fluorite | Level 6

Apologies, I somehow deleted that ; when copying the code over, but it was there when I was trying to run the data. I've included the log in a reply below. 

ballardw
Super User

Show us the LOG include the CODE and any messages for everything starting at :ODS _all_ Close; 

 

"No errors" is not the same a "no issues that may affect the output".

 

And I say show the log as the code you show for the second image doesn't run properly:

proc sgplot data=work.class; 
  bubble x=weight y=height size=age
run;

a missing ; on the Bubble statement means that would generate:

5    proc sgplot data=work.class;
6      bubble x=weight y=height size=age
7    run;
     ---
     22
     202
ERROR 22-322: Syntax error, expecting one of the following: ;, /.

ERROR 202-322: The option or parameter is not recognized and will be
               ignored.

laharts
Fluorite | Level 6

Apologies, somehow I dropped that ; when copying the code over, that doesn't seem to be the issue. Here is the log for the code (updated to test with actual email):



1 The SAS System 12:13 Friday, July 28, 2023

1 %_eg_hidenotesandsource;
5 %_eg_hidenotesandsource;
33
34 data work.class (label='Student Data');
35 infile datalines dsd dlm='|' truncover;
36 input Name :$8. Sex :$1. Age Height Weight ;
37 datalines4;

NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

57 ;;;;
58
59 %let workdir=%trim(%sysfunc(pathname(work)));
60 ods _ALL_ close;
61 ods listing gpath="&workdir";
62 ods graphics / reset=index outputfmt=PNG imagename='email';
63 *title1 'Graph output emailed using SAS';
64 proc sgplot data=work.class noautolegend;
65 bubble x=weight y=height size=age;
66 run;

NOTE: PROCEDURE SGPLOT used (Total process time):
real time 0.22 seconds
cpu time 0.07 seconds

NOTE: Listing image output written to D:\saswork\hartsough-lauren\_TD29132_S16ZAPAP02895_\Prc2\email.png.
NOTE: There were 19 observations read from the data set WORK.CLASS.

67
68 filename sendmail email to=("hartsough-lauren@norc.org") from=("hartsough-lauren@norc.org")
69 attach=("&workdir./email.png" inlined='sgplot')
70 type='text/html' subject="Emailing graphics output";
71 data _null_;
72 file sendmail;
73 put '<html>';
74 put '<body>';
75 put '<img src=cid:sgplot>';
76 put '</body>';
77 put '</html>';
78 run;

NOTE: The file SENDMAIL is:
E-Mail Access Device

Message sent
To: "hartsough-lauren@norc.org"
Cc:
Bcc:
Subject: Emailing graphics output
Attachments: ( "D:\saswork\hartsough-lauren\_TD29132_S16ZAPAP02895_\Prc2/email.png" INLINED = 'sgplot' )
NOTE: 5 records were written to the file SENDMAIL.
The minimum record length was 6.
The maximum record length was 20.
NOTE: DATA statement used (Total process time):
2 The SAS System 12:13 Friday, July 28, 2023

real time 0.13 seconds
cpu time 0.00 seconds

79 filename sendmail clear;
NOTE: Fileref SENDMAIL has been deassigned.
80
81 %_eg_hidenotesandsource;
95
96
97 %_eg_hidenotesandsource;
100

laharts
Fluorite | Level 6

And this is the log if I run with the sashelp data used in the example, which does successfully send the email with the plot embedded:

1 The SAS System 12:13 Friday, July 28, 2023

1 %_eg_hidenotesandsource;
5 %_eg_hidenotesandsource;
33
34 data work.class (label='Student Data');
35 infile datalines dsd dlm='|' truncover;
36 input Name :$8. Sex :$1. Age Height Weight ;
37 datalines4;

NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

57 ;;;;
58
59 %let workdir=%trim(%sysfunc(pathname(work)));
60 ods _ALL_ close;
61 ods listing gpath="&workdir";
62 ods graphics / reset=index outputfmt=PNG imagename='email';
63 *title1 'Graph output emailed using SAS';
64 /*proc sgplot data=work.class noautolegend;
65 bubble x=weight y=height size=age;
66 run;*/
67 proc sgplot data=sashelp.cars;
68 bubble x=horsepower y=mpg_city size=cylinders;
69 run;

NOTE: PROCEDURE SGPLOT used (Total process time):
real time 0.27 seconds
cpu time 0.11 seconds

NOTE: Listing image output written to D:\saswork\hartsough-lauren\_TD29132_S16ZAPAP02895_\Prc2\email.png.
NOTE: There were 428 observations read from the data set SASHELP.CARS.

70 filename sendmail email to=("hartsough-lauren@norc.org") from=("hartsough-lauren@norc.org")
71 attach=("&workdir./email.png" inlined='sgplot')
72 type='text/html' subject="Emailing graphics output";
73 data _null_;
74 file sendmail;
75 put '<html>';
76 put '<body>';
77 put '<img src=cid:sgplot>';
78 put '</body>';
79 put '</html>';
80 run;

NOTE: The file SENDMAIL is:
E-Mail Access Device

Message sent
To: "hartsough-lauren@norc.org"
Cc:
Bcc:
Subject: Emailing graphics output
Attachments: ( "D:\saswork\hartsough-lauren\_TD29132_S16ZAPAP02895_\Prc2/email.png" INLINED = 'sgplot' )
NOTE: 5 records were written to the file SENDMAIL.
The minimum record length was 6.
2 The SAS System 12:13 Friday, July 28, 2023

The maximum record length was 20.
NOTE: DATA statement used (Total process time):
real time 0.13 seconds
cpu time 0.03 seconds

81 filename sendmail clear;
NOTE: Fileref SENDMAIL has been deassigned.
82
83 %_eg_hidenotesandsource;
97
98
99 %_eg_hidenotesandsource;
102

Quentin
Super User

This makes no sense to me.  Everything in the log looks like things went fine.  And this is happening repeatedly?  Hoping that there might just be a delay in your mail server, or maybe your spam filter picked up the second message and treated it as junk.  But if you can repeat this behavior, than I'm flummoxed.  But no, the cause can't be using sashelp data, because your SGPLOT doesn't do anything special for sashelp data, and it's just writing the .png file.

 

In fact, if you're running this in EG, and the .png file just sits in your work directory, you should just be able to run the filename statement and data _null_ step at the end, at it should send the email every time.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
laharts
Fluorite | Level 6

Yes it happens repeatedly- I initially thought it was an issue with my data but it happens with the dummy data (class) as well. With the sashelp data the email arrives within seconds, and since I first encountered this issue yesterday I don't think it's a delay (though could be something weird with spam). I wasn't sure where to go from there since it didn't make sense to me why the SGPLOT/data changing would affect things when everything else is the same. 

 

I'm having some luck using a different approach described in the article to use title vs data _null_ to include the image, it at least is sending emails with the plot embedded.

filename sendmail email  to=("first.last@company.com") from=("first.last@company.com")
     attach=("c:\temp\sgplot.png" inlined='sgplot') 
     type='text/html' subject="Email test of GRAPH output";
      ods _all_ close; 
ods html file=sendmail; 
title1 '<img src=cid:sgplot>';
proc print data=sashelp.class; 
run;
ods html close; 
ods listing; 
filename sendmail clear;
Quentin
Super User

If you've got a helpful IT person, maybe they could check the mail server logs.  

 

I'm assuming this is EG connecting to SAS running on a Windows server.  And there is some SMTP mail server that is actually sending the mails.

 

From the SAS logs, seems like SAS thinks everything worked fine.  Which means (I think) SAS said to the mail server "please send this message" and the mail server said "ok, will do."  But it's possible that the mail server still chokes later for some reason, and doesn't tell SAS there was a problem.  But there might be mail server logs that would help trace the problem.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ballardw
Super User

Not that this is the cause of your problem  but might cause different problems later.

Note the path used for the file and attachment. Such inconsistencies mixing delimiters may cause other problems.

NOTE: Listing image output written to D:\saswork\hartsough-lauren\_TD29132_S16ZAPAP02895_\Prc2\email.png.
NOTE: There were 428 observations read from the data set SASHELP.CARS.

70 filename sendmail email to=("hartsough-lauren@norc.org") from=("hartsough-lauren@norc.org")
71 attach=("&workdir./email.png" inlined='sgplot')

I am surprised that your Work library is a folder named Prc2 with that path as that is not typical for most SAS installs I've seen.

Did you try writing the file to another location? Someplace other than the WORK library?

Quentin
Super User

Can you run the code using work.class, go to your work directory, and see if the .png file is happy? Does the timestamp indicate that it was written when you ran the code?  If you open it, do you see the graph?

 

In your original code, I'm wondering what happens if you change it to add some text in the email, e.g.:

 

 file sendmail;  
 put '<html>';
 put '<body>';
 put 'Hello world';
 put '<img src=cid:sgplot>';
 put 'Goodbye world';
 put '</body>';
 put '</html>';
run; 

Do you get an email with text only, or still no email?

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
laharts
Fluorite | Level 6

Thank you all for your help! 

 

@ballardw  @Quentin  @Tom  It does appear to have been an issue with saving the .png in the work library- not sure exactly what the problem was but when I saved the file to another location (external to SAS) I was successfully able to embed the plots in an email. Not sure why my work library has that path, IT handles all installs for my computer. 

 

I also ended up using ods html and ods html text to inline the image as opposed to the data _null_  or title options in the original article/code. This gave me some flexibility on including multiple plots along with reports in the desired order. E.g. 
ods html text= '<img src=cid:sgplot1>'; 

Quentin
Super User

Truly bizzare.  It's possible that the homedir could be part of the problem (maybe the mailserver doesn't have access?) but I can't see why it would work when you run it with sashelp.class, then not work with work.class.  But, glad you got it working!

 

When you've got a final version you like, would you mind sharing a full example of your approach to sending multiple in-line graphs, along with text (I assume)?  I may want to play with this in the future.  I hadn't realized it was possible without needing to use HTML5.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
laharts
Fluorite | Level 6

Thank you! I've included sample code below based on what I'm now using, it was initially in a loop to send custom emails to each individual so may have some weird formatting that I didn't catch when extracting it.

options nofmterr compress=yes mprint mlogic validvarname=v7 emailsys=SMTP emailhost="MX.COMPANY.ORG" emailport=25;
%let plot_output = filepath;
 
ods noresults;
ODS listing GPATH="&plot_output." style=statistical;
ods graphics on / noborder border=off reset=index imagename="group_var1";
proc sgplot data=my_data noautolegend;
vbar group / response=var1 group=group datalabel datalabelattrs=(size=12pt); 
xaxis display=(nolabel) valueattrs=(size=12pt);
yaxis label='Var1' valueattrs=(size=10pt) labelattrs=(size=12pt);
run;
ods results;
ods listing close; 
 
ODS listing GPATH="&plot_output." style=statistical;
ods graphics on / noborder border=off reset=index imagename="group_var2";
proc sgplot data=my_data noautolegend;
vbar group / response=var2 group=group datalabel datalabelattrs=(size=12pt); 
xaxis display=(nolabel) valueattrs=(size=12pt); 
yaxis label='Var2' valueattrs=(size=10pt) labelattrs=(size=12pt);
run;
ods results;
ods listing close; 
 
ODS listing GPATH="&plot_output." style=statistical;
ods graphics on / noborder border=off reset=index imagename="group_var3";
proc sgplot data=my_data noautolegend;
vbar group / response=var3 group=group datalabel datalabelattrs=(size=12pt); 
xaxis display=(nolabel) valueattrs=(size=12pt);
yaxis label='Var3' valueattrs=(size=10pt) labelattrs=(size=12pt);
run;
ods results;
ods listing close; 
 
 
filename temp2 email from=('from@company.org')
attach=("&plot_output.\group_var1.png" inlined='sgplot1')
attach=("&plot_output.\group_var2.png" inlined='sgplot2')
attach=("&plot_output.\group_var3.png" inlined='sgplot3')
to=('to@company.org')
subject="Subject" type='text/html';
ods _all_ close;
ods escapechar='^';
ods html body=temp2 options(pagebreak="no")style=mystyle rs=none; 
proc odstext;
p 'Hello,';
p '';
p 'Below are your locating statistics for last week...';
run;
proc report data=my_data nowd style(report)=[cellspacing=0 cellpadding=3 just=l];
column Group Var1 Var2 Var3;
run;
proc odstext;
p '';
p 'Text';
p '<img src=cid:sgplot1 width="500">';
run;
proc odstext;
p '';
p '';
p 'Text';
p '<img src=cid:sgplot2 width="500">';
run;
proc odstext;
p '';
p '';
p 'Text';
p '<img src=cid:sgplot3 width="500">';
run;
proc odstext;
p '';
p '';
p "Please email ^{style [url='mailto:questions@company.org' linkcolor=white color=blue textdecoration=underline]questions@company.org} if you have any questions." ;
run;
ods html close; 
ods listing; 
filename temp2 clear;
 
 
 
 
Tom
Super User Tom
Super User

Some things to check.

1) That the code that make the PNG file is actually changing the file. Perhaps the file is locked so no file got written.

 

2) That the code that makes the PNG file is not changing the name each time it runs.  Perhaps it sees the existing file and decides it needs call it something else to avoid the conflict.  Like browsers do when you download files with the same name.  So perhaps it appended a digit to the filename to make it distinct.

 

3) That there is not some delay in sending the email.  If what SAS does it queue up a command to send the mail that just points to name of the file to attach and the actual sendmail routine copies the file later perhaps by then you have replaced the file with something else.

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 1941 views
  • 3 likes
  • 5 in conversation