Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2011 15:50
Show Gist options
  • Save bergie/1226809 to your computer and use it in GitHub Desktop.
Save bergie/1226809 to your computer and use it in GitHub Desktop.
Node.js email handling examples

Some examples of sending and receiving emails with Node.js.

This uses the following libraries:


Copy config.json.example to config.json and enter your email account details.

Run coffee to send yourself an email with an attachment.

Run coffee to receive the sent email and write the attachment back to disk with a new name.

"name": "My Name",
"email": "",
"username": "",
"password": "MYPASSWORD",
"imap": {
"host": "",
"port": 993,
"secure": true
"smtp": {
"host": "",
"ssl": true
# This example script opens an IMAP connection to the server and
# seeks unread messages sent by the user himself. It will then
# download those messages, parse them, and write their attachments
# to disk.
# Install node-imap with `npm install imap`
imap = require "imap"
# Install mailparser with `npm install mailparser`
mailparser = require "mailparser"
# You need a config file with your email settings
fs = require "fs"
config = JSON.parse fs.readFileSync "#{process.cwd()}/config.json", "utf-8"
server = new imap.ImapConnection
username: config.username
password: config.password
port: config.imap.port
exitOnErr = (err) ->
console.error err
do process.exit
server.connect (err) ->
exitOnErr err if err
server.openBox "INBOX", false, (err, box) ->
exitOnErr err if err
console.log "You have #{} messages in your INBOX" ["UNSEEN", ["SINCE", "Sep 18, 2011"], ["FROM",]], (err, results) ->
exitOnErr err if err
unless results.length
console.log "No unread messages from #{}"
do server.logout
fetch = server.fetch results,
body: "full"
headers: false
fetch.on "message", (message) ->
fds = {}
filenames = {}
parser = new mailparser.MailParser
parser.on "headers", (headers) ->
console.log "Message: #{headers.subject}"
parser.on "astart", (id, headers) ->
filenames[id] = headers.filename
fds[id] = fs.openSync headers.filename, 'w'
parser.on "astream", (id, buffer) ->
fs.writeSync fds[id], buffer, 0, buffer.length, null
parser.on "aend", (id) ->
return unless fds[id]
fs.close fds[id], (err) ->
return console.error err if err
console.log "Writing #{filenames[id]} completed"
message.on "data", (data) ->
parser.feed data.toString()
message.on "end", ->
do parser.end
fetch.on "end", ->
do server.logout
# This script will send an image as an email attachment to the
# user himself. The receiving part of this is in
# Install EmailJS with `npm install emailjs`
email = require "emailjs"
# You need a config file with your email settings
fs = require "fs"
config = JSON.parse fs.readFileSync "#{process.cwd()}/config.json", "utf-8"
server = email.server.connect
user: config.username
password: config.password
ssl: config.smtp.ssl
message = email.message.create
text: "This is test"
from: "#{} <#{}>"
to: "#{} <#{}>"
subject: "Testing Node.js email capabilities"
message.attach "reading.png", "image/png", "reading-image.png"
server.send message, (err, message) ->
return console.error err if err
console.log "Message sent with id #{message['header']['message-id']}"
Copy link

Hiya, thanks for this great gist.

Maybe I'm missing something, but as of trying it out today I'm getting an error with "parser.feed". Maybe mailparser changed it's API, b/c now it appears to be "parser.write".

Just a headsup in case anyone else hits that hiccup.

Copy link

There are some things that have changed with the parser. I fixed it up here:

Copy link


I am using emailjs and I check the document that there is no reply to function built in. Is there anyway we get get around with this as I really need it.


Copy link

Hello Sir,
I am trying to use u r code for sending and receiving mail purpose.I am able to send msgs with attachment but while m receiving it gives error .

imap.ImapConnection is not a function.

can u please look into that.


Copy link

jdrydn commented Jan 3, 2016

Lol. I just used inbox and .pipe(parser), and parser.on('end', function (email) { ... }); and it's working like a charm!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment