简体   繁体   English

使用Node.js服务器从iOS应用发送文本(电子邮件)

[英]Send text(email) from iOS app using Node.js server

I'm trying to send an mail from my app using a Node.js server. 我正在尝试使用Node.js服务器从我的应用程序发送邮件。 I would want to send the 'from' 'to' and 'content' of the email to the server and he would then send the email to the person. 我想将电子邮件的“从”,“至”和“内容”发送到服务器,然后他将电子邮件发送给该人。

I have found Nodemailer to use for sending emails, but how can I send the data (from,to,content) from my app to the server? 我发现Nodemailer可用于发送电子邮件,但是如何将数据(从,到,内容)从我的应用程序发送到服务器? I'm writing my app in Swift. 我正在用Swift编写我的应用程序。

EDIT: 编辑:
Here is my code for the app: 这是我的应用程序代码:

func post(params: Dictionary<String,String>, url:String) {
    var request = NSMutableURLRequest(URL: NSURL(string: url)!)
    var session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"

    var err:NSError?
    var jsonObject = NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted, error: &err)

    request.HTTPBody = jsonObject
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: { (data,response,error)-> Void in

        println(response)

    })
    task.resume()

}

I call this function with parameters like Email, From, To, and Content and send them to the Nodejs server. 我使用电子邮件,发件人,收件人和内容等参数调用此函数,然后将它们发送到Nodejs服务器。 What would be the correct code for the server to handle the JSON object (send the email) and return 'Success' or 'Fail'? 服务器处理JSON对象(发送电子邮件)并返回“成功”或“失败”的正确代码是什么? How can I use the JSON object in the server code? 如何在服务器代码中使用JSON对象?

in Node 

/*Config mailer*/
    var nodemailer = require('nodemailer');
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'xxx@gmail.com',
        pass: 'yyyy'
      }
    });


Create Function 

    function MSGEmail(MSG) {
      // send the message and get a callback with an error or details of the message that was sent
      transporter.sendMail({
        from: 'sender@address',
        to: 'xx@gmail.com',
        cc: 'yy@gmail.com',
        subject: 'Auto Email From server',
        text: MSG
      });
    }



    //Register Event

    client.on('SEND_MSG', function(data, callback) {
      sendEmail(data); //handle msg and pass to funcation
      callback();
    });

    // iOS (ObjC)


    [APPCONTEXT.socketIOHandler.socketIO sendEvent: KEY_SEND_MSG withData: dictionary
      andAcknowledge: ^ (id argsData) {
        if (argsData) {}];

Nodemailer has an API that shows you how to deal with thier module, all you have to do is just proividing these details (from,to,content) to the server side and handle it this way, Nodemailer有一个API,向您展示如何处理该模块,您所要做的只是向服务器端提供这些详细信息(从,到,到内容)并以这种方式进行处理,

var nodemailer = require('nodemailer');
// in case your using gmail as your mail serivce.
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'sender@gmail.com',
        pass: 'password'
    }
});
transporter.sendMail({
    from: 'sender@address',
    to: 'receiver@address',
    subject: 'hello',
    text: 'hello world!'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM