一、前言
最近公司有个需求,在微信小程序中点击后发送邮件到指定地址,并带附件。要求用云开发处理,不用本地的服务器。
参考了网上的一些资料,并动手做了测试,成功实现。
以下是一些博客的链接:
https://blog.csdn.net/tiramisu_ljh/article/details/78580635
https://www.jianshu.com/p/f8d330d9911a
https://blog.csdn.net/a446362429/article/details/105527425/ (注意:path的路径)
用到的是node.js的nodemailer邮件发送模块,安装时要注意,用npm安装。如果用cnpm安装会有问题
代码如下:
//引入发送邮件的类库 let nodemailer = require('nodemailer'); //创建一个SMTP客户端配置 let config = { host: "smtp.163.com" //邮箱的主机 ,prot: 465 //邮箱的端口号 ,secure: true//启用SSL协议 465端口true,其他端口false,看的官方文档 ,auth: { user: "xxxxxxx@163.com" //邮箱 ,pass: "KAQTZUNDDXFNZNNW" //邮箱授权码或密码 } } //创建SMTP客户端对象 let transporter = nodemailer.createTransport(config); let mail = { from: 标题+"<xxxxxxx@163.com>" //发件人,里面的邮箱必须填写正确,否则会有问题 ,subject: '邮件' //邮件主题 ,to: 'xxxxxxx@163.com' // 收件列表,可以多个,也就是群发,用逗号","隔开 ,text: "" //发送文本 ,html: "请查收附件" //发送html代码 ,attachments: [{}]//附件,格式为{filename,path} } let res = transporter.sendMail(mail);