Giter Club home page Giter Club logo

mini-smtp-server's Introduction

mini-smtp-server

MiniSmtpServer is a small and easily customizable SMTP server. It is designed mainly to be integrated into other systems that will decide what to do with the mail that is received. By default mini-smtp-server operates as a black-hole and does not do anything with the mail being sent to it… Don’t let that discourage you though, as MiniSmtpServer is designed to be extremely easy to customize which makes it fit more easily into your system!

Using the server

To run the default server simply use:

# Create a new server instance listening at 127.0.0.1:2525
# and accepting a maximum of 4 simultaneous connections
server = MiniSmtpServer.new(2525, "127.0.0.1", 4)

# Start the server
server.start

# ...
# serving requests & doing other work here
# ...

# Shutdown the server without interrupting any connections:
server.shutdown
while(server.connections > 0)
  sleep 0.01
end
server.stop
server.join

Installation

For convenience MiniSmtpServer is packaged as a RubyGem, to install it simply enter the following at your command line:

gem install mini-smtp-server

Customizing the server

MiniSmtpServer is designed to be easy to customize via subclassing. Simply subclass the ‘MiniSmtpServer` class and re-define the `new_message_event` event handler:

# This is an SMTP server that logs all
# the messages it receives to STDOUT
class StdoutSmtpServer < MiniSmtpServer

  def new_message_event(message_hash)
    puts "# New email received:"
    puts "-- From: #{message_hash[:from]}"
    puts "-- To:   #{message_hash[:to]}"
    puts "--"
    puts "-- " + message_hash[:data].gsub(/\r\n/, "\r\n-- ")
    puts
  end

end

Then create an instance of your new server and run it like you would normally:

server = StdoutSmtpServer.new
server.start
server.join

The possibilities are endless, and easy to implement! Want all your email stored in a database? No worries:

class DatabaseSmtpServer < MiniSmtpServer
  def new_message_event(message_hash)
    # Imagine we have an ActiveRecord model named 'Email'
    Email.create(message_hash)
  end
end

# Presto!

If you have a great example of something you’ve done with MiniSmtpServer let me know and I will add it here!

Author & Credits

Author

Aaron Gough

MiniSmtpServer is based on code originally written by Peter Cooper

Copyright © 2010 Aaron Gough (thingsaaronmade.com), released under the MIT license

mini-smtp-server's People

Contributors

aarongough avatar

Watchers

Orang Utan avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.