public
Last active

Node.js email handling examples

  • Download Gist
.gitignore
1 2
config.json
reading-image.png
README.md
Markdown

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

This uses the following libraries:

Running

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

Run coffee send.coffee to send yourself an email with an attachment.

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

config.json.example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{
"name": "My Name",
"email": "user@domain.com",
"username": "user@gmail.com",
"password": "MYPASSWORD",
"imap": {
"host": "imap.gmail.com",
"port": 993,
"secure": true
},
"smtp": {
"host": "smtp.gmail.com",
"ssl": true
}
}
read.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
# 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
host: config.imap.host
port: config.imap.port
secure: config.imap.secure
 
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 #{box.messages.total} messages in your INBOX"
 
server.search ["UNSEEN", ["SINCE", "Sep 18, 2011"], ["FROM", config.email]], (err, results) ->
exitOnErr err if err
 
unless results.length
console.log "No unread messages from #{config.email}"
do server.logout
return
 
fetch = server.fetch results,
request:
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
send.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# This script will send an image as an email attachment to the
# user himself. The receiving part of this is in read.coffee
 
# 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
host: config.smtp.host
ssl: config.smtp.ssl
 
message = email.message.create
text: "This is test"
from: "#{config.name} <#{config.email}>"
to: "#{config.name} <#{config.email}>"
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']}"

missing ',' in your config.json example for username makes parse fail

@jdross Thanks, fixed!

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.

There are some things that have changed with the parser. I fixed it up here: https://gist.github.com/4283888

Hello,

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.

regards,
Mark

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.