Graph api send mail with attachment from SharePoint Online:
Alternative:
This Graph api has 4MB total size limitation over which you can go for ShareLink option using [/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink] rest api with minimum 'Members' permission level.
Preq:
Need to Register App in Graph and give it 'Admin Consent'.
Graph api Encoded Code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/XXX.onmicrosoft.com/oauth2/v2.0/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "XXX",
"client_secret": "XXX",
"scope ": "https://graph.microsoft.com/.default"
},
success: function(response) {
token = response.access_token;
},
error: function(error) {
console.log(JSON.stringify(error));
}
})
}
function sendEmail() {
//Variable to get values from input elements
var to = $('#email').val();
var sub = $('#txtsubject').val();
var msg = $('#txtmessage').val();
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/v1.0/users/XXX@XXX.onmicrosoft.com/sendMail",
"method": "POST",
"headers": {
"authorization":"Bearer " + token,
"content-type": "application/json"
},
"processData": false,
"data": JSON.stringify({
"message": {
"subject": sub,
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [{
"emailAddress": {
"address": to
}
}]
}
}),
success: function(response) {
alert("email sent successfully to " + to + "");
},
error: function(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
})
}
</script>
<div class="container">
<div class="form-group">
<label for="email">To:</label>
<input type="email" class="form-control" id="email" required>
</div>
<div class="form-group">
<label for="txtsubject">Subject:</label>
<input type="text" class="form-control" id="txtsubject" required>
</div>
<div class="form-group">
<label for="txtmessage">Message:</label>
<textarea class="form-control" id="txtmessage" rows="7" required></textarea>
</div>
<button type="submit" onclick="sendEmail();" class="btn btn-default">Send</button>
</div>
</body>
ShareLink Encoded Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
getShareLink();
});
function getShareLink(){
var formDigestURL ="https://XXX/_api/contextinfo";
return $.ajax({
url: formDigestURL,
type: "POST",
datatype:"json",
headers: {
"accept": "application/json;odata=verbose",
},
success:function(formDigestData){
var dataURL="https://XXX/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink";
$.ajax({
url: dataURL,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": formDigestData.d.GetContextWebInformation.FormDigestValue,
},
contentType: "application/json; charset=utf-8",
dataType: "json",
data:JSON.stringify({
"request":{
"createLink":true,
"settings":{
"allowAnonymousAccess":false,
"linkKind":2,
"expiration":null,
"restrictShareMembership":false,
"updatePassword":false,
"password":""
}
}
}),
success:function(data){
alert("Sucess");
console.log(data.d.ShareLink.sharingLinkInfo.Url);
$('Div#showLinks').html(data.d.ShareLink.sharingLinkInfo.Url);
},
error: function (error) {
alert("Error");
console.log(JSON.stringify(error));
}
});
},
error: function (error) {
$("#error").append(JSON.stringify(error));
}
});
}
</script>
<div id="showLinks"></div>
Alternative:
This Graph api has 4MB total size limitation over which you can go for ShareLink option using [/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink] rest api with minimum 'Members' permission level.
Preq:
Need to Register App in Graph and give it 'Admin Consent'.
Graph api Encoded Code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<body>
<script type="text/javascript">
$(document).ready(function() {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/XXX.onmicrosoft.com/oauth2/v2.0/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "XXX",
"client_secret": "XXX",
"scope ": "https://graph.microsoft.com/.default"
},
success: function(response) {
token = response.access_token;
},
error: function(error) {
console.log(JSON.stringify(error));
}
})
}
function sendEmail() {
//Variable to get values from input elements
var to = $('#email').val();
var sub = $('#txtsubject').val();
var msg = $('#txtmessage').val();
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/v1.0/users/XXX@XXX.onmicrosoft.com/sendMail",
"method": "POST",
"headers": {
"authorization":"Bearer " + token,
"content-type": "application/json"
},
"processData": false,
"data": JSON.stringify({
"message": {
"subject": sub,
"body": {
"contentType": "Text",
"content": msg
},
"toRecipients": [{
"emailAddress": {
"address": to
}
}]
}
}),
success: function(response) {
alert("email sent successfully to " + to + "");
},
error: function(error) {
console.log(JSON.stringify(error));
alert(JSON.stringify(error));
}
})
}
</script>
<div class="container">
<div class="form-group">
<label for="email">To:</label>
<input type="email" class="form-control" id="email" required>
</div>
<div class="form-group">
<label for="txtsubject">Subject:</label>
<input type="text" class="form-control" id="txtsubject" required>
</div>
<div class="form-group">
<label for="txtmessage">Message:</label>
<textarea class="form-control" id="txtmessage" rows="7" required></textarea>
</div>
<button type="submit" onclick="sendEmail();" class="btn btn-default">Send</button>
</div>
</body>
ShareLink Encoded Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
getShareLink();
});
function getShareLink(){
var formDigestURL ="https://XXX/_api/contextinfo";
return $.ajax({
url: formDigestURL,
type: "POST",
datatype:"json",
headers: {
"accept": "application/json;odata=verbose",
},
success:function(formDigestData){
var dataURL="https://XXX/_api/web/Lists/getByTitle('Documents')/GetItemById('1')/ShareLink";
$.ajax({
url: dataURL,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": formDigestData.d.GetContextWebInformation.FormDigestValue,
},
contentType: "application/json; charset=utf-8",
dataType: "json",
data:JSON.stringify({
"request":{
"createLink":true,
"settings":{
"allowAnonymousAccess":false,
"linkKind":2,
"expiration":null,
"restrictShareMembership":false,
"updatePassword":false,
"password":""
}
}
}),
success:function(data){
alert("Sucess");
console.log(data.d.ShareLink.sharingLinkInfo.Url);
$('Div#showLinks').html(data.d.ShareLink.sharingLinkInfo.Url);
},
error: function (error) {
alert("Error");
console.log(JSON.stringify(error));
}
});
},
error: function (error) {
$("#error").append(JSON.stringify(error));
}
});
}
</script>
<div id="showLinks"></div>