使用Facebook API向多个收件人发送邮件

Max*_*nko 4 javascript facebook dialog send web

是否有任何其他方式将邮件发布到多个收件人.我们试图整合逻辑,这里描述的Facebook发送对话框使用收件人阵列给多个朋友, 但它现在看起来不起作用.它只允许向ID列表中的第一个收件人发送信息.

谢谢你的帮助.

小智 5

我找到了将Facebook消息发送给多个朋友的解决方法.

每个Facebook用户自动获得@ facebook.com电子邮件地址.该地址与公共用户名或公共用户ID相同.

因此,您只需将常规电子邮件发送到此电子邮件地址即可.邮件将像普通邮件一样显示在Facebook收件箱中.

将连接的用户电子邮件用作发件人非常重要,否则无法使用.

这是一个让所有朋友通过电子邮件发送地址并调用Web服务的示例.

<div id="fb-root"></div>
<script type="text/javascript" src="https://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
    FB.init({
        appId: '#APP_ID#',
        status: true,
        cookie: true,
        xfbml: true
    });

    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {
            GetData();
        } else {
            Login();
        }
    });

    function Login() {
        FB.login(function (response) {
            if (response.authResponse) {
                GetData();
            }
        }, { scope: 'email' });
    }

    function GetData() {
        //Get user data
        FB.api('/me', function (response) {
            //Sender
            var sender = response.email;

            //Get friends
            FB.api('/me/friends', function (response) {

                //Recepients array
                var recipients = [];
                var length = response.data.length;
                var counter = 0;

                //Loop through friends
                for (i = 0; i < length; i++) {
                    var id = response.data[i].id;

                    //Get friend data
                    FB.api('/' + id, function (response) {
                        var recipient = "";

                        //User got a username, take username
                        if (response.username) {
                            recipient = response.username + '@facebook.com';
                        }
                        //No username, take id
                        else {
                            recipient = response.id + '@facebook.com';
                        }

                        //Add e-mail address to array
                        recipients.push(recipient);

                        counter++;
                        //last email -> send
                        if (counter == length) {
                            SendEmail(sender, recipients);
                        }
                    });
                }
            });
        });
    }

    function SendEmail(sender, recipients) {
        //Call webservice to send e-mail e.g.
        $.ajax({ type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            url: '#WEBSERVICE#',
            data: '{ sender:"' + sender + '", recipients: ["' + recipients.join('","') + '"] }',
            success: function (response) {
                //do something
            }
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)