0

I can send out email with HTML contents using the below code. But the a tag href links are being received as "[google.com]Link", instead of it being a clickable "Link".

    var data = {}; data.properties = {};
    data.properties.__metadata = { "type": "SP.Utilities.EmailProperties" };
    data.properties.To = { "results": [receipient] };
    data.properties.Subject = "Test email";
    data.properties.Body =
        "<h3>Some Heading for the mail</h3>" +
        "<p>Content</p><div>Content</div>" +
        "<a href='google.com'>Link</a><br/>";
    data.properties.AdditionalHeaders = {
        "__metadata": {
            "type": "Collection(SP.KeyValue)"
        },
        "results": [{
            "__metadata": {
                "type": "SP.KeyValue"
            },
            "Key": "content-type",
            "Value": "text/html",
            "ValueType": "Edm.String"
        }]
    }

    var xmlHttp = new XMLHttpRequest();
    var appWebUrl = GetUrlKeyValue("SPAppWebUrl", false, document.URL, false); 
    var url = appWebUrl + "/_api/SP.Utilities.Utility.SendEmail";
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("accept", "application/json;odata=verbose");
    xmlHttp.setRequestHeader("content-type", "application/json;odata=verbose");
    xmlHttp.setRequestHeader("X-RequestDigest", document.getElementById("__REQUESTDIGEST").value);
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState != 4) { return; }
        var res = JSON.parse(xmlHttp.responseText);
        callback(null, res);
    }
    xmlHttp.send(JSON.stringify(data));

Edit: To get above working, all I have to do is change "google.com" to "https://www.google.com"

1

Check out this code. It sends mail with clickable link in body. Note : Edit from and to variables.

function processSendEmails() {

    var from = 'abc@domain.com',
        to = 'xyz@domain.com',
        body = '<a href="https://www.google.com">Test Visit google!</a>',
        subject = 'Hello World Subject, this is cool!!';

    // Call sendEmail function
    //
    sendEmail(from, to, body, subject);
}

function sendEmail(from, to, body, subject) {

    //Get the relative url of the site

    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] },
                     'Body': body,
                     'Subject': subject,
                      "AdditionalHeaders":
                      {"__metadata":
                          {"type":"Collection(SP.KeyValue)"},
                          "results":
                          [ 
                              {               
                                  "__metadata": {"type": 'SP.KeyValue'},                                                                                               
                                  "Key": "X-Message-Flag",
                                  "Value": 'Follow up',
                                  "ValueType": "Edm.String"
                             },
                              {               
                                  "__metadata": {"type": 'SP.KeyValue'},                                                                                               
                                  "Key": "Reply-By",
                                  "Value": 'Wed, 23 Nov 2016 13:30:00 +0530',
                                  "ValueType": "Edm.DateTime"
                             }                            
                          ]
                      }
                }


        }),
        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) {
            alert('Error in sending Email: ' + JSON.stringify(err));
        }
    });
}

Usage:

$(document).ready(function () {

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', processSendEmails);

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.