簡體   English   中英

如何使用Parse和Mandrill發送電子郵件通知?

[英]How can I send email notifications with Parse and Mandrill?

我正在嘗試使用Mandrill向我的Web應用程序的用戶發送基於事件的電子郵件通知。 我在Back4App中使用Parse。

在本教程( https://docs.back4app.com/docs/integrations/parse-server-mandrill/ )中,托管服務提供商建議使用以下方法從Android應用程序中調用Mandrill雲代碼:

public class Mandrill extends AppCompatActivity {


 @Override

 protected void onCreate(Bundle savedInstanceState) {

   Parse.initialize(new Parse.Configuration.Builder(this)

    .applicationId("your back4app app id”)

     .clientKey(“your back4app client key ")

      .server("https://parseapi.back4app.com/").build()

     );


     Map < String, String > params = new HashMap < > ();

     params.put("text", "Sample mail body");

     params.put("subject", "Test Parse Push");

     params.put("fromEmail", "someone@example.com");

     params.put("fromName", "Source User");

     params.put("toEmail", "other@example.com");

     params.put("toName", "Target user");

     params.put("replyTo", "reply-to@example.com");

     ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback < Object > () {

      @Override

      public void done(Object response, ParseException exc) {

       Log.e("cloud code example", "response: " + response);

      }

     });


     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_mandrill);

    }

   }

如何使用Parse JavaScript SDK在JavaScript中實現此功能?

這是我到目前為止所做的,但不會發送電子郵件。 我已經設置了Mandrill,以及經過驗證的電子郵件域以及有效的DKIM和SPF。

// Run email Cloud code
Parse.Cloud.run("sendMail", {
    text: "Email Test",
    subject: "Email Test",
    fromEmail: "no-reply@test.ca",
    fromName: "TEST",
    toEmail: "test@gmail.com",
    toName: "test",
    replyTo: "no-reply@test.ca"
}).then(function(result) {
    // make sure to set the email sent flag on the object
    console.log("result :" + JSON.stringify(result));
}, function(error) {
    // error
});

我什至在控制台上都沒有得到結果,所以我認為雲代碼甚至沒有執行。

您必須將Mandrill電子郵件適配器添加到Parse Server的初始化中,如其Github頁面上所述 另請參閱《 Parse Server指南》以了解如何初始化或使用其示例項目

然后按照指南設置Cloud Code。 您將要使用您的Android應用程序或從任何Javascript應用程序調用Cloud Code函數,或者直接在Cloud Code中使用Parse Object的beforeSave或afterSave掛鈎,這使您可以在用戶注冊時發送歡迎電子郵件。 如果您想基於對象更新來實現基於行為的電子郵件,那可能會派上用場。 另外,由於它在服務器上而不在客戶端上,因此維護和擴展更加容易。

要使Cloud Code函數實際上通過Mandrill發送電子郵件,您需要向Cloud Code函數添加更多代碼。 首先,添加具有以下內容的文件:

var _apiUrl = 'mandrillapp.com/api/1.0';
var _apiKey = process.env.MANDRILL_API_KEY || '';

exports.initialize = function(apiKey) {
  _apiKey = apiKey;
};

exports.sendTemplate = function(request, response) {
  request.key = _apiKey;

  return Parse.Cloud.httpRequest({
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    url: 'https://' + _apiUrl + '/messages/send-template.json',
    body: request,
    success: function(httpResponse) {
      if (response) {
        response.success(httpResponse);
      }
      return Parse.Promise.resolve(httpResponse);
    },
    error: function(httpResponse) {
      if (response) {
        response.error(httpResponse);
      }
      return Parse.Promise.reject(httpResponse);
    }
  });
};

在您的Cloud Code文件中要求該文件,並像其他Promise一樣使用它。

var Mandrill = require("./file");
Mandrill.sendTemplate({
      template_name: "TEMPLATE_NAME",
      template_content: [{}],
      key: process.env.MANDRILL_API_KEY,
      message: {
        global_merge_vars: [{
          name: "REPLACABLE_CONTENT_NAME",
          content: "YOUR_CONTENT",
        }],
        subject: "SUBJECT",
        from_email: "YOUR@EMAIL.COM",
        from_name: "YOUR NAME",
        to: [{
          email: "RECIPIENT@EMAIL.COM",
          name: "RECIPIENT NAME"
        }],
        important: true
      },
      async: false
    })
    .then(
      function success() {

      })
    .catch(
      function error(error) {

      });

確保在Mailchimp上創建一個模板,右鍵單擊它,然后選擇“發送到Mandrill”,以便在通過API發送時可以使用該模板的名稱。

它涉及到一點,但是一旦設置,它就像一個魅力。 祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM