每当我注册用户时,我都想从我的nodejs服务器发送电子邮件。我正在使用Angular5作为前端。它不返回任何错误,但也不起作用。好像根本没有调用我的函数。这就是我尝试过的(在此示例中,电子邮件不是真实的):
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)
}
}
当我运行此代码时,没有错误。在检查器/客户端控制台窗口中,我看到消息“ sendMail()被调用。” ,这意味着它正在明确地调用我的服务。但是,我没有收到邮件也没有任何错误
答案 0 :(得分:1)
http.post()
中的sendMail()
返回一个可观察值。您必须订阅此可观察的,否则将不执行。
因此在registration.component.ts
中,您需要类似以下内容:
this.userService.sendMail(user).subscribe(
response => {
console.log(response);
}
);
答案 1 :(得分:0)
您还需要在sendMail函数调用中包含return语句。在错误块中,res.send是无法访问的代码。下面的修改代码:
In [1]: import numpy
...: import pandas
...: easy_matrix_example = numpy.array([
...: [100, 20200, 20900 ],
...: [200, 80200, 80900 ],
...: [300, 180200, 180900 ],
...: [400, 320200, 320900 ],
...: [500, 500200, 500900 ],
...: ], dtype=numpy.float64)
...: easy_df_example = pandas.DataFrame(easy_matrix_example, columns=["A","B","C"])
...:
...:
In [2]: easy_df_example
Out[2]:
A B C
0 100.0 20200.0 20900.0
1 200.0 80200.0 80900.0
2 300.0 180200.0 180900.0
3 400.0 320200.0 320900.0
4 500.0 500200.0 500900.0
In [5]: easy_df_example.values.flags
Out[5]:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
In [6]: df_copy = easy_df_example.copy()
In [7]: df_copy
Out[7]:
A B C
0 100.0 20200.0 20900.0
1 200.0 80200.0 80900.0
2 300.0 180200.0 180900.0
3 400.0 320200.0 320900.0
4 500.0 500200.0 500900.0
In [8]: df_copy.values.flags
Out[8]:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False