项目作者: QueenieCplusplus

项目描述 :
MailGun
高级语言: JavaScript
项目地址: git://github.com/QueenieCplusplus/Backend_Script_Nodejs_Mailer.git


Backend_Script_Nodejs_Mailer

MailGun

cd to proj_dir, then execute cli as

  1. npm start

then browse to localhost:3000, and route to /mail

and browse to localhost:3000, and route to /nodemail

and route to /gmail

https://github.com/QueenieCplusplus/Backend_Script_Nodejs_Mailer/blob/main/KsExpressApp/routes/gmail.js

  1. var fs = require('fs');
  2. var readline = require('readline');
  3. var google = require('googleapis');
  4. var googleAuth = require('google-auth-library');
  5. var express = require('express');
  6. var router = express.Router();
  7. router.get('/', function(req, res, next) {
  8. res.render('gmail', { title: 'GMail is abled now..., see log' });
  9. });
  10. var SCOPES = ['https://mail.google.com/'];
  11. var TOKEN_PATH = 'gmail-api-token.json'; // need token
  12. // https://console.developers.google.com/apis/dashboard
  13. // Load client secrets from a local file.
  14. fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  15. if (err) {
  16. console.log('Error loading client secret file: ' + err);
  17. return;
  18. }
  19. // Authorize a client with the loaded credentials, then call the
  20. // Gmail API.
  21. authorize(JSON.parse(content), sendMessage);
  22. });
  23. /**
  24. * Create an OAuth2 client with the given credentials, and then execute the
  25. * given callback function.
  26. *
  27. * param {Object} credentials The authorization client credentials.
  28. * param {function} callback The callback to call with the authorized client.
  29. */
  30. function authorize(credentials, callback) {
  31. var clientSecret = credentials.installed.client_secret;
  32. var clientId = credentials.installed.client_id;
  33. var redirectUrl = credentials.installed.redirect_uris[0];
  34. var auth = new googleAuth();
  35. var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
  36. // Check if we have previously stored a token.
  37. fs.readFile(TOKEN_PATH, function (err, token) {
  38. if (err) {
  39. getNewToken(oauth2Client, callback);
  40. } else {
  41. oauth2Client.credentials = JSON.parse(token);
  42. callback(oauth2Client);
  43. }
  44. });
  45. }
  46. /**
  47. * Get and store new token after prompting for user authorization, and then
  48. * execute the given callback with the authorized OAuth2 client.
  49. *
  50. * //param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
  51. * //param {getEventsCallback} callback The callback to call with the authorized
  52. * client.
  53. */
  54. function getNewToken(oauth2Client, callback) {
  55. var authUrl = oauth2Client.generateAuthUrl({
  56. access_type: 'offline',
  57. scope: SCOPES
  58. });
  59. console.log('Authorize this app by visiting this url: ', authUrl);
  60. var rl = readline.createInterface({
  61. input: process.stdin,
  62. output: process.stdout
  63. });
  64. rl.question('Enter the code from that page here: ', function (code) {
  65. rl.close();
  66. oauth2Client.getToken(code, function (err, token) {
  67. if (err) {
  68. console.log('Error while trying to retrieve access token', err);
  69. return;
  70. }
  71. oauth2Client.credentials = token;
  72. storeToken(token);
  73. callback(oauth2Client);
  74. });
  75. });
  76. }
  77. /**
  78. * Store token to disk be used in later program executions.
  79. *
  80. * //param {Object} token The token to store to disk.
  81. */
  82. function storeToken(token) {
  83. fs.writeFile(TOKEN_PATH, JSON.stringify(token));
  84. console.log('Token stored to ' + TOKEN_PATH);
  85. }
  86. /**
  87. * Lists the labels in the user's account.
  88. *
  89. * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
  90. */
  91. function listLabels(auth) {
  92. var gmail = google.gmail('v1');
  93. gmail.users.labels.list({
  94. auth: auth,
  95. userId: 'me',
  96. }, function (err, response) {
  97. if (err) {
  98. console.log('The API returned an error: ' + err);
  99. return;
  100. }
  101. var labels = response.labels;
  102. if (labels.length == 0) {
  103. console.log('No labels found.');
  104. } else {
  105. console.log('Labels:');
  106. for (var i = 0; i < labels.length; i++) {
  107. var label = labels[i];
  108. console.log('- %s', label.name);
  109. }
  110. }
  111. });
  112. }
  113. /**
  114. * Send Message.
  115. */
  116. function sendMessage(auth) {
  117. var gmail = google.gmail('v1');
  118. var email_lines = [];
  119. email_lines.push("From: katesapp2019@gmail.com");
  120. email_lines.push("To:katesreact2020@gmail.com");
  121. email_lines.push('Content-type: text/html;charset=utf-8');
  122. email_lines.push('MIME-Version: 1.0');
  123. email_lines.push("Subject: Gmail Sender here");
  124. email_lines.push("");
  125. email_lines.push("text text tex");
  126. email_lines.push("<b>bold text text </b>");
  127. var email = email_lines.join("\r\n").trim();
  128. var base64EncodedEmail = new Buffer(email).toString('base64').replace(/\+/g, '-').replace(/\//g, '_');
  129. console.log(base64EncodedEmail);
  130. function sendDone(err, response) {
  131. if (err) {
  132. console.log('The API returned an error: ' + err);
  133. return;
  134. }
  135. console.log('send mail success', response);
  136. }
  137. gmail.users.messages.send({
  138. auth: auth,
  139. userId: 'me',
  140. resource: {
  141. raw: base64EncodedEmail
  142. }
  143. }, sendDone);
  144. }
  145. module.exports = router;

ref:

https://www.npmjs.com/package/mail-notifier (npm module)

https://npm.io/search/keyword:email (maingun)

https://nodemailer.com/about/ (nodemailer)