Search This Blog

Tuesday, January 1, 2019

SharePoint 2016- Send E-mail from JavaScript using REST API

Step 1: Add Script Editor on SharePoint Page, Copy and paste below scripts.


<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        //SUBJECT OF THE EMAIL
        //var subject = "SUBJECT OF THE MAIL";
        //CONTENT OF THE MAIL - CAN BE HTML OR PLAIN TEXT.
       // var mailContent = "<h3>Some Heading for the mail</h3><p>Content</p><div>Content</div>";
        //AN ARRAY HAVING THE RECIPIENT DETAILS.
        //THE USERNAME/EMAIL-ID SHOULD BE OF A VALID SHAREPOINT USER.
        //EXTERNAL USERS OR EXTERNAL EMAIL IDS ARE NOT SUPPORTED
       // var toList = ["DOMAIN\\USERNAME", "USER1-EMAIL-ID@DOMAIN.COM"]
             
        function processSendEmails() {
            var toList = [$("#txtUserName").val(), $("#txtRecipients").val()]
            var body = $("#txtBody").val();
            var subject = $("#txtSubject").val();
            var mailContent = "<h3>Some Heading for the mail</h3><p>" + body + "</p>";
            // Call sendEmail function      
            sendMail(toList, subject, mailContent).done(function (response) {
                alert("Mail Sent.");
            }).fail(function () {
                alert("Error while sending mail.");
            });

        }
        //Send-Email
        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
                },
                '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
        });
    }

    </script>

    <table>
        <tr>
            <td>User Name(with Domain) for ex : Exdoamin\JaisonA</td>
            <td>
                <input type="text" name="txtUserName" id="txtUserName" value="" /></td>
        </tr>
        <tr>
            <td>Email Id</td>
            <td>
                <input type="text" name="txtRecipients" id="txtRecipients" value="" />
            </td>
        </tr>
        <tr>
            <td>Subject :
            </td>
            <td>
                <input type="text" name="txtSubject" id="txtSubject" value="test mail from rest APi" />
            </td>
        </tr>
        <tr>
            <td>Body :
            </td>
            <td>
                <input type="text" name="txtBody" id="txtBody" value="Testing123" />
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input id="Button1" onclick="processSendEmails()" type="button" value="SendMail" /></td>
        </tr>
    </table>

No comments:

Post a Comment