none
How to send email by javascript in SharePoint Online 2013 RRS feed

  • General discussion

  • Hi everyone,

    I have one case that need to create an sending email function by javascript. There is a button in a form, once I click this button it will call the sending email function to send email to specific users.

    In SharePoint farm solution, I can use SPUtility.SendEmail method (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail.aspx) to do this. How can I do this in SharePoint Online?

    Thanks for your support.

    Duy Vo

    Thursday, September 4, 2014 4:27 PM

All replies

  • Hi,

    There is no direct solution to send the mail in SharePoint Online.  Alternate way is to create a custom list containing at least three text columns: one to store each of the email's address, subject and body. Create the workflow and associate with list.

    For your solution, when the button is clicked, you add an item in the list, which will in turn trigger workflow.

    For more details, please refer to the following Tip.

    Guru Tip: Sending an Email in Office 365

    Please mark it answered, if your problem resolved. 

    Thursday, September 4, 2014 5:00 PM
  • You can use the REST API from your javascript to send an email. The code below shows an example that runs in a SharePoint Online app.

    function sendEmail() {
    
        appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
        hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
        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': 'smc@tenant.onmicrosoft.com',
                    'To': { 'results': ['steve@whatever.com'] },
                    'Body': 'Hello',
                    'Subject': 'remember'
                }
            }
          ),
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                var result = data.d.results;
                var i = result.length;
            },
            error: function (err) {
                alert(JSON.stringify(err));
            }
        });
    }
        


    Blog | SharePoint Field Notes Dev Tools | SPFastDeploy | SPRemoteAPIExplorer



    • Edited by Steve.Curran Thursday, September 4, 2014 9:59 PM
    Thursday, September 4, 2014 9:58 PM
  • This one worked perfectly for me.
    Wednesday, December 13, 2017 10:32 AM