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: '[email protected]',
        To: { 'results': ['[email protected]','[email protected]'] },
        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

        }
    });
1
  • Your code is quite right, your problem is not the code.
    – Marco
    Commented Jan 25, 2017 at 18:38

1 Answer 1

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
    });
});
2
  • I am now getting an error "Uncaught ReferenceError: utils is not defined" when the "getAppWebUrlUrl" is being created
    – dbrown
    Commented Jan 25, 2017 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
    Commented Jan 25, 2017 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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