├── example ├── package.json ├── testmultiple.js └── test.js ├── package.json ├── worknotes.md ├── LICENSE ├── readme.md └── sendmail.js /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test", 3 | "description": "Test app for davemail.", 4 | "author": "Dave Winer ", 5 | "version": "0.4.0", 6 | "dependencies" : { 7 | "davemail": "*" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /example/testmultiple.js: -------------------------------------------------------------------------------- 1 | const mail = require ("../sendmail.js"); 2 | for (var i = 1; i <= 10; i++) { //send a bunch of emails very quickly 3 | mail.send ("dave.winer@gmail.com", "Mail #" + i, "Please ignore this message.", "dave@scripting.com"); 4 | } 5 | -------------------------------------------------------------------------------- /example/test.js: -------------------------------------------------------------------------------- 1 | const mail = require ("davemail"); 2 | 3 | mail.send ("dave.winer@gmail.com", "hello", "This is a kind message", "dave@scripting.com"); 4 | 5 | var attachments = [ 6 | { 7 | data: "Oh the buzzing of the tweets.", 8 | type: "text/html", 9 | name: "test.html" 10 | } 11 | ]; 12 | mail.send ("dave.winer@gmail.com", "hello", "Message with an attachment", "dave@scripting.com", undefined, attachments); 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "davemail", 3 | "description": "Send email through AWS.", 4 | "author": "Dave Winer ", 5 | "version": "0.5.7", 6 | "main": "sendmail.js", 7 | "files": [ 8 | "sendmail.js" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/scripting/mail.git" 13 | }, 14 | "dependencies" : { 15 | "daveutils": "*", 16 | "aws-sdk": "*", 17 | "nodemailer": "*" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /worknotes.md: -------------------------------------------------------------------------------- 1 | #### 7/11/25; 9:47:28 AM by DW 2 | 3 | Add debugging code to show the result of sending an SES message in the console. 4 | 5 | #### 1/23/23; 3:05:07 PM by DW -- v0.5.0 6 | 7 | davemail works with SMTP. 8 | 9 | #### 1/5/20; 10:24:53 AM by DW 10 | 11 | AWS SES throttles email, limited to 14 messages per second. 12 | 13 | Clearly I am blowing right through that, they started objecting last night. the best thing to do is to add a queue to davemail that limits it to say 10 per second. Now every app that uses davemail gets the benefit. 14 | 15 | Added a new test script that sends multiple emails as fast as is recommended by AWS. 16 | 17 | #### 11/18/19; 1:20:35 PM by DW 18 | 19 | Added a new optional param to sendMail, attachment. If present we send the newly-written glue for SES's sendRawEmail. 20 | 21 | BTW, the interface allows for an array of attachments, but so far we only have it working with a single attachment. 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Dave Winer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # mail 2 | 3 | One place to send mail from all my Node apps. 4 | 5 | ### Features 6 | 7 | * I was scattering this code everywhere. Now I have a package. 8 | 9 | * It builds on SMTP or Amazon's SES. 10 | 11 | * One routine for all your mail-sending needs: mail.send. 12 | 13 | * Limits mail sending to 10 messages per second, to comply with AWS's rate limits. It keeps a queue so you can call it as fast as you like. 14 | 15 | * I use this code to send the nightly emails from Scripting News. 16 | 17 | ### Example 18 | 19 | ```javascript 20 | 21 | const mail = require ("davemail"); 22 | 23 | mail.send (recipient, subject, message, sender); 24 | 25 | ``` 26 | 27 | ### Using with SMTP 28 | 29 | The first versions of this package only worked with SES, which was fine because that was all I needed. But this package is now part of FeedLand and we want to be able to send mail with SMTP so have added this ability. This is how you set it up. 30 | 31 | There's a new function mail.start. It takes one param, options, a JavaScript object. 32 | 33 | You only have to call mail.start if you're using SMTP, otherwise it will send mail via SES, so there's no breakage for older apps. 34 | 35 | Here are the values that must be present in the options object: 36 | 37 | * flUseSes: if true we use Amazon SES and ignore the other values. If false we use SMTP. Default true, to preserve backward compatibility. 38 | 39 | * smtpHost: the domain name or IP address of the mail server. 40 | 41 | * port: the port the mail server is running on. 42 | 43 | * username: the account associated with the email sending priviledge. 44 | 45 | * password: the password for the account. 46 | 47 | Attachments are not supported for SMTP at this time. 48 | 49 | -------------------------------------------------------------------------------- /sendmail.js: -------------------------------------------------------------------------------- 1 | const myProductName = "davemail", myVersion = "0.5.7"; 2 | 3 | exports.start = start; //1/23/23 by DW 4 | exports.send = sendMail; 5 | exports.sendRawEmail = sendRawEmail; //11/18/19 by DW 6 | 7 | const AWS = require ("aws-sdk"); 8 | const utils = require ("daveutils"); 9 | const nodemailer = require ("nodemailer"); 10 | 11 | var ses = new AWS.SES ({ 12 | apiVersion: "2010-12-01", 13 | region: "us-east-1" 14 | }); 15 | 16 | var config = { //1/23/23 by DW 17 | flUseSes: true 18 | }; 19 | 20 | var mailTransport; //1/23/23 by DW 21 | 22 | const maxMailsPerSec = 10; //1/5/20 by DW -- there's now a queue that limits mail to ten per second 23 | var mailQueue = new Array (); 24 | var flQueueThreadRunning = false; 25 | var mailQueueInterval; 26 | 27 | function checkMailQueue () { 28 | if (mailQueue.length > 0) { 29 | var item = mailQueue.shift (); //remove first element 30 | sendActualMail (item.recipient, item.subject, item.message, item.sender, item.callback, item.attachments); 31 | } 32 | else { 33 | if (flQueueThreadRunning) { 34 | clearInterval (mailQueueInterval); 35 | flQueueThreadRunning = false; 36 | } 37 | } 38 | } 39 | function sendRawEmail (recipient, subject, message, sender, attachments, callback) { //11/18/19 by DW 40 | var mailtext = ""; 41 | function add (s) { 42 | mailtext += s + "\n"; 43 | } 44 | 45 | add ("From: " + sender); 46 | add ("To: " + recipient); 47 | add ("Subject: " + subject); 48 | add ("MIME-Version: 1.0"); 49 | add ("Content-Type: multipart/mixed; boundary=\"NextPart\"\n"); 50 | add ("--NextPart"); 51 | add ("Content-Type: text/html\n"); 52 | add (message + "\n"); 53 | add ("--NextPart"); 54 | if (attachments !== undefined) { 55 | attachments.forEach (function (item) { 56 | add ("Content-Type: " + item.type + "; name=\"" + item.name + "\""); 57 | add ("Content-Transfer-Encoding: base64"); 58 | add ("Content-Disposition: attachment"); 59 | add (item.data.toString ("base64") + "\n"); 60 | add ("--NextPart--"); 61 | }); 62 | } 63 | 64 | var params = { 65 | RawMessage: {Data: mailtext}, 66 | Source: "dave@scripting.com" 67 | }; 68 | 69 | ses.sendRawEmail (params, function (err, data) { 70 | if (err) { 71 | console.log ("\nsendRawEmail: err.message == " + err.message); 72 | } 73 | if (callback !== undefined) { 74 | callback (err, data); 75 | } 76 | }); 77 | } 78 | function sendActualMail (recipient, subject, message, sender, callback, attachments) { 79 | if (attachments === undefined) { 80 | if (config.flUseSes) { 81 | var theMail = { 82 | Source: sender, 83 | ReplyToAddresses: [sender], 84 | ReturnPath: sender, 85 | Destination: { 86 | ToAddresses: [recipient] 87 | }, 88 | Message: { 89 | Body: { 90 | Html: { 91 | Charset: "UTF-8", 92 | Data: message 93 | }, 94 | Text: { 95 | Charset: "UTF-8", 96 | Data: utils.stripMarkup (message) 97 | } 98 | }, 99 | Subject: { 100 | Data: subject 101 | } 102 | }, 103 | }; 104 | console.log ("sendActualMail via SES: recipient == " + recipient); //7/11/25 by DW 105 | ses.sendEmail (theMail, function (err, data) { 106 | if (err) { 107 | console.log ("\nsendActualMail via SES: err.message == " + err.message); 108 | } 109 | else { 110 | console.log ("\nsendActualMail via SES: data == " + utils.jsonStringify (data)); //7/11/25 by DW 111 | } 112 | if (callback !== undefined) { //8/18/19 by DW 113 | callback (err, data); 114 | } 115 | }); 116 | } 117 | else { 118 | const mailOptions = { 119 | to: recipient, 120 | subject, 121 | html: message, 122 | text: utils.stripMarkup (message), 123 | from: sender 124 | }; 125 | mailTransport.sendMail (mailOptions, function (err, data) { 126 | if (err) { 127 | console.log ("\nsendMail: err.message == " + err.message); 128 | } 129 | if (callback !== undefined) { 130 | callback (err, data); 131 | } 132 | }); 133 | } 134 | } 135 | else { 136 | sendRawEmail (recipient, subject, message, sender, attachments, callback); 137 | } 138 | } 139 | function sendMail (recipient, subject, message, sender, callback, attachments) { 140 | mailQueue.push ({ 141 | recipient, subject, message, sender, callback, attachments 142 | }); 143 | if (!flQueueThreadRunning) { 144 | flQueueThreadRunning = true; 145 | mailQueueInterval = setInterval (checkMailQueue, 1000 / maxMailsPerSec); 146 | } 147 | } 148 | function start (options) { //1/23/23 by DW 149 | var flUseSes = (options.flUseSes === undefined) ? true : utils.getBoolean (options.flUseSes); 150 | config.flUseSes = flUseSes; //so davemail routines know whether to use SMTP or SES 151 | if (!flUseSes) { //using SMTP 152 | const nodemailerOptions = { 153 | host: options.smtpHost, 154 | port: options.port, 155 | secure: (options.port == 465), 156 | auth: { 157 | user: options.username, 158 | pass: options.password 159 | } 160 | }; 161 | mailTransport = nodemailer.createTransport (nodemailerOptions); 162 | } 163 | } 164 | --------------------------------------------------------------------------------