Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Cloud function e-mail


May 22, 2021 Mini Program Cloud Development Advanced



With the help of the third-party module Nodemailer, we can also implement the use of cloud functions to send messages. C ombined with the ability to send e-mail, we can send e-mail notifications to users when they have registered a user, or commented on someone replying and important event information needs to be notified. The process of using cloud functions for this full set of user notifications is also simple to implement.

Technical documentation: Nodemailer Github address, Nodemailer official documentation

First, send an e-mail with a cloud function

1, turn on SMTP service

It is very troublesome to build our own mail server, we can use QQ mailbox, Gmail, 163 personal mail system or enterprise mail system to open IMAP/SMTP service, IMAP is the Internet mail access protocol, through which you can get mail information from the mail server, download mail, that is, receive mail; Here we'll just show you how to use cloud functions to send messages, mainly using smtp services.

Different mail systems have different smtp mail server, port number will also be different, these can go to the corresponding mailbox settings to see the relevant instructions, here only QQ mailbox for example, login QQ mailbox, in the mail settings - account open SMTP service, QQ mailbox send mail server: smtp.qq.com, using SSL, port number 465 or 587.

Cloud Development Cloud function e-mail

QQ will get the mail authorization code (the mail authorization code is not the mailbox password) when the SMTP service is turned on, which will be used later.

2, using the cloud function to send mail

Use the developer tool to create a cloud function, such as nodemail, and then add the dependency on nodemailer's latest latest at package.json, and right-click the cloud function directory to select the input command npm install installation dependency to open in the terminal:

  1. "dependencies": {
  2. "nodemailer": "latest"
  3. }

Then enter the following .js in the index and modify the parameters, such as:

  • Auth inside the mailbox account number and mailbox password (different mail system may not have the same mechanism, QQ mailbox is mailbox authorization code)

  • Message inside the from and to parameters, respectively, your sending mailbox and the sender's email address, you can also fill in cc and bcc, CC or secret to the designated mailbox;

  • host, port, here is QQ mailbox SMTP server address and the corresponding port, if you are using other mail systems, pay attention to modification;

  • nodemailer supports sending text text content, html web content and attachments in the form of attachments in a format that supports String, Buffer or Stream (various files), although mailboxes with different file sizes are limited and should not be too large, which can affect the execution time of cloud functions, and recommends that large files use cloud storage links;

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. exports.main = async (event, context) => {
  6. const nodemailer = require("nodemailer");
  7. let transporter = nodemailer.createTransport({
  8. host: "smtp.qq.com", //SMTP服务器地址
  9. port: 465, //端口号,通常为465,587,25,不同的邮件客户端端口号可能不一样
  10. secure: true, //如果端口是465,就为true;如果是587、25,就填false
  11. auth: {
  12. user: "3441****[email protected]", //你的邮箱账号
  13. pass: "你的QQ邮箱授权码" //邮箱密码,QQ的需要是独立授权码,不是QQ邮箱的密码
  14. }
  15. });
  16. let message = {
  17. from: '来自李东bbsky <[email protected]>', //你的发件邮箱
  18. to: '你要发送给谁', //你要发给谁
  19. // cc:'', 支持cc 抄送
  20. // bcc: '', 支持bcc 密送
  21. subject: '欢迎大家参与云开发技术训练营活动',
  22. //支持text纯文字,html代码
  23. text: '欢迎大家',
  24. html:
  25. '<p><b>你好:</b><img src="https://hackwork-1251009918.cos.ap-shanghai.myqcloud.com/handbook/html5/weapp.jpg" rel="external nofollow" /></p>' + '<p>欢迎欢迎<br/></p>',
  26. attachments: [ //支持多种附件形式,可以是String, Buffer或Stream
  27. {
  28. filename: 'image.png',
  29. content: Buffer.from( 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' + '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' + 'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
  30. 'base64'),
  31. },
  32. ],
  33. };
  34. let res = await transporter.sendMail(message);
  35. return res;
  36. }

After the deployment of the upload cloud function, the cloud function can be called in a small terminal or the cloud development console to receive mail, this is only directed to a send message, only suitable for sending to the administrator for mail notification scenarios.

Second, the expansion and application of mailbox functions

Although mail has fallen, mailboxes are an Internet product that is used by almost all Internet users, and the ability to send mail using cloud functions combined with mail can expand the same as the traditional backend.

Send e-mail to a specified person in conjunction with the cloud database

When the user in the small terminal to do some business operations, we can combine the database directed to the user or the target user to send an email, such as user binding registration or retrieval of password, when user A to user B wrote articles or comments can send an email to B, when the user to participate in activities need to be notified, the administrator can send mail to the target user. The people who receive messages in different business scenarios will also be different, and the contents of the messages will vary depending on the needs of the business, so the database plays a very important role in the delivery of messages.

However, because QQ mailbox is a personal mail system, can only send hundreds of mail every day, not suitable for the user volume of small programs as enterprise business to send mail.

Implement password verification and timed delivery of messages

When a user binds his or her mailbox in his or her profile, he or she can send mail and check codes, which can be a field in the database, and its value can be some randomly generated strings, but has a certain life cycle, such as failure after half an hour, and this automatically invalidated operation needs to be used to Timed triggers, messages can also be sent periodically in weekly or daily newspapers, and at some point in the day or week, the bulk collection of data for the day or week is automatically sent to the user, which is also dependent on the timed trigger, which we'll explain later in the timed trigger section.