admin管理员组文章数量:1208153
I have built a web app using React.js in ES6. I currently want to create a basic "Contact Us" page and want to send an email. I am new to React and just discovered that I cannot actually send an email using React itself. I'm following the tutorial with nodemailer
and express-mailer
but have had some difficulty integrating the example code with my React files. Specifically, calling node expressFile.js
works, but I have no idea how to link this to my React front-end.
Nodemailer:
Express-mailer:
My React component for the form is below. How would I write an Express file so that it is called from the contactUs()
method in my React component? Thanks!
import React from 'react';
import {
Col,
Input,
Button,
Jumbotron
} from 'react-bootstrap';
class ContactView extends React.Component{
contactUs() {
// TODO: Send the email here
}
render(){
return (
<div>
<Input type="email" ref="contact_email" placeholder="Your email address"/>
<Input type="text" ref="contact_subject" placeholder="Subject"/>
<Input type="textarea" ref="contact_content" placeholder="Content"/>
<Button onClick={this.contactUs.bind(this)} bsStyle="primary" bsSize="large">Submit</Button>
</div>
)
}
};
export default ContactView;
I have built a web app using React.js in ES6. I currently want to create a basic "Contact Us" page and want to send an email. I am new to React and just discovered that I cannot actually send an email using React itself. I'm following the tutorial with nodemailer
and express-mailer
but have had some difficulty integrating the example code with my React files. Specifically, calling node expressFile.js
works, but I have no idea how to link this to my React front-end.
Nodemailer: https://github.com/nodemailer/nodemailer
Express-mailer: https://www.npmjs.com/package/express-mailer
My React component for the form is below. How would I write an Express file so that it is called from the contactUs()
method in my React component? Thanks!
import React from 'react';
import {
Col,
Input,
Button,
Jumbotron
} from 'react-bootstrap';
class ContactView extends React.Component{
contactUs() {
// TODO: Send the email here
}
render(){
return (
<div>
<Input type="email" ref="contact_email" placeholder="Your email address"/>
<Input type="text" ref="contact_subject" placeholder="Subject"/>
<Input type="textarea" ref="contact_content" placeholder="Content"/>
<Button onClick={this.contactUs.bind(this)} bsStyle="primary" bsSize="large">Submit</Button>
</div>
)
}
};
export default ContactView;
Share
Improve this question
asked May 4, 2016 at 2:10
felix_xiaofelix_xiao
1,6285 gold badges22 silver badges29 bronze badges
2 Answers
Reset to default 12When the button is clicked, execute an ajax POST request to your express server, i.e "/contactus". /contactus can fetch the email, subject, and content out of the post data and send to the mail function.
In React:
$.ajax({
url: '/contactus',
dataType: 'json',
cache: false,
success: function(data) {
// Success..
}.bind(this),
error: function(xhr, status, err) {
console.error(status, err.toString());
}.bind(this)
});
In express add the nodemailer code within an express post handler:
app.post('/contactus', function (req, res) {
// node mailer code
});
@ryan-jenkin is completely correct.
Alternatively, if you don't have / want jQuery as a dependency, you can use the native fetch api. Also, I typically set up my form so each input has a state, then use that state in the stringified blob.
Client-side (React):
handleSubmit(e){
e.preventDefault()
fetch('/contactus', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: this.state.email,
// then continue this with the other inputs, such as email body, etc.
})
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.success) {
this.setState({formSent: true})
}
else this.setState({formSent: false})
})
.catch((error) => {
console.error(error);
});
}
render(){
return (
<form onSubmit={this.handleSubmit} >
<input type="text" name="email" value={this.state.email} />
// continue this with other inputs, like name etc
</form>
)
}
本文标签: javascriptSend an Email Using ReactjsExpressjsStack Overflow
版权声明:本文标题:javascript - Send an Email Using React.js + Express.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738753111a2110514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论