0

I have javascript function to sendEmail like:

function sendEmail() {
    var appweburl = _spPageContextInfo.webAbsoluteUrl;
    var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";
    var fieldName = "Notificar a";
    var _PeoplePicker = $("div[title='" + fieldName + "']");
    var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
    var _sid ="";
    if (emailSpan.length > 0) {
        _sid = _sid = $(emailSpan).attr('sid');
        _sid = _sid.split("|")[2];
    } else {
        alert("null");
    }

    var fieldUsuario = _sid;    
    
    $.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': '[email protected]',
                'To': { 'results': [fieldUsuario] },
                'Body': 'Hello',
                'Subject': 'remember'
            }
        }),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            var result = data.d.results;
            var i = result.length;
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}

Problem occurs whenn it reach success function, data always come undefined:

enter image description here

Sources: How to Send Email By Javascript

Some ideas why it comes null? Regards

1
  • are you adding multiple values in People Picker field ? Commented May 3, 2018 at 7:18

1 Answer 1

0

You might be receiving emails but in Junk Mail folder. So You have to pass all the attributes e.g Body, Subject, To, From as a variable rather than static.

Please check the updated code below :

function sendEmail() {

var appweburl = _spPageContextInfo.webAbsoluteUrl;
var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";


var fieldName = "Notificar a";
var _PeoplePicker = $("div[title='" + fieldName + "']");
var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid ="";
if (emailSpan.length > 0) {
         _sid = _sid = $(emailSpan).attr('sid');
     _sid =  _sid.split("|")[2];
} else {
       alert("null");
}

        var fieldUsuario = _sid;
        var fromMail = '[email protected]';
        var subject = 'remember ';
        var body = 'Hello';    

    $.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': fromMail,
                'To': { 'results': [fieldUsuario] },
                'Body': body, 
                'Subject': subject
            }
        }
      ),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            var result = data.d.results;
            var i = result.length;
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}
3
  • one important question.. What should be my onmicrosoft from Email? Whatever I want?
    – David
    Commented May 3, 2018 at 14:39
  • yes, You can add any SharePoint registered user email address as From user, those emails will be sent from that mentioned user. Commented May 3, 2018 at 18:08
  • Another question sir, If I want to send value to multiple users, I change fieldUsuario for array into 'To', so my array is like [email protected], [email protected] , they are correctly separatted by commas or they need to separated by ;
    – David
    Commented May 3, 2018 at 20:18

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.