gpt4 book ai didi

javascript - 尝试将 EmailJS 与 React 一起使用时服务 ID 无效

转载 作者:行者123 更新时间:2023-12-02 02:45:51 24 4
gpt4 key购买 nike

我在我的网站上创建了一个表格来联系我,为此我使用了 EmailJS。但是,当我尝试通过联系表单向自己发送邮件时,我收到了 400 错误 The service ID is invalid。我遵循了该教程的每个步骤,因为我之前没有使用过 EmailJS https://blog.mailtrap.io/react-send-email/

这是我的联系人组件

    class Contact extends React.Component {

constructor(props) {
super(props);
this.state = { feedback: '', name: 'Name', email: 'email@example.com' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

render() {
return (
<form className="test-mailing">
<h1>Let's see if it works</h1>
<div>
<textarea
id="test-mailing"
name="test-mailing"
onChange={this.handleChange}
placeholder="Post some lorem ipsum here"
required
value={this.state.feedback}
style={{width: '100%', height: '150px'}}
/>
</div>
<input type="button" value="Submit" className="btn btn--submit" onClick={this.handleSubmit} />
</form>
)
}

handleChange(event) {
this.setState({feedback: event.target.value})
}

handleSubmit() {
const templateId = 'template_id';

this.sendFeedback(templateId, {message_html: this.state.feedback, from_name: this.state.name, reply_to: this.state.email})
}

sendFeedback (templateId, variables) {
window.emailjs.send(
'gmail', templateId,
variables
).then(res => {
console.log('Email successfully sent!')
})
// Handle errors here however you like, or use a React error boundary
.catch(err => console.error('Oh well, you failed. Here some thoughts on the error that occured:', err))
}
}

这是我在 index.html

中添加的内容
`<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/emailjs-com@2.3.2/dist/email.min.js"></script>
<script type="text/javascript">
(function(){
emailjs.init("my_user_ID_here"); // Obtain your user ID at the dashboard https://dashboard.emailjs.com/integration
})();
`

最佳答案

要解决此问题,我必须将 'gmail' 替换为我的服务 ID。

sendFeedback (templateId, variables) {
window.emailjs.send(
***serviceID here***, templateId,
variables
).then(res => {
console.log('Email successfully sent!')
})
// Handle errors here however you like, or use a React error boundary
.catch(err => console.error('Oh well, you failed. Here some thoughts on the error that occured:', err))
}

我的网络浏览器中的 JavaScript 控制台帮助识别了这一点。

关于javascript - 尝试将 EmailJS 与 React 一起使用时服务 ID 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62819127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com