SharePoint 2013通过REST / JS从联系我们表单发送电子邮件

时间:2015-11-25 22:27:03

标签: javascript forms rest sharepoint sharepoint-2013

并提前感谢您的回答。

我正在尝试从联系我们表单发送电子邮件。它似乎有效,但我没有收到任何电子邮件。我没有任何REST经验,并希望有人能够发现他们是否能发现任何问题。

这是在SharePoint 2013企业发布站点上。

出于隐私目的,我更改了一些变量和ID。

HTML采用自定义页面布局,并且在jQuery之后,在同一页面布局中成功调用JS。

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>

1 个答案:

答案 0 :(得分:1)

你的代码有一个错误:

我将To: toAddress更改为To: { 'results': [toAddress] }

现在一切正常,我也收到了电子邮件。

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