Send Email from SharePoint Online through JSOM + JavaScript/JQuery

Sending Email from SharePoint Online JavaScript

You need to add following script into an script editor and you can send email though.

<script type="text/javascript">
    function processSendEmails() {

        var from;
        from = $("#txtFrom").val();
        var to;
        to = $("#txtRecipients").val();
        var body;
        body = $("#txtBody").val();
        var subject;
        subject = $("#txtSubject").val();
        sendEmail(from, to, body, subject);
    }


    function sendEmail(from, to, body, subject) {
        debugger;
        // Get the relative url of the site
        var siteurl = _spPageContextInfo.webAbsoluteUrl;
        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]
                    },
                    '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 (err) {
                debugger;
                alert('Error in sending Email:' + JSON.stringify(err));
            }
        });
    }</script>

Recipients: <br />
<input type="text"
       name="txtRecipients"
       id="txtRecipients"
       value="[EMAIL or USER DISPLAY NAME]">
<br />
<br />
from:
<br />
<input type="text"
       name="txtFrom"
       id="txtFrom"
       value="[EMAIL or USER DISPLAY NAME]"> <br />
<br />
Subject:
<br />
<input type="text"
       name="txtSubject"
       id="txtSubject"
       value="test mail from JS"> <br />
<br />
Body:
<br />
<input type="text"
       name="txtBody"
       id="txtBody"
       value="email body ..."> <br />
<br />
<a onclick=" processSendEmails(); " href="#">Send </a>

Search This Blog

Link Within Related Posts Plugin for WordPress, Blogger...