以下代码是云函数发送验证码api,直接复制+改个人参数:
其中"user"和"pass"使用自己的账号数据,如何拿到看以下步骤:
网易邮箱6.0版:登录--点击设置--点击POP3/SMTP/IMAP--点击开启服务:IMAP/SMTP服务【会弹出自己的账号授权码,有效期一个月】
'use strict';var vkmail;try {vkmail = require('vk-mail');
} catch (err) {console.error("请先添加公共模块:vk-mail");
}module.exports = {/*** XXXnameXXX* @url client/email/pub/sendEmailCode 前端调用的url参数地址* data 请求参数* @param {String} params1 参数1*/main: async (event) => {let { data = {}, userInfo, util, filterResponse, originalParam } = event;let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;let { uid } = data;let res = { code: 0, msg: "" };// 业务逻辑开始-----------------------------------------------------------// email是vk的云函数写法,故接受不会在event里面而是在Data里面let {email, // 前端接收邮箱type = "bind", // 前端接收验证码类型(如: login register bind unbind)} = data;console.log(" email", email)// 支持QQ 163 等主流邮箱let emailConfig = {"host": "smtp.163.com","port": 465,"secure": true,"auth": {"user": "13574401245@163.com", // 发件人邮箱账号"pass": "NGaZnczUnmssa6x4", // 账号授权码}};if (!email) return { code: -1, msg: "email不能为空" };if (!type) return { code: -1, msg: "type不能为空" };let code = Math.floor(Math.random() * 100000) + 100000;let subject = `验证码 ${code}`;let text = `验证码 ${code},打死也不要告诉别人!`;// 创建邮箱服务实例let emailService = vkmail.createTransport({"host": emailConfig.host,"port": emailConfig.port,"secure": emailConfig.secure, // use SSL"auth": emailConfig.auth});try {// 发送邮件res.sendMailRes = await emailService.sendMail({"from": emailConfig.auth.user, // 邮件的发送者"to": email, // 邮件的接收者"cc": emailConfig.auth.user, // 由于邮件可能会被当成垃圾邮件,但只要把右键抄送给自己一份,就不会被当成垃圾邮件。"subject": subject, // 邮件的标题"text": text, // 邮件的内容});// 标记发送成功res.code = 0;res.msg = "ok";// 发送验证码成功后,通常需要设置验证码(写入数据库)// await uniID.setVerifyCode({ code, email, type });} catch (err) {res.code = -1;res.msg = "邮件发送失败";res.err = err;}// 业务逻辑结束-----------------------------------------------------------return res;}
}
前端传要发送的email即可
<template><view class="content"><input type="text" placeholder="邮箱" v-model="form1.email" /><input type="text" placeholder="验证码" v-model="form1.code" /><view class="tips" style="color: red;">上次发送的验证码:{{form1.code}}</view><view class="title">邮箱注册&登录相关</view><view class="tips" style="text-align: left;">如需要使用此功能,先确保已安装npm包<view>"nodemailer": "^6.4.11"</view></view><view style="display: flex;"><button type="default" @click="sendEmailCode('login')">真实-发送验证码</button><button type="default" @click="setVerifyCode('login')">模拟-发送验证码</button></view><button type="default" @click="loginByEmail">邮箱验证码登录(不存在则注册)</button><view class="title">绑定邮箱相关</view><view style="display: flex;"><button type="default" @click="sendEmailCode('bind')">真实-发送验证码</button><button type="default" @click="setVerifyCode('bind')">模拟-发送验证码</button></view><button type="default" @click="bindEmail">绑定邮箱(需先登录)</button><view class="tips">绑定邮箱时如果不传验证码或者传空则直接绑定并设置email_confirmed为1,不会去校验验证码</view><view class="title">解绑邮箱相关</view><view style="display: flex;"><button type="default" @click="sendEmailCode('unbind')">真实-发送验证码</button><button type="default" @click="setVerifyCode('unbind')">模拟-发送验证码</button></view><button type="default" @click="unbindEmail">解绑邮箱(需先登录)</button><view class="tips">解绑邮箱时如果不传验证码或者传空则直接解除绑定绑定,不会去校验验证码</view></view>
</template><script>var vk = uni.vk;export default {data() {return {form1:{email: '',code: ''}}},onLoad(options) {vk = uni.vk;},methods: {// 为了演示把这个逻辑放在客户端getCode() {const randomStr = '00000' + Math.floor(Math.random() * 1000000)this.form1.code = randomStr.substring(randomStr.length - 6)},sendEmailCode(type) {// let that = this;// if (!/.+@.+/.test(that.form1.email)) {// uni.showModal({// content: '请输入正确的邮箱',// showCancel: false// })// return// }// var form1 = that.form1;vk.callFunction({url: 'client/email/pub/sendEmailCode',title: '请求中...',data: {email:"13574401245@163.com"},success: (data) => {vk.log("data",data)}});// vk.userCenter.sendEmailCode({// data:{// serviceType:"qq",// email: form1.email,// type: type,// },// success: (data) => {// vk.alert("邮件发送成功");// }// });},setVerifyCode(type) {let that = this;if (!/.+@.+/.test(that.form1.email)) {uni.showModal({content: '请输入正确的邮箱',showCancel: false})return}that.getCode();var form1 = that.form1;vk.userCenter.setVerifyCode({data:{email: form1.email,code: form1.code,type: type,},success: (data) => {vk.toast("发送成功");}});},// 邮箱登录loginByEmail() {let that = this;var form1 = that.form1;vk.userCenter.loginByEmail({data:form1,success: (data) => {vk.alert("登录成功");}});},// 绑定邮箱bindEmail(){let that = this;var form1 = that.form1;vk.userCenter.bindEmail({data:form1,success: (data) => {vk.alert("绑定成功");}});},// 解绑邮箱unbindEmail(){let that = this;var form1 = that.form1;vk.userCenter.unbindEmail({data:form1,success: (data) => {vk.alert("解绑成功");}});}}}
</script><style lang="scss" scoped>.content {padding: 15px;}.content input {height: 46px;border: solid 1px #DDDDDD;border-radius: 5px;margin-bottom: 15px;padding: 0px 15px;display: block;}.content button {margin-bottom: 15px;margin-left: auto;margin-right: auto;padding-left: 14px;padding-right: 14px;}.content navigator {display: inline-block;color: #007AFF;border-bottom: solid 1px #007AFF;font-size: 16px;line-height: 24px;margin-bottom: 15px;}.tips {text-align: center;line-height: 20px;font-size: 14px;color: #999999;margin-bottom: 20px;}.title{font-size: 36rpx;margin-bottom: 20rpx;}
</style>