Send Email With Attachment In Office 365 (SharePoint Online)

Recently, I came up with a requirement to send an email with attachment in Office 365 (SharePoint Online). This needs to be a custom functionality wherein for each line item in a jQuery Data table (as shown in below image), a user needs to send an email with an attachment to a specific email address.

SharePoint

As shown in above image, every line item has an email icon, on click of which a pop needs to be opened with To, Subject, Add Attachment fields needs to be there. However, after searching on the internet, it was explored that we cannot send an email with attachment using REST API in SharePoint Online.

SharePoint
Fig: Email Pop-Up

Below code snippet shows the REST API implementation to send an email,

  1. $.ajax({  
  2.     contentType: 'application/json',  
  3.     url: urlTemplate,  
  4.     type: "POST",  
  5.     data: JSON.stringify({  
  6.         'properties': {  
  7.             '__metadata': {  
  8.                 'type''SP.Utilities.EmailProperties'  
  9.             },  
  10.             'From': from,  
  11.             'To': {  
  12.                 'results': [to]  
  13.             },  
  14.             'Body': body,  
  15.             'Subject': subject  
  16.         }  
  17.     }),  
  18.     headers: {  
  19.         "Accept""application/json;odata=verbose",  
  20.         "content-type""application/json;odata=verbose",  
  21.         "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  22.     },  
  23.     success: function(data) {  
  24.         alert('Email Sent Successfully');  
  25.     },  
  26.     error: function(err) {  
  27.         alert('Error in sending Email: ' + JSON.stringify(err));  
  28.     }  
  29. }); //End of AJAX  

In the above code, “'SP.Utilities.EmailProperties'” is the main API (object) which sends the email, however only below mentioned properties are exposed by this object. So, it is found that sending email with attachment is not possible.

  • BCC
  • Body
  • CC
  • from
  • subject
  • to 

Fig: Only the following properties have been exposed by this object are to be used within REST call.

Solution Implemented

So, what I did to achieve this functionality is, I developed a SharePoint Designer Workflow and when the user adds an attachment on the email pop up screen (shown above), I added the document in a SharePoint Document Library and then workflow triggers.

The “To” and “Subject” fields also got updated (as metadata) in the document library.

So, when the user sends an email, we made an entry in the document library with the document. However, in this solution, while in the email, we can only send the document link to the user. Below is the sample email sent to a user,

SharePoint
Fig: Email Body sent to customer

Key Points found during this implementation

  1. You cannot send email to an external email address using REST API code. External emails are not supported. You will get an error if you will try to send email to the external user stating that – “A recipient must be specified”.

  2. You can only send email to users of that Office 365 tenant from which you are triggering the email.

  3. The email gets triggered from a specific email address in SharePoint Online i.e. “From” field remains same and the email address is – noreply@sharepointonline.com

  4. Below are the IMAP server settings to be used for outgoing email in Office 365,
SettingIMAP (Incoming)SMTP (Outgoing)
Server NameOutlook.office365.comsmtp.office365.com
Port Number993587
Encryption MethodSSLTLS
  1. There are third party solutions using Workflows which allows sending emails with attachments in SharePoint Designer.

Summary

In this article, I explained an alternative that how can we send an email with attachment (document link) in SharePoint Online (Office 365).