SharePoint Emails — Setting the email FROM field pt. 1

Changing the from field on emails coming from SharePoint is one of the most requested things I’ve tripped across and while it’s certainly possible to achieve I haven’t found a good blog post showing or explaining how to do it. This post and the one to follow will detail two solutions to accomplish that: the first through javascript using the REST API and the second in a SharePoint 2013 workflow using the call http function.

REST API

First, I found this solution here:
http://sharepoint.stackexchange.com/questions/148226/send-e-mail-from-javascript-using-rest-api

function sendEmail(to, cc, bcc, from, body, subject) {
     var siteurl = _spPageContextInfo.webServerRelativeUrl;
     var urlTemplate = siteurl + "/_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]
                 },
                 'CC': {
                     'results': [cc]
                 },
                 'BCC': {
                      'results': [bcc]
                 },
                 'Body': body,
                 'Subject': subject
              }
          }),
          headers: {
              'Accept': 'application/json;odata=verbose',
              'content-type': 'application/json;odata=verbose',
              'X-RequestDigest': jQuery('#__REQUESTDIGEST').val()
          },
          success: function(data){
              //alert('Email sent successfully');
          },
          error: function(data){
              alert('Error in sending email: ' + JSON.stringify(data));
          }
     });
}

Calling it is pretty easy.

sendEmail(to, cc, bcc, from, body, subject);

Keep in mind that to, cc, bcc and from must be a user like name@yourdomain.com, while body can contain html and subject must be a string.

The next post will show how to achieve the same thing though a 2013 workflow.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s