使用 Angular5 前端在 NodeJS 中使用 nodemailer 发送邮件
Send mail with nodemailer in NodeJS with Angular5 front-end
我想在注册用户时从我的 nodejs 服务器发送一封电子邮件。我使用 Angular5 作为前端。它没有 return 任何错误,但它也不起作用。就像我的函数根本没有被调用一样。这是我尝试过的(在这个例子中电子邮件不是真实的):
server.js
app.post('/sendMails', function(req,res) {
console.log("WENT INTO SEND MAILS!!!")
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'someemail@gmail.com',
pass: process.env.MAIL_PASSWORD
},
tls: {
rejectUnauthorized: false
},
});
let HelperOptions = {
from: '"Babababa" <someemail@gmail.com>',
to: 'someemail@gmail.com',
subject: 'Hello world',
text: 'hey dude'
};
transporter.sendMail(HelperOptions, (error, response) => {
if(error) {
return console.log(error);
res.json({error: "API Error"});
}
else{
console.log("The message is sent: " + response.message);
res.json({response: "sent"})
}
})
});
来自 Angular
UserService.ts(相关部分)
constructor(private http: HttpClient) { }
sendMail(user: User){
console.log("sendMail() was called.")
return this.http.post("http://localhost:3000/sendMails", user.email);
}
这是在我注册时在注册组件中调用的
registration.component.ts
public signUp(){
//marks all fields as touched.
this.fs.markFormGroupTouched(this.registerForm);
if(this.registerForm.valid) {
let user: User = this.registerForm.value;
console.log(user)
this.userService.createUser(user);
this.userService.sendMail(user); //<--- only this is relevant for this case.
}
else {
//shows the error messages, for the not valid input fields.
this.formErrors = this.fs.validateForm(this.registerForm,
this.formErrors, false)
}
}
当我运行这段代码没有错误。在检查器/客户端控制台 window 中,我看到消息 "sendMail() was called.",这意味着它肯定会调用我的服务。但是,我没有收到邮件,也没有收到任何错误
http.post()
inside sendMail()
returns 一个 observable。您必须订阅这个 observable,否则它不会被执行。
所以在 registration.component.ts
你需要这样的东西:
this.userService.sendMail(user).subscribe(
response => {
console.log(response);
}
);
您还需要在 sendMail 函数调用中包含 return 语句。在错误块中,res.send 是无法访问的代码。编辑代码如下:
app.post('/sendMails', function(req,res) {
console.log("WENT INTO SEND MAILS!!!")
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'someemail@gmail.com',
pass: process.env.MAIL_PASSWORD
},
tls: {
rejectUnauthorized: false
},
});
let HelperOptions = {
from: '"Babababa" <someemail@gmail.com>',
to: 'someemail@gmail.com',
subject: 'Hello world',
text: 'hey dude'
};
transporter.sendMail(HelperOptions, (error, response) => {
if(error) {
console.log(error);
return res.json({error: "API Error"}); // return should be here instead of at console.log
}
else{
console.log("The message is sent: " + response.message);
return res.json({response: "sent"})
}
})
});
我想在注册用户时从我的 nodejs 服务器发送一封电子邮件。我使用 Angular5 作为前端。它没有 return 任何错误,但它也不起作用。就像我的函数根本没有被调用一样。这是我尝试过的(在这个例子中电子邮件不是真实的):
server.js
app.post('/sendMails', function(req,res) {
console.log("WENT INTO SEND MAILS!!!")
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'someemail@gmail.com',
pass: process.env.MAIL_PASSWORD
},
tls: {
rejectUnauthorized: false
},
});
let HelperOptions = {
from: '"Babababa" <someemail@gmail.com>',
to: 'someemail@gmail.com',
subject: 'Hello world',
text: 'hey dude'
};
transporter.sendMail(HelperOptions, (error, response) => {
if(error) {
return console.log(error);
res.json({error: "API Error"});
}
else{
console.log("The message is sent: " + response.message);
res.json({response: "sent"})
}
})
});
来自 Angular
UserService.ts(相关部分)
constructor(private http: HttpClient) { }
sendMail(user: User){
console.log("sendMail() was called.")
return this.http.post("http://localhost:3000/sendMails", user.email);
}
这是在我注册时在注册组件中调用的
registration.component.ts
public signUp(){
//marks all fields as touched.
this.fs.markFormGroupTouched(this.registerForm);
if(this.registerForm.valid) {
let user: User = this.registerForm.value;
console.log(user)
this.userService.createUser(user);
this.userService.sendMail(user); //<--- only this is relevant for this case.
}
else {
//shows the error messages, for the not valid input fields.
this.formErrors = this.fs.validateForm(this.registerForm,
this.formErrors, false)
}
}
当我运行这段代码没有错误。在检查器/客户端控制台 window 中,我看到消息 "sendMail() was called.",这意味着它肯定会调用我的服务。但是,我没有收到邮件,也没有收到任何错误
http.post()
inside sendMail()
returns 一个 observable。您必须订阅这个 observable,否则它不会被执行。
所以在 registration.component.ts
你需要这样的东西:
this.userService.sendMail(user).subscribe(
response => {
console.log(response);
}
);
您还需要在 sendMail 函数调用中包含 return 语句。在错误块中,res.send 是无法访问的代码。编辑代码如下:
app.post('/sendMails', function(req,res) {
console.log("WENT INTO SEND MAILS!!!")
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'someemail@gmail.com',
pass: process.env.MAIL_PASSWORD
},
tls: {
rejectUnauthorized: false
},
});
let HelperOptions = {
from: '"Babababa" <someemail@gmail.com>',
to: 'someemail@gmail.com',
subject: 'Hello world',
text: 'hey dude'
};
transporter.sendMail(HelperOptions, (error, response) => {
if(error) {
console.log(error);
return res.json({error: "API Error"}); // return should be here instead of at console.log
}
else{
console.log("The message is sent: " + response.message);
return res.json({response: "sent"})
}
})
});