1

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

        }
    });
  • Your code is quite right, your problem is not the code. – Marco Jan 25 '17 at 18:38
2

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
    });
});
  • I am now getting an error "Uncaught ReferenceError: utils is not defined" when the "getAppWebUrlUrl" is being created – user2756091 Jan 25 '17 at 18:58
  • I strongly recommend you to use this property _spPageContextInfo.webAbsoluteUrl that the site provide. I edit my answer to help you with this – Marco Jan 25 '17 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.