我无法使用nodemailer和angular 2+发送邮件。它显示请求失败错误。
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();
}
}
服务
uptpwd(maildata) {
return this.http.post('http://localhost:3000/sendemail', maildata)
.map(res => res.json());
}
服务器
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'});
};
});
});
错误
POST http://localhost:3000/sendmail 500(内部服务器错误)
请帮助我。告诉我,如果我错过了什么。
答案 0 :(得分:1)
我假设你的后端已经在localhost:3000
运行了。您需要在HttpRequest中设置headers
。
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());
}