在 React Native 应用程序中与 mailchimp 集成

Integration with mailchimp in React Native Application

我是 React Native 的初学者。我基本上是在尝试在 React Native 和 MailChimp 中执行我的应用程序之间的集成,我想做的是:从用户向我们提供他们的电子邮件并发送表单的那一刻起,然后通过 MailChimp 为其触发一封电子邮件,我使用Firebase创建自己的邮箱库,但是我想通过mailchimp自动发送任务,我在firebase中保存邮件的方法如下:

saveEmail() {
    var id = firebase.database().ref().child('xxx').push().key;
    const valueEmail = this.state.input;
    const valueName = this.state.name;

    firebase.database().ref('/xxx/' + id).set({
        email: valueEmail,
        name: valueName
    });

    //here where the post for the mailchimp API will be carried out

    this.setState(
        {
            isModalVisible: !this.state.isModalVisible,
            valid: false,
            name: '',
            input: ''

        }
    );
}

谢谢大家!

这里很安静吗? 在一些研究和想法的中间,一个可能的问题解决方案任务,下面是为 React Native 应用程序执行 MailChimp 和 Firebase 之间的集成。

首先,根据一些研究,它被视为服务器端数据库,因为我已经在使用 Firebase 实时数据库,然后我决定将它用作 Cloud Functions 来解决这个问题,方法是Cloud Function,它正在检查数据库的状态,当它被提供时,一个 POST 到 MailChimp API 被触发,这里是为此创建的一个函数:

const functions = require('firebase-functions');

\Code suppressed here

exports.onDataAdded = functions.database.ref('/xxx/{id}').onCreate((snap, context) => {
    const data = snap.val();
    var fetch = require('isomorphic-fetch');
    var btoa = require('btoa');

    console.log(data.email);
    // POST /lists/{list_id}/members
    // Add a new list member

    var MAILCHIMP_API_KEY = 'Key Here';
    var listId = 'Id Here';
    // NOTE: mailchimp's API uri differs depending on your location. us6 is the east coast. 
    var url = 'https://<dc>.api.mailchimp.com/3.0/lists/' + listId + '/members';
    var method = 'POST';
    var headers = {
        'authorization': "Basic " + btoa('randomstring:' + MAILCHIMP_API_KEY),
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    };
    var body = JSON.stringify(
        {
            email_address: data.email,
            status: 'subscribed',
            'language': data.language,
            merge_fields: { FNAME: data.name }
        }
    );

    return fetch(url, {
        method,
        headers,
        body
    }).then(resp => resp.json())
        .then(resp => {
            return console.log(resp)
        })
        .catch(error => {
            throw new Error(error.statusText)
        });
});

使用在 Firebase 的 Cloud Functions 中创建的这个函数,从插入实时数据库的那一刻起,就会触发触发这个函数的触发器。

感谢您的快速回复!!

您也可以使用 Firebase mailchimp 扩展 https://firebase.google.com/products/extensions/mailchimp-mailchimp-firebase-sync