Chris
Chris

Reputation: 1304

Couldn't send mail with NodeMailer and Angular 2+

I couldn't send mail with nodemailer and angular 2+. It is showing request fail error.

HTML

 <form class="example-form" (ngSubmit)="uptpwd()">  
     <input class="form-control lht" placeholder="Email" type="email" name="email" [(ngModel)]="mailData.to"> 
        <br/> 
        <br/> 
     <button class="lgbtn" type="submit">Enter</button> 
 </form>

TS

export class ForgotComponent implements OnInit {
 to: string = '';
 mailData = {
   to: this.to,
   subject: 'testing email',
   text: 'This is just a testing email send by the phpsollution.blogspot.com',
 }

 ngOnInit() {}

 uptpwd(mailData) {
  this.auth.uptpwd(this.mailData).subscribe();
 }
}

Service

   uptpwd(maildata) {
    return this.http.post('http://localhost:3000/sendemail', maildata)
              .map(res => res.json());
   }

Server

app.post('/sendemail',function(req,res){
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
    user: 'fe***@gmail.com',
    pass: '******'
}
});

var attachment = (typeof req.body.attachment !="undefined") ? req.body.attachment : '';
var mailOptions = {
     from: "f******@gmail.com",
     to: req.body.to,
     subject: req.body.subject,
     html: req.body.text,


  } 

  smtpTransport.sendMail(mailOptions, function(error, info){
     if(error){
    res.json({status: 'error'});
   }else{
    res.json({status: 'success'});
   };
  });
 });

Error

POST http://localhost:3000/sendmail 500 (Internal Server Error)

Kindly help me. Tell me if I missed anything.

Upvotes: 0

Views: 723

Answers (1)

Chau Tran
Chau Tran

Reputation: 5098

I am going to assume you already have your backend running at localhost:3000. You need to set up headers in your HttpRequest.

uptpwd(maildata) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({headers: headers});
    return this.http.post('http://localhost:3000/sendemail', maildata, options)
              .map(res => res.json());
   }

Upvotes: 1

Related Questions