Question

I am trying to send an email via SharePoint rest when a button is pushed in a custom HTML form that I've made within SharePoint.

I was implementing the JS solution found here but this is simply opening up my default mail client instead of automatically sending the email. Is there a way to send the email automatically without opening the default mail client?

var mail = {
    properties: {
        __metadata: { 'type': 'SP.Utilities.EmailProperties' },
        From: 'from@mail.com',
        To: { 'results': ['one@mail.com','two@mail.com'] },
        Body: 'some body',
        Subject: 'subject'
    }
};

var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
var urlTemplate = getAppWebUrlUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify(mail),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {

            // code

        },

        error: function (err) {

            // code

        }
    });
Était-ce utile?

La solution

Your code is quite right, my guess is that your problem is in the html who calls the method. HTML

<input type="button"id="sendMail" value="Send Email"/>

javascript

$(function() {
    $("#sendMail").click(function() {
        //var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
        var urlTemplate = _spPageContextInfo.webAbsoluteUrl +"/_api/SP.Utilities.Utility.SendEmail";
        //your code
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top