时间:2019-05-03 来源:小程序工厂
sendmail函数的代码如下:
index.js
// 云函数入口文件const nodemailer = require("nodemailer");var transporter = nodemailer.createTransport({
service: 'qq',
port: 465, // SMTP 端口
secure: true, // 使用 SSL
auth: {
user: 'xxxx@qq.com', //发邮件邮箱
pass: '*******' //此处不是qq密码是
}});var mailOptions = {
from: 'xxxxxxx@qq.com', // 发件地址
to: 'xxxxxxxx@qq.com', // 收件列表
subject: '测试云函数', // 标题
text: '测试云函数'};// 云函数入口函数exports.main = async (event, context) => {
console.log("Start to sendemail")
//开始发送邮件
const info = await transporter.sendMail(mailOptions);
console.log('Message sent: ' + info.response);
return info}package.json
{
"name": "sendmail",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "latest",
"nodemailer":"^4.7.0" //在此处注明要使用的nodemailer库,上传云函数的时候后台可以自动部署
}}需要修改下index.js里的邮箱地址,然后在微信IDE里右键选择“上传并部署(云端安装依赖)”。