最近有一个需求,前端发送一个form表单到一个邮箱,找了一圈发现emailjs还不错就使用他了。
首先emailjs官网注册一个账号
![](https://i-blog.csdnimg.cn/blog_migrate/8fc3da5b1cfa473ebfe947d77449d844.png)
注册完之后创建一个邮件服务(我这里使用的是谷歌邮箱)
![](https://i-blog.csdnimg.cn/blog_migrate/a4fca46e8f98980303c423492fa2c468.png)
链接谷歌邮箱账户 然后创建服务
![](https://i-blog.csdnimg.cn/blog_migrate/b6aee546aeb6f3c3087cb1d8e2768d5f.jpeg)
接下来就要创建一个邮件的模版
![](https://i-blog.csdnimg.cn/blog_migrate/2f7c0a4fd7895ff042dd3ce11c5bc675.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b5de317ded0c478e3d922aa659c1b8c0.png)
模版可以是里面的变量是用{{}}包裹起来的 to Eamil 是设置收邮件的邮箱
到此为止服务就差不多了,现在开始看api
安装
![](https://i-blog.csdnimg.cn/blog_migrate/e40bcc13ed3058210807bc3e5369f8c8.png)
这里有案例
![](https://i-blog.csdnimg.cn/blog_migrate/5ccc8e2ebc65c01801547e639aec18b3.png)
我这里用的是send方法
import { send } from '@emailjs/browser';
const formRef = ref()
const submit = ()=>{
formRef.value.validate((valid, fields) => {
if (valid) {
let loadingInstance = ElLoading.service({
lock: true,
text: 'Loading',
background: 'rgba(0, 0, 0, 0.7)',
})
send(serviceID, templateID, templatePrams, publicKey).then(function(response) {
ElMessage.info('發送成功!')
loadingInstance.close()
}, function(error) {
ElMessage.error('發送失敗!')
loadingInstance.close()
})
}
})
我们来重点说一下这几个参数 serviceID, templateID, templatePrams, publicKey
serviceID
![](https://i-blog.csdnimg.cn/blog_migrate/d462f400dc36537472d92bac3ae75fe9.png)
templateID
![](https://i-blog.csdnimg.cn/blog_migrate/184de1d51a3d0720f27034a675e276bb.png)
templatePrams 就是一个包含key和value的对象,而在上面提到的邮件模版中的{{}}中的内容对应这里的对象的key,解析出来后邮件的内容就是对应的value了。
publicKey
![](https://i-blog.csdnimg.cn/blog_migrate/5b7305bfcb08e69e1f35b82d47e93676.jpeg)
最后我们看一下收到的邮件
![](https://i-blog.csdnimg.cn/blog_migrate/2c540dee0c71535c4ff7b304393a2feb.png)