React Form:如何使用 Nodemailer 发送电子邮件
React Form: How to send email using Nodemailer
我正在尝试使用 NodeMailer
从我的网站联系人邮箱向我的 GoDaddy
电子邮件发送邮件。
但是在发送它时我收到 500
错误 net::ERR_EMPTY_RESPONSE
.
我也看过 SO
的一些解决方案,但似乎我错过了一些内容。
我是否需要更改我的配置或任何与在 godaddy 邮箱中接收电子邮件相关的内容?
任何需要在这里完成的建议。
//ReactForm
class BookAConsultation extends Component{
constructor(props){
super(props);
this.state={
name: '',
phone: '',
email: '',
comment: ''
};
this.handleOnChange = this.handleOnChange.bind(this);
this.handleOnSubmit = this.handleOnSubmit.bind(this);
}
handleOnChange = (e) => {
this.setState({ [e.target.name] : e.target.value });
}
async handleOnSubmit(e) {
e.preventDefault();
const { name, phone, email, comment } = this.state;
const bookConsultation = await axios.post('api/consultation',{
name,
phone,
email,
comment
})
}
render(){
return(
<Container>
<Grid className="book-a-consultation-header">
<Cell col={6} >
<div className="book-a-consultation">
<Form onSubmit ={this.handleOnSubmit}>
<FormGroup>
<div className="row ">
<Label for="name">Name*</Label>
<Input type="text" name="name" id="name" placeholder="Name"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="phone">Phone Number*</Label>
<Input type="text" name="phone" id="phone" placeholder="Phone Number"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="email">Email Id*</Label>
<Input type="email" name="email" id="email" placeholder="Email"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="comment">Additional Comment</Label>
<textarea type="text" name="comment" id="comment" placeholder="Comment"
className="mb-3"
onChange={this.handleOnChange}/>
<Button
color="dark"
style={{marginTop: '2rem'}}
block
>Book Free Consultation</Button>
</div>
</FormGroup>
</Form>
</div>
</Cell>
</Grid>
</Container>
)
}
}
//Server.js
app.post('/api/consultation', (req, res) => {
nodemailer.createTestAccount((err, account) => {
const htmlEmail = `
<h3>Contact deatails </h3>
<ul>
<li>Name: ${req.body.name} </li>
<li>Phone: ${req.body.phone} </li>
<li>Email: ${req.body.email} </li>
</ul>
<h3> Message <h3>
<p>${req.body.content}</p>
`
let mailerConfig = {
host: "smtpout.secureserver.net",
secure: true,
secureConnection: false, // TLS requires secureConnection to be false
tls: {
ciphers:'SSLv3'
},
requireTLS:true,
port: 465,
debug: true,
auth: {
user: "hello@testemail.com",
pass: "password"
}
};
let transporter = nodemailer.createTransport(mailerConfig);
let mailOptions = {
from: 'testemail@gmail.com',
to: 'hello@testemail.com',
replyTo: 'testemail@gmail.com',
subject: 'Some Subject',
text: req.body.content,
html: htmlEmail
};
transporter.sendMail(mailOptions, (err, info) => {
if (error) {
console.log('error:', err);
} else {
console.log('Message sent: %s', info.content);
console.log('Message URL: %s', nodemailer.getTestMessageUrl);
}
});
})
})
//错误
看起来一切正常,当服务器没有任何响应并且前端正在等待答案时出现错误,因此在您的 sendMail 回调中尝试使用:
transporter.sendMail(mailOptions, (err, info) => {
if (error) {
// ERROR SERVER RESPONSE
res.status(500).send({status: 'FAIL', msg: 'Internal error: email not sent'})
...
} else {
...
// SUCCESS SERVER RESPONSE
res.status(200).json({status: 'OK', msg: 'Email sent'})
...
}
});
这应该可以解决问题。
我正在尝试使用 NodeMailer
从我的网站联系人邮箱向我的 GoDaddy
电子邮件发送邮件。
但是在发送它时我收到 500
错误 net::ERR_EMPTY_RESPONSE
.
我也看过 SO
的一些解决方案,但似乎我错过了一些内容。
我是否需要更改我的配置或任何与在 godaddy 邮箱中接收电子邮件相关的内容?
任何需要在这里完成的建议。
//ReactForm
class BookAConsultation extends Component{
constructor(props){
super(props);
this.state={
name: '',
phone: '',
email: '',
comment: ''
};
this.handleOnChange = this.handleOnChange.bind(this);
this.handleOnSubmit = this.handleOnSubmit.bind(this);
}
handleOnChange = (e) => {
this.setState({ [e.target.name] : e.target.value });
}
async handleOnSubmit(e) {
e.preventDefault();
const { name, phone, email, comment } = this.state;
const bookConsultation = await axios.post('api/consultation',{
name,
phone,
email,
comment
})
}
render(){
return(
<Container>
<Grid className="book-a-consultation-header">
<Cell col={6} >
<div className="book-a-consultation">
<Form onSubmit ={this.handleOnSubmit}>
<FormGroup>
<div className="row ">
<Label for="name">Name*</Label>
<Input type="text" name="name" id="name" placeholder="Name"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="phone">Phone Number*</Label>
<Input type="text" name="phone" id="phone" placeholder="Phone Number"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="email">Email Id*</Label>
<Input type="email" name="email" id="email" placeholder="Email"
className="mb-3"
onChange={this.handleOnChange}/>
<Label for="comment">Additional Comment</Label>
<textarea type="text" name="comment" id="comment" placeholder="Comment"
className="mb-3"
onChange={this.handleOnChange}/>
<Button
color="dark"
style={{marginTop: '2rem'}}
block
>Book Free Consultation</Button>
</div>
</FormGroup>
</Form>
</div>
</Cell>
</Grid>
</Container>
)
}
}
//Server.js
app.post('/api/consultation', (req, res) => {
nodemailer.createTestAccount((err, account) => {
const htmlEmail = `
<h3>Contact deatails </h3>
<ul>
<li>Name: ${req.body.name} </li>
<li>Phone: ${req.body.phone} </li>
<li>Email: ${req.body.email} </li>
</ul>
<h3> Message <h3>
<p>${req.body.content}</p>
`
let mailerConfig = {
host: "smtpout.secureserver.net",
secure: true,
secureConnection: false, // TLS requires secureConnection to be false
tls: {
ciphers:'SSLv3'
},
requireTLS:true,
port: 465,
debug: true,
auth: {
user: "hello@testemail.com",
pass: "password"
}
};
let transporter = nodemailer.createTransport(mailerConfig);
let mailOptions = {
from: 'testemail@gmail.com',
to: 'hello@testemail.com',
replyTo: 'testemail@gmail.com',
subject: 'Some Subject',
text: req.body.content,
html: htmlEmail
};
transporter.sendMail(mailOptions, (err, info) => {
if (error) {
console.log('error:', err);
} else {
console.log('Message sent: %s', info.content);
console.log('Message URL: %s', nodemailer.getTestMessageUrl);
}
});
})
})
//错误
看起来一切正常,当服务器没有任何响应并且前端正在等待答案时出现错误,因此在您的 sendMail 回调中尝试使用:
transporter.sendMail(mailOptions, (err, info) => {
if (error) {
// ERROR SERVER RESPONSE
res.status(500).send({status: 'FAIL', msg: 'Internal error: email not sent'})
...
} else {
...
// SUCCESS SERVER RESPONSE
res.status(200).json({status: 'OK', msg: 'Email sent'})
...
}
});
这应该可以解决问题。