Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Forked from alexiskattan/.gitignore
Last active August 29, 2015 14:12
Show Gist options
  • Save aaronksaunders/ce01905673f0a4973cc4 to your computer and use it in GitHub Desktop.
Save aaronksaunders/ce01905673f0a4973cc4 to your computer and use it in GitHub Desktop.

Revisions

  1. @alexiskattan alexiskattan revised this gist Dec 14, 2012. 1 changed file with 6 additions and 14 deletions.
    20 changes: 6 additions & 14 deletions read.coffee
    Original file line number Diff line number Diff line change
    @@ -50,21 +50,13 @@ server.connect (err) ->
    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"

    parser.on "attachment", (attachment)->
    console.log attachment.generatedFileName
    output = fs.createWriteStream(attachment.generatedFileName)
    attachment.stream.pipe(output)

    message.on "data", (data) ->
    parser.feed data.toString()
    parser.write data.toString()

    message.on "end", ->
    do parser.end
  2. @bergie bergie revised this gist Apr 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion config.json.example
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    {
    "name": "My Name"
    "name": "My Name",
    "email": "[email protected]",
    "username": "[email protected]",
    "password": "MYPASSWORD",
  3. @bergie bergie revised this gist Sep 19, 2011. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,15 @@
    Some examples of sending and receiving emails with Node.js
    Some examples of sending and receiving emails with Node.js.

    This uses the following libraries:

    * [emailjs](https://github.com/eleith/emailjs) for sending email
    * [node-imap](https://github.com/mscdex/node-imap) for receiving email
    * [mailparser](https://github.com/andris9/mailparser) for parsing received emails

    ## 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.
  4. @bergie bergie revised this gist Sep 19, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    config.json
    reading-image.png
  5. @bergie bergie revised this gist Sep 19, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Some examples of sending and receiving emails with Node.js
  6. @bergie bergie revised this gist Sep 19, 2011. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions config.json.example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    {
    "name": "My Name"
    "email": "[email protected]",
    "username": "[email protected]",
    "password": "MYPASSWORD",
    "imap": {
    "host": "imap.gmail.com",
    "port": 993,
    "secure": true
    },
    "smtp": {
    "host": "smtp.gmail.com",
    "ssl": true
    }
    }
  7. @bergie bergie created this gist Sep 19, 2011.
    73 changes: 73 additions & 0 deletions read.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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
    Binary file added reading.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
    27 changes: 27 additions & 0 deletions send.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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']}"
  8. @bergie bergie created this gist Sep 19, 2011.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Some examples of sending and receiving emails with Node.js