![](/img/trans.png)
[英]How to access Node Express server using proxy once deployed to Heroku?
[英]How to send an email via form input with angular and node once deployed on heroku?
我在heroku
上部署了一个应用程序,前端为角,后端为node(express)。 在联系页面上,我有一个表单,一旦按下发送按钮,该表单将通过电子邮件发送,然后通过HTTP将信息发送到节点,以便用户输入通过节点功能传递-我正在使用SendGrid
插件发送电子邮件的电子邮件过程。 我尝试了很多次,但是我无法使它正常工作,也找不到带有角度的教程。 它以前在localhost
上与nodemailer
一起使用,但是一旦部署,我就无法弄清楚。 非常感谢您的宝贵时间。
节点代码app.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const sgMail = require('@sendgrid/mail');
const port = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'client')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client/index.html'));
});
app.post('/client-contact', (req, res) => {
const clientMsg = `
<p> Email sent from your portfolio website</p>
<h3>Contact details</h3>
<ul>
<li>Name ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'mrwanzein@outlook.com',
from: 'mrwanzein@outlook.com',
subject: 'Angular portfolio form user response',
html: clientMsg,
};
sgMail.send(msg);
});
app.listen(port, () => {
console.log(`Server is on on port ${port}`);
});
该函数用于注册用户输入,然后将其发送到节点(并处理一些验证,感兴趣的是最后一行)contact.component.ts
sendMail() {
var name = <HTMLInputElement>document.getElementById('inputName'),
email = <HTMLInputElement>document.getElementById('inputEmail'),
msg = <HTMLInputElement>document.getElementById('inputMsg');
var obj = {
name: name.value,
email: email.value,
message: msg.value
}
let validateEmail = () => {
const re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?
^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])/;
return re.test(email.value);
}
if(!name.value || !email.value || !msg.value || !validateEmail()) {
this.bool = true;
} else {
this.bool = false;
setTimeout(() => {
window.location.reload()
}, 1500);
return this.http.post('https://mrwanzein.herokuapp.com/client-contact', obj).subscribe();
}
}
您需要为您的应用程序配置SMTP服务器。
最简单的方法是使用heroku插件,例如mailgun
我修复了它,基本上代码没问题,我不得不删除sgMail.send(msg)周围的res.send(),并且忘记了ng build(angular)来更新HTTP地址。 我正在推送对仓库的更改,而没有缺少更新的代码,哦,亲爱的...:P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.