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 = 'bocar@bocar.com';
        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': ['user1@onmicrosoft.com'] },
                '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': ['user1@onmicrosoft.com;user2@onmicrosoft.com'] },

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': ['user1@onmicrosoft.com,user2@onmicrosoft.com'] },

But it shouldn´t work

Update:

enter image description here

  • You are doing it right, just you have to separate all email address in single quote each. 'To': { 'results': ['user1@onmicrosoft.com','user2@onmicrosoft.com'] }, – Gopal Zadafiya May 4 '18 at 5:05
  • How can I achieve that? my comma separated array is var arrEmailID = []; @GopalZadafiya – David May 4 '18 at 5:39
  • I would recommend you some modification in arrEmailID so that You can have above format in ready position. – Gopal Zadafiya May 4 '18 at 5:49
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");
}
  • I try it but when it try to push second email I get: Uncaught type error: arrEmailID.push is not a function – David May 4 '18 at 6:12
  • In which browser are you executing this code ? – Gopal Zadafiya May 4 '18 at 6:14
  • I debbuging in google chrome – David May 4 '18 at 6:19
  • Is your variable declaration is like var arrEmailID = []; ? – Gopal Zadafiya May 4 '18 at 6:21
  • yes sir, as my code in my question – David May 4 '18 at 6:24

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.