How to send email from SharePoint Online using Rest API?

How to send email from SharePoint Online using Rest API?
InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can send email using rest api in SharePoint online or SharePoint 2013. Here in this example I have created a sample contact us form. Here there will be three fields and a button. On click of the button it will send the email.

Also read:
– Create SharePoint 2013 group using Rest API and Update SharePoint Group Details using Rest API

– SharePoint 2013 Get All Users from SharePoint group using Rest API

– Configure On Premise environment for Apps in SharePoint 2016

Here I have added the below script to a web part page in a script editor web part.

<h1>Contact Us</h1>?<br/>
Name: ?<br/>
<input type=”text” name=”myname” id=”myname” placeholder=”Enter your Name”/><br/>
Email: <br/>
<input type=”text” name=”myemail” id=”myemail” placeholder=”Enter your Email Address”/><br/>
Message: <br/>
<textarea rows=”4″ cols=”50″ id=”mymessage”></textarea><br/><br/>
<button type=”button” id=”fbutton”>Send Message</button>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
<script>
$(document).ready(function() {
$(“#fbutton”).click(function(){
var sitetemplateurl = _spPageContextInfo.webServerRelativeUrl + “_api/SP.Utilities.Utility.SendEmail”;
var name = $(“#myname”).val();
var from = $(“#myemail”).val();
var msg = ‘From: ‘ + name + ‘<br/><br/>’ + ‘Email: ‘ + from + ‘<br/><br/><br/>’ + $(“#mymessage”).val();

$.ajax({
contentType: ‘application/json’,
url: sitetemplateurl,
type: “POST”,
data: JSON.stringify({
‘properties’: {
‘__metadata’: { ‘type’: ‘SP.Utilities.EmailProperties’ },
‘From’: from,
‘To’: { ‘results’: [‘user1@OnlySharePoint2013.onmicrosoft.com’] },
‘Body’: msg,
‘Subject’:’New Email From SharePoint Online using RestAPI’
}
}),
headers: {
“Accept”: “application/json;odata=verbose”,
“content-type”: “application/json;odata=verbose”,
“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()
},
success: function (data) {
alert(‘Message sent successfully !!!’);
$(“#myname”).val(”);
$(“#myemail”).val(”);
$(“#mymessage”).val(”);
},
error: function (err) {
alert(JSON.stringify(err));
}
});
});
});
</script>

Once save the page, the page will appear like below:

How to send email from SharePoint Online using Rest API?
How to send email from SharePoint Online using Rest API?

After saving the page, after clicking on the button the email will go like below:

How to send email from SharePoint Online using Rest API?
How to send email from SharePoint Online using Rest API?

Hope this will be helpful.

Similar SharePoint 2013 Tutorials

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply