0

I'm trying to send an mail from my app using a Node.js server. 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? I'm writing my app in 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. What would be the correct code for the server to handle the JSON object (send the email) and return 'Success' or 'Fail'? How can I use the JSON object in the server code?

Amer Hukic
  • 1,494
  • 1
  • 19
  • 29
  • You need to show the code you've tried for connecting the the web service presented by the server and explain what didn't work – Wain Jan 29 '15 at 12:36
  • The thing is, I'm a complete beginner in iOS and client-server communication in general. I need an advice on where to start and how to connect to the server. Can you just put me in the right direction on how I can communicate with the server (what functions/api/etc can I use)? – Amer Hukic Jan 29 '15 at 12:42
  • You should really google search for a tutorial on connecting to a web service from iOS and work from there, then bring specific problems to SO – Wain Jan 29 '15 at 12:48
  • @Wain Could you take a look at my code now? I just need to know how the server handles the recieved JSON object? – Amer Hukic Feb 01 '15 at 16:49
  • Looks good for the send. Not sure what you mean about the server but that is a plain node question. – Wain Feb 01 '15 at 16:56
  • I would want to run a Node server on localhost that would accept the data sent from my app (and that data is an email address and content of the email) and use that data to send the email to some other address. I just don't know how the node.js server receives that json object. – Amer Hukic Feb 01 '15 at 16:59
  • So you need to look at something like http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js – Wain Feb 01 '15 at 18:12

2 Answers2

2
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) {}];
1

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,

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!'
});
Danny
  • 793
  • 1
  • 9
  • 20