18

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?

Rothrock
  • 3,010
  • 7
  • 43
  • 92
  • looks like additionalHeaders is a valid property as you have written, did you try "AdditionalHeaders" instead of "additionalHeaders". I think it is written in the same way for To and From (the first letter is capital). Just an Idea. – Shekar Reddy Jul 30 '15 at 00:07
  • Thanks. Yes I tried AdditionalHeaders and Additional Headers and Additional-Headers and a couple of other variations. None of them worked. – Rothrock Jul 30 '15 at 00:25

2 Answers2

14

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'
      }
    })
  }
}
Anders Aune
  • 6,268
  • 1
  • 22
  • 31
  • thanks. I'm in all-day meetings about something else. I'll try this out as soon as I can. – Rothrock Aug 18 '15 at 14:44
  • ah very nice. I was hoping the serializer would magically convert that back to a keyvalue collection :-) – John Liu Aug 19 '15 at 08:01
  • Thank you that allows me to send the headers without an error. I'm trying to get SP to send a meeting invite to Lotus Notes by using these headers 'Content-Type': 'multipart/alternative; boundary="--Status--"', 'Content-class': 'urn:content-classes:calendarmessage' the emails are coming through, but Notes doesn't recognize them as invites. Any ideas? – Rothrock Aug 19 '15 at 14:47
  • @Rothrock , I'm not sure tbh. But a guess is that the Content-Type property is somehow overridden by something. You can try and check the headers on the mail you receive. I don't have a notes server floating around so can't test it :) – Anders Aune Aug 20 '15 at 09:37
  • Yeah I'm looking at the headers that Notes gets. It does get the content-class header, but Content-Type is still text/html, not the "multipart/alternative" that I'm setting it to. hmmm. – Rothrock Aug 20 '15 at 15:43
  • Oh I just realized that selecting your answer as correct doesn't award you the points. Done that now. Really appreciate the help. – Rothrock Aug 20 '15 at 15:44
  • @Rothrock same behavior here when I send to an Exchange server, the Content-Type is always text/html. Its probably the default Content-Type chosen, but does not change when you specify one. So its either a bug or by design. You could try contacting Microsoft for an answer. – Anders Aune Aug 21 '15 at 06:16
  • Thank you for that. I'll pass it along through our support channels and see what I can find out. – Rothrock Aug 21 '15 at 15:58
6

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' }
            ]
        }
    }
}
John Liu
  • 986
  • 7
  • 11