BookmarkSubscribeRSS Feed
thesasuser
Lapis Lazuli | Level 10

Hello
I have been trying to send emails with excel files as attachment using SAS code from SAS Viya4 on AWS.
Sending of email using Oauth2 and Microsoft graph is the method we are expected to use.
(SMTP approach is no more allowed).
One can send emails but when an excel file is attached, it file is restricted to 270 bytes. The file is simply unusable.
The relevant portion of the code are attached.
There are no error in the log and get the message "NOTE : 202 ACCEPTED.
Request the community for guidance.
The code is as given below.


/* Step 1: Base64 encode the XLSX file */
filename myfile "/mnt/viya-share/data/test/cars.xlsx";
filename b64file temp;

data _null_;
    infile myfile recfm=n;
    file b64file lrecl=32767;
    input byte $char1.;
    put byte $base64x1. @@;  
run;
/* Step 2: Build JSON email payload */
filename mailbdy temp;

/* Write JSON header */
data _null_;
    file mailbdy;
    put '{';
    put '  "message": {';
    put '    "subject": "Test email from SAS Viya 4",';
    put '    "body": { "contentType": "Text", "content": "Hello from SAS Viya!" },';
    put '    "toRecipients": [ { "emailAddress": { "address": "recipient@example.com" } } ],';
    put '    "attachments": [';
    put '      {';
    put '        "@odata.type": "#microsoft.graph.fileAttachment",';
    put '        "name": "cars.xlsx",';
    put '        "contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",';
    put '        "contentBytes": "';
run;

/* Append Base64 content directly */
data _null_;
    infile b64file lrecl=32767;
    file mailbdy mod;
    input;
    put _infile_;
run;

/* Close JSON */
data _null_;
    file mailbdy mod;
    put '"';
    put '      }';
    put '    ]';
    put '  },';
    put '  "saveToSentItems": "true"';
    put '}';
run;

/* Send via microsoft Graph */
/* Step 3: Send email via Microsoft Graph */
filename sendresp temp;

proc http
    url="https://graph.microsoft.com/v1.0/users/sender@example.com/sendMail"
    method="POST"
    in=mailbdy
    out=sendresp;
    headers
        "Authorization"="Bearer &access_token"
        "Content-Type"="application/json";
run;

 

1 REPLY 1
gwootton
SAS Super FREQ
I tested your DATA step code on a file and it did not produce base64 encoded content.

I found this blog post that provides some code to base64 encode an entire file using the base64 format.
https://support.selerity.com.au/hc/en-us/articles/223345708-Tip-SAS-and-Base64

Does using that code to encode your file allow the attachment to send as expected?
--
Greg Wootton | Principal Systems Technical Support Engineer