我的 Parse.com 应用程序如何使用 JavaScript 发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12620318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can my Parse.com app send an email using JavaScript?
提问by Sharon
I'm using Parse.com (JavaScript SDK), and I want users to be able to send an email from my app. Basically, they create a page using the app, and then I need to allow them to enter a list of email addresses; the app will then send each address a link to the page they've created.
我正在使用 Parse.com (JavaScript SDK),我希望用户能够从我的应用程序发送电子邮件。基本上,他们使用该应用程序创建一个页面,然后我需要允许他们输入电子邮件地址列表;然后,该应用程序将向每个地址发送一个指向他们创建的页面的链接。
I can find anything in the documentation which tells me how to send the email, though. I can take the list of email addresses and generate the email, I just can't figure out how to send it.
不过,我可以在文档中找到任何告诉我如何发送电子邮件的内容。我可以获取电子邮件地址列表并生成电子邮件,但我不知道如何发送它。
Is this possible with Parse?
这可以用 Parse 实现吗?
回答by nicko
回答by uudaddy
I created a simple iOS example here, using Mandrill, and Parse Cloud Code http://www.stlplace.com/2013/11/24/send-email-via-cloud-code-in-parse/
我在这里创建了一个简单的 iOS 示例,使用 Mandrill 和解析云代码 http://www.stlplace.com/2013/11/24/send-email-via-cloud-code-in-parse/
回答by silentsudo
Here is the android version for @uudaddy's answer
这是@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", "[email protected]");
params.put("fromName", "Source User");
params.put("toEmail", "[email protected]");
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);
}
});
}
Server side JS Code(main.js) Parse Cloud
服务端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");
}
});
});
回答by hris.to
Someone might find useful example using Mailgun, iOS and Parse Cloud.
有人可能会找到使用 Mailgun、iOS 和 Parse Cloud 的有用示例。
I decided to went with Mailgun as Mandril currently had only 4k free mails.
我决定使用 Mailgun,因为 Mandril 目前只有 4k 免费邮件。
Please note that you had to have access to your domain in order to setup 'TXT' and 'CNAME' records prove Mailgun you are the owner of the domain.
请注意,您必须有权访问您的域才能设置“TXT”和“CNAME”记录以证明 Mailgun 您是域的所有者。
Cloud code:
云代码:
// 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");
}
});
});
And now somewhere in your ObjC project:
现在在你的 ObjC 项目中的某个地方:
[PFCloud callFunctionInBackground:@"mailSend"
withParameters:@{
@"target": @"[email protected]",
@"originator": @"[email protected]",
@"subject": @"Hey There",
@"text": @"This is your iOS originated mail"
}
block:^(NSString *result, NSError *error){
NSLog(@"error %@", error);
NSLog(@"result %@", result);
}];
回答by user94154
There is no native method to do this. your best bet is to wait until Parse's Cloud Code supports 3rd-party HTTP requests. I made a quick mockup of how you could accomplish this using IronWorker + Ruby to send the email, but you could certainly use other languages:
没有本地方法可以做到这一点。最好的办法是等到 Parse 的 Cloud Code 支持 3rd-party HTTP 请求。我制作了一个快速模型,说明如何使用 IronWorker + Ruby 发送电子邮件来完成此操作,但您当然可以使用其他语言: