Take files from a gulp pipeline and convert to a MIME file for emailing
npm install gulp-target-mimeThis is a gulp plugin which is a wrapper for mailcomposer. It produces an standard raw MIME/.eml email file suitable for sending as an email using gulp-email-adapter, and runs in one of two modes:
- normal mode takes a json file describing an email, using it to produce the email file
- filesAreAttachments mode takes a file of any type and uses it as an attachment on the resulting email file
The simplest way to get started is to start with file(s) like mail.json (a file whose properties match E-mail message fields):
`` json`
{
"from": "test@test.com",
"to": "me@gmail.com",
"subject": "This is a test!",
"text": "nothing really to say..."
}
This sample gulpfile.js takes mail.json and creates a mime file, then sends it using gulp-email-adapter
` javascript
import { targetMime } from 'gulp-target-mime'
// or: var targetMime = require('gulp-target-mime').targetMime
import { emailAdapter } from 'gulp-email-adapter'
// or: var emailAdapter = require('gulp-email-adapter').emailAdapter
// options for sending using AWS SES
var emailOptions = { "accessKeyId": "(your access key)", "secretAccessKey": "(your access secret)", "region": "(enter region)" };
function createAndSendEmails(callback: any) {
return gulp.src('data/mail.json')
.pipe(targetMime()) // create MIME file from incoming file (e.g. mail.json)
.pipe(emailAdapter(emailOptions})) // send MIME files as emails using emailAdapter
}
exports.default = createAndSendEmails
`
- configObj is an optional object whose properties match E-mail message fields. You can pass a configObj to override properties of your email files:
` javascriptmyemail@live.com
let configObj = {from: 'myemail@live.com' }
// ...
.pipe(targetMime({configObj})) // every email will come from , overriding the from property in mail.json`
// ...
- filesAreAttachments is an optional second parameter (default is false) that can switch from receiving files like mail.json (as described above) to instead receiving
files of any type to be treaded as attachments.
#### filesAreAttachments mode
` javascriptfilesAreAttachments
// ...
// since is true, configObj must contain all information (besides attachments) for the emailfilesAreAttachments
let configObj = {
"from": "logger@test.com",
"to": "admin@test.com",
"subject": "Log File",
"text": "Log file is attached"
}
return gulp.src('logs/**.log')
.pipe(targetMime({configObj}, true)) // is true, so each .log file is treated as an attachment for its own email`
.pipe(emailAdapter(emailOptions})) // send MIME files as emails using emailAdapter
}
// ...
filesAreAttachments mode treats each file as its own email, but what if you need to attach multiple files to a single email? You can do that using configObj ormail.json
with your -type files using this info to set it up manually. But here's a fancy way togulp
do it with a separate task which runs first to collect the attachments you want and make an array of objects out of them. That array can then
be used to populate the outgoing email.
` javascript
let attachmentArr:any = []
function collectAttachments(callback: any) {
return gulp.src(['data/.','!data/mail.json'])
.on('data', function (file:Vinyl) {
attachmentArr.push(
{
filename: file.basename,
content: file.contents
})
})
}
function createAndSendEmails(callback: any) {
return gulp.src('data/mail.json')
.pipe(targetMime({attachments: attachmentArr}))
.pipe(gulp.dest('output/'))
}
exports.default = gulp.series(collectAttachments, createAndSendEmails)
`
configObj can be omitted in favor of the API suggested by gulp-data. Our implementation looks for both a targetMimegulp-target-mime
property and , avoiding interference with other plugins that may look for their own config properties in similar way.
` javascript
import { targetMime } from 'gulp-target-mime'
var data = require('gulp-data');
// ...
return gulp.src('logs/**.log')
.pipe(data(function(file) {
return {
targetMime: {
"from": "logger@test.com",
"to": "admin@test.com",
"subject": file.basename, // this property is specific to the current file, which is not possible when passing configObj to targetMime below
"text": "Log file is attached"
}
}
}))
.pipe(targetMime({}, true)) // we can pass a blank configObj here
// ...
`
If there is a conflict, the properties from configObj will be overriden, as the targetMime/gulp-target-mime properties are specific to the particular file and thus more granular.
- Dependencies:
- git
- nodejs - At least v6.3 (6.9 for Windows) required for TypeScript debugging
- npm (installs with Node)
- typescript - installed as a development dependency
- Clone this repo and run npm install to install npm packagesOpen Folder
- Debug: with VScode use to open the project folder, then hit F5 to debug. This runs without compiling to javascript using ts-nodenpm test
- Test: or npm tnpm run build
- Compile to javascript:
We are using Jest for our testing. Each of our tests are in the test folder.
- Run npm test` to run the test suites
Note: This document is written in Markdown. We like to use Typora and Markdown Preview Plus for our Markdown work..