简体   繁体   中英

SharePoint 2013 Send Email from Contact Us Form via REST/JS

and thanks in advance for your answers.

I am trying to send an email from a contact us form. It appears to work but I don't receive any emails. I don't have any experience with REST and would like someone who does to see if they can spot any problems.

This is on a SharePoint 2013 Enterprise Publishing Site.

I have changed some variables and IDs for privacy purposes.

The HTML is in a custom page layout, and the JS is being called successfully in the same page layout after jQuery.

JS:

$.noConflict();
jQuery( document ).ready(function( $ ) {
  // Code that uses jQuery's $ can follow here.
    function submitForm() {
        var toAddress = "email@domain.com";
        var fromAddress ="email@domain.com";
        var emSubject = "Public Contact Us Form Entry";

        var lblName = "Name: ";
        var valName = document.getElementById('form-name').value;
        var lblEmail = "Email: ";
        var valEmail = document.getElementById('form-email').value;
        var lblMessage = "Message: ";
        var valMessage = document.getElementById('form-message').value;
        var emBody = lblName.concat(valName,lblEmail,valEmail,lblMessage,valMessage);

        var data = {
            properties: {
                __metadata: { 'type': 'SP.Utilities.EmailProperties' },
                From: fromAddress,
                To: toAddress,
                Body: emBody,
                Subject: emSubject
            }            
        }

        var urlTemplate = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";

        $.ajax({
            contentType: 'application/json',
            url: urlTemplate,
            type: "POST",
            data: JSON.stringify(data),
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                alert('User(s) notified')
            },
            error: function (err) {
                alert("Unable to send email -- " + JSON.stringify(err));
            }
        });
    }
});

HTML:

<div class="label">Name</div>
<input name="Name" type="text" id="form-name" size="40">
<div class="label">Email</div>
<input name="E-mail" type="text" id="form-email" size="40">
<div class="label">Message</div>
<textarea name="Message" cols="55" rows="5" id="form-message"></textarea>
<div class="form-button">
    <button onclick='submitForm()'>Submit</button>
</div>

You code has one mistake:

I changed To: toAddress to To: { 'results': [toAddress] } .

Now every thing is working fine and I am also getting the emails.

var data = {
        properties: {
            __metadata: { 'type': 'SP.Utilities.EmailProperties' },
            From: fromAddress,
            To: { 'results': [toAddress] } ,
            Body: emBody,
            Subject: emSubject
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM