jeudi 30 juillet 2015

Simple email sender with NodeJS and Express

I'm totally new to web development and working with web related concepts and frameworks (IPs, networks, routers, I cringe everytime :D ).

But, thanks to some internship work I have "forced myself" to work with this and thanks to lots of digging on the internet I have managed to develop a super simple app that can send emails if I am under my localhost. But, I have some (many) questions, but first, the files that comprise all of my code:

Package.json file

{
"name": "email-node",
"version": "1.0.0",
"dependencies": {
"nodemailer": "~0.7.1",
"express": "~4.5.1"
}
}

Index.html

<html>
<head>
<title>Node.JS Email application</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script>// <![CDATA[
$(document).ready(function(){
    var from,to,subject,text;
    $("#send_email").click(function(){     
        to=$("#to").val();
        subject=$("#subject").val();
        text=$("#content").val();
        $("#message").text("Sending E-mail...Please wait");
        $.get("http://localhost:3000/send",{to:to,subject:subject,text:text},function(data){
        if(data=="sent")
        {
            $("#message").empty().html("Email is been sent at "+to+" . Please check inbox !");
        }

});
    });
});
</script>
</head>
<body>
<div id="container">
<h1>Mailer In Node.JS</h1>
<input id="to" type="text" placeholder="Enter E-mail ID where you want to send" />
<input id="subject" type="text" placeholder="Write Subject" />
<textarea id="content" cols="40" rows="5" placeholder="Write what you want to send"></textarea>
<button id="send_email">Send Email</button>
<span id="message"></span>
</div>
</div>

app.js File

var express=require('express');
var nodemailer = require("nodemailer");
var app=express();
/*
Here we are configuring our SMTP Server details.
STMP is mail server which is responsible for sending and recieving email.
*/
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "MYGMAIL@gmail.com",
pass: "MYGMAILPASS"
}
});
/*------------------SMTP Over-----------------------------*/

/*------------------Routing Started ------------------------*/

app.get('/',function(req,res){
res.sendfile('index.html');
});
app.get('/send',function(req,res){
var mailOptions={
to : req.query.to,
subject : req.query.subject,
text : req.query.text
}
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
res.end("error");
}else{
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});

/*--------------------Routing Over----------------------------*/

app.listen(3000,function(){
console.log("Express Started on Port 3000");
});

From what I have read here is what I concluded:

Node.js + Express applications always "listen" to incoming connections on port 3000 (regardless of the IP? Or is this a stupid thing to ask?) so that is the value I use.

Ideally, to send stuff from a website instead of my localhost I believe that the problem lies on the index.html file.

Specifically, this line:

$.get("http://localhost:3000/send",{to:to,subject:subject,text:text},function(data){

where instead of using http://localhost:3000/send I would use something like:

http://ift.tt/1SOtM3o

I have read countless forums and posts, tried everything from:

$.get("/send",{to:to,subject:subject,text:text},function(data){

to searching for github servers IP adress range ( http://ift.tt/1MzpjCK ) to try and use them directly there and nothing seems to work...

(The application is supposedly host here:

http://ift.tt/1SOtM3q

)

Can someone help me?

Best,

Bruno




Aucun commentaire:

Enregistrer un commentaire