我正在使用Parse.com(JavaScript SDK),并且希望用户能够从我的应用程序发送电子邮件。基本上,他们使用该应用程序创建页面,然后我需要允许他们输入一系列电子邮件地址;然后将向每个地址发送一个链接以便访问他们所创建的页面。
我在文档中找不到任何关于如何发送电子邮件的信息。我可以获取电子邮件地址列表并生成电子邮件,但我无法弄清如何发送它。
在Parse中是否可能实现这一点?
我正在使用Parse.com(JavaScript SDK),并且希望用户能够从我的应用程序发送电子邮件。基本上,他们使用该应用程序创建页面,然后我需要允许他们输入一系列电子邮件地址;然后将向每个地址发送一个链接以便访问他们所创建的页面。
我在文档中找不到任何关于如何发送电子邮件的信息。我可以获取电子邮件地址列表并生成电子邮件,但我无法弄清如何发送它。
在Parse中是否可能实现这一点?
我在这里创建了一个简单的iOS示例,使用Mandrill和Parse Cloud Code。 http://www.stlplace.com/2013/11/24/send-email-via-cloud-code-in-parse/
有人可能会发现使用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);
}];
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");
}
});
});
目前没有本地方法来实现这个。你最好等待Parse的云代码支持第三方HTTP请求。我做了一个快速模型,使用IronWorker + Ruby发送电子邮件,但你也可以使用其他语言: