Giter Site home page Giter Site logo

smtproutes's Introduction

SMTPRoutes

SMTPRoutes is a light weight SMTP server built on top of Secure-SMTPD.

It's what you'd get if Sinatra and SMTP had a baby.

Installation

$ easy_install smtproutes

Note: You must use easy_install here. If you use pip there will be conflicting dependencies and things won't work.

Routes

Routes are specified via a regex provided in the route decorator.

from smtproutes import Route
from smtproutes.decorators import route

class ExampleRoute(Route):
	@route(r'myroute@.*')
    def my_route(self):
		print self.mailfrom.email

When invoked a route will have access to the following instance variables:

  • self.message the parsed email message.
  • self.mailfrom a contact object indicating who the message was received from.
  • self.tos an array of contact objects extracted from the To field.
  • self.ccs an array of contact objects extracted from the CC field.
  • self.bccs an array of contact objects extracted from the BCC field.

Any named groups specified in the route regex will be availble as instance variables.

class ExampleRoute(Route):
    
	@route(r'(?P<prefix>open)@(?P<suffix>.*)')
    def open_route(self):
        print "%s at %s sent the message: \n\n %s" % (
            self.prefix,
            self.suffix,
            self.message
        )

Sender Authentication

Email is vulnerable to spoofing attacks. SMTPRoutes allows you to provide an authentication object to protect against these.

An authentication class can be provided in the sender_auth kwarg of a route decorator.

@route(r'(?P<prefix>spf)@(?P<suffix>.*)', sender_auth=SPFAuth)
def spf_route(self):
    print "%s at %s sent the message: \n\n %s" % (
        self.prefix,
        self.suffix,
        self.message
    )

Currently the following sender authentication methods are supported:

You can provide multiple authentication approaches in the sender_auth kwarg, if any pass the route will be called:

@route(r'(?P<prefix>spf_google)@(?P<suffix>.*)', sender_auth=[SPFAuth, GmailSPFAuth])
def google_apps_spf_route(self):
    print "%s at %s sent the message: \n\n %s" % (
        self.prefix,
        self.suffix,
        self.message
    )

Running a Server

The server is a thin abstraction on top of secure-smtpd (https://github.com/bcoe/secure-smtpd).

  • SSL is supported.
  • Basic SMTP authentication is supported.

Create an instance of the server using any of the configuration options specified in the secure-smtpd project.

from smtproutes import Server

server = Server(('0.0.0.0', 25), None)

Once the server is created, you can register routes with it and start it running:

from example_route import ExampleRoute
server.add_route(ExampleRoute).start()

The server will now be listening on port 25 for inbound SMTP messages.

The Contact Objects

self.mailfrom, self.tos, self.ccs, and self.bccs each contain instances of contact objects:

  • contact.name the name extracted from the email address.
  • contact.email the email of the contact.

The Message Object

self.message is available as an instance variable when a route is executed.

self.messsage is a subclass of email.message.Message described here: http://docs.python.org/library/email.message.html#module-email.message

Extended functionality:

  • message.body the plain text body of the message.
  • message.attachments the attachments on the message.

Additional Support for Attachments

self.message has been extended upon to include an attachments property, which contains a list of pre-processed attachments:

  • attachment.filename the filename of the attachment.
  • attachment.data the binary data of the attachment.
  • attachment.extension the file extension of the attachment.
  • attachment.mime_type the mime type of the attachment.

smtproutes's People

Contributors

bcoe avatar

Watchers

James Cloos avatar  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.