2

When sending emails via SP.Utilities.Utility.SendEmail, is it possible to change the From's display name so that it says something different rather than a title of the current site?

I'm fully aware of the fact that the from's email will always be no-reply@sharepointonline.com, but can I change the display name of it, at least? enter image description here

Here is the code I'm using:

var subject = "SUBJECT OF THE MAIL";
var mailContent = "<h3>Some Heading for the mail</h3><p>Content</p><div>Content</div>";
var toList = ["usser5@contoso.onmicrosoft.com"]

//Send email message over REST
function sendMail(toList, subject, mailContent) {
    var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail",
    restHeaders = {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Content-Type": "application/json;odata=verbose"
    },
    mailObject = {
        'properties': {
            '__metadata': {
                'type': 'SP.Utilities.EmailProperties'
            },
            'To': {
                'results': toList
            },
            //'From': 'user@contoso.onmicrosoft.com"',
            'FromDisplay': 'Display From"', // <--- I want a property like this one
            // Important Note: this property does not work in SharePoint Online.
            // the <from> field will always be "no-reply@sharepointonline.com"
            'Subject': subject,
            'Body': mailContent,
            "AdditionalHeaders":
                {
                    "__metadata":
                    { "type": "Collection(SP.KeyValue)" },
                    "results":
                    [
                        {
                            "__metadata": {
                                "type": 'SP.KeyValue'
                            },
                            "Key": "content-type",
                            "Value": 'text/html',
                            "ValueType": "Edm.String"
                        }
                    ]
                }
        }
    };
    return $.ajax({
        contentType: "application/json",
        url: restUrl,
        type: "POST",
        data: JSON.stringify(mailObject),
        headers: restHeaders
    });

} 

$(function(){
 sendMail(toList, subject, mailContent).then(function(data){console.log(data.d)})
})    
Denis Molodtsov
  • 9,602
  • 9
  • 43
  • 94

4 Answers4

1

i am currently working on a similar problem and you can do it like this if you want to display a SharePoint user:

"properties" : {
    "__metadata": {
        "type": "SP.Utilities.EmailProperties"
    },
    "From": "username@domain", // <-- this one must be a SharePoint user 
    "Subject": "TestMail",
    "To": {
        "results": to
    },
    "Body": "Hallo Welt"
}

the result in outlook is the display name of the SharePoint User and <no-replay@sharepointonline.com> like the image below

enter image description here

And in an answer the to field will be the user.

0

In SharePoint Online it is impossible to set the from property when using the SP.Utilities.Utility.SendEmail method.

The from: field will always be no-reply@sharepointonline.com, and the description will be SharePoint Online.

Denis Molodtsov
  • 9,602
  • 9
  • 43
  • 94
0

Thank to this answer, I found a way to change the From and the Reply also. I tested it with Sharepoint 2013 On-Promise:

{
  'properties': {
    '__metadata': {
      'type': 'SP.Utilities.EmailProperties'
    }
    'AdditionalHeaders': {
      "__metadata": {
        "type":"Collection(SP.KeyValue)"
      },
      "results": [
        {
          "__metadata": {
            "type": 'SP.KeyValue'
          },
          "Key": "From:",
          "Value": 'My Title <some.email@example.com>', // here you can change the From
          "ValueType": "Edm.String"
        },
        {
          "__metadata": {
            "type": 'SP.KeyValue'
          },
          "Key": "Reply-To:",
          "Value": 'email@example.com', // when the user will hit the Reply button, this email will be used
          "ValueType": "Edm.String"
        }
      ]
    },
    'To': {
      'results': [username]
    },
    'Body': body,
    'Subject': subject
  }
}
AymKdn
  • 1,189
  • 8
  • 20
0

I have not found a way to modify anything in the “From” field of an email when using the SendEmail REST API in SharePoint Online.

Testing

I’m including some code below so you can try this in your own environment. The testSendEmail function below takes the different headers you provide it and sends you emails with every combination (including the null combination) of those headers to see which make it through. This includes an example using the From:, Reply-To:, and Sender: headers.

async function testSendEmail(headers)
{
  const getDigest = async () => (
    (
      await (
        await fetch(
          _spPageContextInfo.siteAbsoluteUrl + "/_api/contextinfo",
          { headers: { Accept: "application/json;odata=verbose" }, method: "POST" }
        )
      ).json()
    ).d.GetContextWebInformation.FormDigestValue
  );
  const reduceCombinations = (result, [key, value]) =>
  {
    const mapCombination = (include) => (combo) => ({ ...combo, ...(include ? { [key]: value } : {})});
    return [...result.map(mapCombination(false)), ...result.map(mapCombination(true))];
  };
  const sendEmail = (digest) => (headers) =>
  {
    const mapHeader = ([key, value]) => ({
      "__metadata": {
        "type": "SP.KeyValue"
      },
      "Key": key,
      "Value": value,
      "ValueType": "Edm.String"
    });
    return fetch(
      _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail",
      {
        body: JSON.stringify({
          properties: {
            __metadata: { type: "SP.Utilities.EmailProperties" },
            AdditionalHeaders: {
              __metadata: { type: "Collection(SP.KeyValue)" },
              results: Object.entries(headers).map(mapHeader)
            },
            Body: `
              <p>Headers included:</p>
              <ul>${Object.keys(headers).map((key) => (`<li>${key}</li>`)).join("")}</ul>
            `,
            Subject: "SendEmail Test",
            To: { results: [_spPageContextInfo.userEmail] }
          }          
        }),
        headers: {
          Accept: "application/json;odata=verbose",
          "Content-Type": "application/json;odata=verbose",
          "X-RequestDigest": digest
        },
        method: "POST"
      }
    );
  };
  return Promise.all(Object.entries(headers).reduce(reduceCombinations, [{}]).map(sendEmail(await getDigest())));
}
testSendEmail({
  "From:": _spPageContext.userEmail,
  "Reply-To:": _spPageContext.userEmail,
  "Sender:": _spPageContext.userEmail,
  "X-SharePoint-Site": _spPageContextInfo.webAbsoluteUrl
}).then(console.log).catch(console.error);

Conclusion

If you include any of the three problematic headers—From:, Reply-To:, or Sender:—in a REST API call to SharePoint Online, all your additional headers are removed. The headers have the intended effect in SharePoint 2013 on-prem and SharePoint 2016 on-prem.

Paul Rowe
  • 168
  • 7