1

(Update)Hi all i want to invite friend sent to mail using Nodejs. Mainly server side user and pass which can be use? and mail also can't sent then i tried many ways but unable to get the solution if any one knows the solution please help me..... myplunker

HTML:-

<div id="container">
<center>   
<input id="to" type="text" placeholder="Enter E-mail ID" /><br><br>
<input id="subject" type="text" placeholder="Write Subject" /><br><br>
<textarea id="content" cols="40" rows="5" placeholder="Message"></textarea><br><br>
<button id="send_email">Send Email</button>
</center>
<span id="message"></span>
</div>

Server:-

var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    use_authentication: true,
    auth: {
        user: "sample@gmail.com",
        pass: "password"
    }
});


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");
         }
});
});
  • to send email you need some server part – Alexan Jun 29 '16 at 14:23
  • I am new to angularjs if have the server part solution please help me... – Saravana Kumar Jun 29 '16 at 14:32
  • Maybe the easiest way will be to send it with PHP and it's own mail function. Check it [here](http://php.net/manual/es/function.mail.php) and [here](http://www.w3schools.com/php/func_mail_mail.asp). – Gerard Cuadras Jun 29 '16 at 14:38
  • @Saravana, angularjs is client part too. What you use for server? to access your mail sever you need, for example php, ASP.NET, node.js ... – Alexan Jun 29 '16 at 14:43
  • Thanking you every one for your commands, update my code server side also please help me...[myplunker](https://plnkr.co/edit/PnUhzp8xVNoNpCMWvkUC?p=preview) – Saravana Kumar Jun 30 '16 at 15:33
  • That still says nothing about whether or not you even HAVE a mail server running. – patricksweeney Jun 30 '16 at 15:39

1 Answers1

0

The Following steps may help you:

Add var nodemailer=require("nodemailer") to the top of your server script

Add var express=require("express"); var app=express() to the top of your server script

To Test that the email is being sent move what is in your app.get("/send") script outside the function and comment everything else (For Now) should Look similar to this:

var nodemailer=require("nodemailer");
var express=requre("express");
var app=express();
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    use_authentication: true,
    auth: {
        user: "email@domain.com",
        pass: "PASS"
    }
});

var mailOptions={
    to : "email@domain.com",
    subject :"SUBJECT",
    text : "MESSAGE"
}


console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(error, response){
 if(error){
        console.log(error);

 }else{
        console.log("Message sent: " + response.message);

     }
});

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

Make sure you have this turned on for the email you are trying to send an email with: https://www.google.com/settings/security/lesssecureapps

Make Sure your version of nodemailer is correct, It should be 0.71v for the setup you have: How to downgrade this node.js module to specific version and prevent automatic upgrade later?

Run the server script with a terminal: node fileName.js (If none of the tips help could you please copy the error stack?)

If everything works uncomment everything and delete the mailOptions and smtpTransport that are outside the app.get. Everything should work now...

Good Luck, And I'll be happy to help if you have any more problems.

Community
  • 1
  • 1
The Dark Knight
  • 695
  • 4
  • 10