[英]How can my Parse.com app send an email using JavaScript?
我正在使用 Parse.com (JavaScript SDK),我希望用戶能夠從我的應用程序發送電子郵件。 基本上,他們使用該應用程序創建一個頁面,然后我需要允許他們輸入電子郵件地址列表; 然后,該應用程序將向每個地址發送一個指向他們創建的頁面的鏈接。
不過,我可以在文檔中找到任何告訴我如何發送電子郵件的內容。 我可以獲取電子郵件地址列表並生成電子郵件,但我不知道如何發送它。
這可以用 Parse 實現嗎?
我在這里創建了一個簡單的 iOS 示例,使用 Mandrill 和解析雲代碼http://www.stlplace.com/2013/11/24/send-email-via-cloud-code-in-parse/
這是@uudaddy 回答的安卓版本
public void sendMail(View view) {
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");
ParseCloud.callFunctionInBackground("sendMail", params, new FunctionCallback<Object>() {
@Override
public void done(Object response, ParseException exc) {
Log.e("cloud code example", "response: " + response);
}
});
}
服務端JS代碼(main.js)解析雲
Parse.Cloud.define("sendMail", function(request, response) {
var Mandrill = require('mandrill');
Mandrill.initialize('12AkxxxxxxxxxxxxxxrZEg');
Mandrill.sendEmail({
message: {
text: request.params.text,
subject: request.params.subject,
from_email: request.params.fromEmail,
from_name: request.params.fromName,
to: [
{
email: request.params.toEmail,
name: request.params.toName
}
]
},
async: true
},{
success: function(httpResponse) {
console.log(httpResponse);
response.success("Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});
});
有人可能會找到使用 Mailgun、iOS 和 Parse Cloud 的有用示例。
我決定使用 Mailgun,因為 Mandril 目前只有 4k 免費郵件。
請注意,您必須有權訪問您的域才能設置“TXT”和“CNAME”記錄以證明 Mailgun 您是域的所有者。
雲代碼:
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});
Parse.Cloud.define("mailSend", function(request, response) {
var Mailgun = require('mailgun');
Mailgun.initialize('DOMAIN_NAME', 'API_KEY');
Mailgun.sendEmail({
to: request.params.target,
from: request.params.originator,
subject: request.params.subject,
text: request.params.text
}, {
success: function(httpResponse) {
console.log(httpResponse);
response.success("Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});
});
現在在你的 ObjC 項目中的某個地方:
[PFCloud callFunctionInBackground:@"mailSend"
withParameters:@{
@"target": @"target@mail.com",
@"originator": @"from@mail.com",
@"subject": @"Hey There",
@"text": @"This is your iOS originated mail"
}
block:^(NSString *result, NSError *error){
NSLog(@"error %@", error);
NSLog(@"result %@", result);
}];
沒有本地方法可以做到這一點。 最好的辦法是等到 Parse 的 Cloud Code 支持 3rd-party HTTP 請求。 我制作了一個快速模型,說明如何使用 IronWorker + Ruby 發送電子郵件來完成此操作,但您當然可以使用其他語言:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.