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 [email protected], 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 = ["[email protected]"]

//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': '[email protected]"',
            '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 "[email protected]"
            '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)})
})    

4 Answers 4

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 <[email protected]> like the image below

enter image description here

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

3
  • you are probably using SharePoint on-premises, right? In this case you can use the "from" field. in SharePoint Online it's ignored. Jan 20, 2017 at 15:39
  • No, I am using SharePoint online. Jan 21, 2017 at 20:23
  • so does it work? When you look at the message in Outlook, what does the "From" field say? Jan 23, 2017 at 1:31
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 <[email protected]>', // here you can change the From
          "ValueType": "Edm.String"
        },
        {
          "__metadata": {
            "type": 'SP.KeyValue'
          },
          "Key": "Reply-To:",
          "Value": '[email protected]', // when the user will hit the Reply button, this email will be used
          "ValueType": "Edm.String"
        }
      ]
    },
    'To': {
      'results': [username]
    },
    'Body': body,
    'Subject': subject
  }
}
1
  • I'm afraid the question was about SharePoint Online. This won't work in SPO. Nov 6, 2019 at 19:06
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.

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 [email protected], and the description will be SharePoint Online.

2
  • This changed recently, instead of the Title of the web site, now the display name and address is: SharePoint Online <[email protected]> Dec 14, 2023 at 10:04
  • 1
    thank you, @AlexFilipovici. I updated the answer based on your feedback. Dec 14, 2023 at 19:31

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.