0

I have sscript function to send enamils like this:

function sendEmail() {

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


var fieldName = "Notificar a";
var body = "Descripción";
var _body = $("textarea[title^='" + body + "']");

var fullURL = window.location.href
var url = new URL(fullURL ); 
var ID = url.searchParams.get("ID");

var arrEmailID = [];
var _PeoplePicker = $("div[title='" + fieldName + "']");
var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid ="";
if (emailSpan.length > 0) {
    $(emailSpan).each(function (){
             _sid = $(this).attr('sid');
            _sid =  _sid.split("|")[2];
            // alert(_sid.split("|")[2]);
             arrEmailID.push(_sid);
        })

         // _sid = _sid = $(emailSpan).attr('sid');
         // _sid =  _sid.split("|")[2];
} else {
       alert("null");
}

        var fieldUsuario = _sid;
        var fromMail = '[email protected]';
        var subject = 'Tarea Editada: ' + 'ID: ' +ID + ' ' + _body.val();
        var body = 'Se ha editado la tarea: '+ 'ID: ' + ID+ ' ' + 'Descripción: ' + _body.val() ;    

       var arraySemicolon =  arrEmailID.join(";");

    $.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: "POST",
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': fromMail,
                'To': { 'results': ['[email protected]'] },
                '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;
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}

If I send in To action only one email it works correctly. Problem starts when I try to send more than one as:

'To': { 'results': ['[email protected];[email protected]'] },

I don´t know why I don´t receiving email when I use multiple users. Any one have an idea why?

I also try to use comma separator as:

'To': { 'results': ['[email protected],[email protected]'] },

But it shouldn´t work

Update:

enter image description here

3
  • You are doing it right, just you have to separate all email address in single quote each. 'To': { 'results': ['[email protected]','[email protected]'] }, Commented May 4, 2018 at 5:05
  • How can I achieve that? my comma separated array is var arrEmailID = []; @GopalZadafiya
    – David
    Commented May 4, 2018 at 5:39
  • I would recommend you some modification in arrEmailID so that You can have above format in ready position. Commented May 4, 2018 at 5:49

1 Answer 1

0

You are doing it right, just you have to separate all email address in single quote each.

Updated code block :

if (emailSpan.length > 0) {
    $(emailSpan).each(function (){
             _sid = $(this).attr('sid');
             _sid =  _sid.split("|")[2];
             arrEmailID.push("'" + _sid + "'");
        });
       arrEmailID = arrEmailID.join(",");
} else {
       alert("null");
}
7
  • I try it but when it try to push second email I get: Uncaught type error: arrEmailID.push is not a function
    – David
    Commented May 4, 2018 at 6:12
  • In which browser are you executing this code ? Commented May 4, 2018 at 6:14
  • I debbuging in google chrome
    – David
    Commented May 4, 2018 at 6:19
  • Is your variable declaration is like var arrEmailID = []; ? Commented May 4, 2018 at 6:21
  • yes sir, as my code in my question
    – David
    Commented May 4, 2018 at 6:24

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.