使用NodeJS和Express的简单电子邮件发件人

我完全不熟悉web开发和web相关的概念和框架(IP,networking,路由器,我每次都畏惧:D)。

但是,由于一些实习工作,我“强迫自己”与这个工作,并感谢大量挖在互联网上,我设法开发一个超级简单的应用程序,可以发送电子邮件,如果我在我的本地主机。 但是,我有一些(很多)问题,但首先,包含我所有代码的文件:

Package.json文件

{ "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> </body> </html> 

app.js文件

 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"); }); 

从我在这里读到的是我的结论:

Node.js + Express应用程序始终“监听”端口3000上的传入连接,这是我使用的值。

理想情况下,从网站而不是我的本地主机发送的东西,我认为问题在于index.html文件。

具体来说,这一行:

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

而不是使用http:// localhost:3000 /发送我会使用类似于:

http://bruno-oliveira.github.io:3000/send

我读过无数的论坛和post,尝试了一切:

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

searchgithub服务器的IP地址范围( https://developer.github.com/v3/meta/ )尝试直接使用它们,似乎没有任何工作…

应用程序被认为是在这里举办:

http://bruno-oliveira.github.io/

有人能帮我吗?

最好,

布鲁诺

只要说我设法设置这个,并使用heroku平台来运行:

Heroku的

你也应该尝试一下。