Question

Want to use the built in SendEmail with javascript and jquery. Found this code and got it working -- sends the email -- using the code below:

var appweburl = "https://example.com/site";
var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";

$.ajax({
    contentType: 'application/json',
    url: urlTemplate,
    type: "POST",
    data: JSON.stringify({
        'properties': {
            '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
            'From': from,
            'To': { 'results': [to] },
            'Body': message,
            'Subject': subject
        }
    }
  ),
    headers: {
        "Accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
       console.log("An email was sent.");
    },
    error: function (args) {
       console.log("We had a problem and an email was not sent.");
    }
});

According to this page there is also an additionalHeaders property. I'm trying to add some additional headers into my email like this.

'additionalHeaders': {
     'content-type': 'text/html'
}

But when I try that I get a server response of

The property 'additionalHeaders' does not exist on type 'SP.Utilities.EmailProperties'. Make sure to only use property names that are defined by the type.

edit After I checked in the page it still doesn't send email, but the server started responding:

 A collection was found without the 'results' property. In OData, each collection must be represented as a JSON object with a property 'results'.

So maybe that is how to add the additionalHeaders?Does anybody know if this is possible to do with javascript?

Was it helpful?

Solution

John is more or less right, but you need to use SP.KeyValue afaik:

'properties': {
               '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
               'From': from,
               'To': { 'results': [to] },
               'Body': body,
               'Subject': subject,

                "AdditionalHeaders":
                {"__metadata":
                    {"type":"Collection(SP.KeyValue)"},
                    "results":
                    [ 
                        {               
                            "__metadata": {
                            "type": 'SP.KeyValue'
                        },
                            "Key": "X-MC-Tags",
                            "Value": 'test',
                            "ValueType": "Edm.String"
                       }
                    ]
                }
           }

enter image description here

SP.Utilities.EmailProperties:

'AdditionalHeaders': { type:'Collection(SP.KeyValue)' },
'BCC': { type:'Collection(Edm.String)' },
'Body': { type:'Edm.String' },
'CC': { type:'Collection(Edm.String)' },
'From': { type:'Edm.String' },
'Subject': { type:'Edm.String' },
'To': { type:'Collection(Edm.String)' }

Here is a function which will convert a regular object to a SP.KeyValue collection:

function objectToSPKeyValueCollection (obj) {
  return {
    __metadata: {
      type: 'Collection(SP.KeyValue)'
    },
    results: Object.keys(obj).map(key => {
      return {
        __metadata: {
          type: 'SP.KeyValue'
        },
        Key: key,
        Value: obj[key],
        ValueType: 'Edm.String'
      }
    })
  }
}

OTHER TIPS

Looking at the example and your error message, I wonder if AdditionalHeaders needs results (similar to To:)

data: JSON.stringify({
    'properties': {
        '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
        'From': from,
        'To': { 'results': [to] },
        'Body': message,
        'Subject': subject,
        'AdditionalHeaders': {
            'results': [
                { 'content-type': 'text/html' }
            ]
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top