node.js - React Native 发送邮件
问题描述
大家好,我正在使用AWS SES发送电子邮件,故事情节是,如果用户忘记了密码,用户会将他们的电子邮件地址输入TextInput并通过电子邮件发送代码,(请参考下图)
注意:在我的 AWS SES 中,我的账户已经过验证,
这是我的示例代码
import React, {useState, createRef} from 'react';
//i dont know if i am doing right here
const submitForm = ({ event }) => {
const payload = {
url: '/user/forgotpassword',
email: this.state.Email,
};
post(payload)
.then(() => {
// on success, clear any errors and set submitted state to true
this.useState({ error: null, submitted: true });
})
.catch((error) => {
// on error, update error state with the error message and set submitted to false
this.setState({ error: error.message, submitted: false });
});
};
<View style={formStyles.SectionStyle}>
<Text style={formStyles.label1}>Email Address:</Text>
<TextInput
name="email"
id="email"
style={formStyles.inputStyle}
onChangeText={(text) => this.setState({ Email: text })}
placeholder="Enter Email Address"
placeholderTextColor="#8b9cb5"
autoCapitalize="none"
keyboardType="email-address"
returnKeyType="next"
underlineColorAndroid="#f000"
blurOnSubmit={false}
/>
</View>
<Hoverable>
{isHovered => (
<TouchableOpacity
style={{
backgroundColor: isHovered ? '#FFFFFF':'#00ADEF',
borderWidth: 1,
color: '#FFFFFF',
borderColor: '#008ABE',
height: 37,
width: '90%',
alignItems: 'center',
borderRadius: 8,
marginTop: 20,
marginLeft: 50,
marginRight: 50,
//
}}
onPress={() => submitForm()}
activeOpacity={0.5}>
<Text style={{
color: isHovered ? '#00ADEF':'#FFFFFF',
paddingVertical:5,
fontSize: 18,
}}>Proceed to Password Reset</Text>
</TouchableOpacity>
)}
</Hoverable>
节点.js api
async function forgotpassword(req, res, next){
var nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: "email-smtp.ap-southeast-1.amazonaws.com",
port: 465,
secure: true,
auth: {
user: "******************",
pass: "******************"
}
});
var mailOptions = {
from: 'support@ecomerce.com',
to: 'testingfor573@gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
这是错误
解决方案
为此,创建一个名为Email
并存储其值的状态变量。
在按下提交按钮时,使用此值发送邮件..
像这样 -
在你的constructor
this.state = {
...
Email: "",
...
}
你的 `` 函数应该是这样的 -
const submitForm = ({ event }) => {
const payload = {
url: '/user/forgotpassword',
email: this.state.Email,
};
post(payload)
.then(() => {
// on success, clear any errors and set submitted state to true
this.setState({ error: null, submitted: true });
})
.catch((error) => {
// on error, update error state with the error message and set submitted to false
this.setState({ error: error.message, submitted: false });
});
};
渲染代码
<View style={formStyles.SectionStyle}>
<Text style={formStyles.label1}>Email Address:</Text>
<TextInput
name="email"
id="email"
style={formStyles.inputStyle}
onChangeText={(text) => this.setState({ Email: text })}
placeholder="Enter Email Address"
placeholderTextColor="#8b9cb5"
autoCapitalize="none"
keyboardType="email-address"
returnKeyType="next"
underlineColorAndroid="#f000"
blurOnSubmit={false}
/>
</View>
<Hoverable>
{(isHovered) => (
<TouchableOpacity
style={{
backgroundColor: isHovered ? '#FFFFFF' : '#00ADEF',
borderWidth: 1,
color: '#FFFFFF',
borderColor: '#008ABE',
height: 37,
width: '90%',
alignItems: 'center',
borderRadius: 8,
marginTop: 20,
marginLeft: 50,
marginRight: 50,
//
}}
onPress={() => submitForm()}
activeOpacity={0.5}>
<Text
style={{
color: isHovered ? '#00ADEF' : '#FFFFFF',
paddingVertical: 5,
fontSize: 18,
}}>
Proceed to Password Reset
</Text>
</TouchableOpacity>
)}
</Hoverable>
推荐阅读
- java - 是否可以从 Gradle 中的不及物配置扩展传递配置?
- python-3.x - Leetcode 219. 我的 python 解决方案不起作用
- python - 用于发送命令和接收输出的 Python 服务器套接字(类似 netcat 侦听器)
- wordpress - 我的 category.php 模板中缺少 ACF 选项图像(徽标)
- javascript - 从 div 列表中选择一个 div 并在该 div reactjs 功能组件上切换类
- kotlin - OpenGL中简单矩形碰撞的问题
- connection-reset - 如何解决访问控制允许来源错误
- python - Pymoo 在候选搜索空间中工作的可能性
- flutter - 我想构建一个动态标签栏和相应的页面,其中包含与该标签对应的项目列表,使用复杂的 JSON 数据在颤振中
- docusignapi - 使用带有 DocuSign SDK 的自定义字段未按预期工作