【发布时间】:2020-06-18 05:36:40
【问题描述】:
我正在尝试使用 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);
}
});
})
})
//错误
【问题讨论】:
-
您需要从 server.js 发回响应
-
类似 res.sendStatus(200) 的东西。但是,实际响应取决于您的邮件请求是否成功
-
@ManosKounelakis 感谢您抽出宝贵时间。但我确实将它发送到我的电子邮件。这里有什么问题吗?
-
电子邮件被发送。但是,您的 server.js 还必须向您发送响应以完成原始请求。查看@forgived 的答案
-
是的,我尝试了@forgived 的答案,但它的响应与我当前的错误相同。
标签: node.js reactjs nodemailer godaddy-api