Skip to content
Browse files

Changed structure of the files.

  • Loading branch information...
1 parent 519a02c commit 27da66b93f18094a697f120b73ac4fbf987b1cfb @alekzvik committed Apr 21, 2012
Showing with 21 additions and 75 deletions.
  1. +2 −56 flask_sendmail/__init__.py
  2. +4 −5 flask_sendmail/connection.py
  3. +15 −14 flask_sendmail/message.py
View
58 flask_sendmail/__init__.py
@@ -1,7 +1,7 @@
"""
flask_sendmail
~~~~~~~~~~~~~~
-
+
Module provides an interface to the system's sendmail client.
It's based heavily off of Flask-Mail (written by danjac),
@@ -13,59 +13,5 @@
:copyright: (c) 2012 by Anthony Ford.
:license: None, see LICENSE for more details.
"""
-from flask_sendmail.message import Message, BadHeaderError
-from flask_sendmail.connection import Connection
-
-class Mail(object):
-
- def __init__(self,app=None):
- if app is not None:
- self.init_app(app)
-
- def init_app(self, app):
- """
- Initializes your mail settings from app.config
-
- Can be used to set up Mail at configuration time
-
- :param app: Flask application instance
- """
-
- self.debug = app.config.get('MAIL_DEBUG',app.debug)
- self.mailer = app.config.get('MAIL_MAILER','/usr/sbin/sendmail')
- self.mailer_flags = app.config.get('MAIL_MAILER_FLAGS','-t')
- self.suppress = app.config.get('MAIL_SUPPRESS_SEND', False)
- self.fail_silently = app.config.get('MAIL_FAIL_SILENTLY', True)
- self.max_emails=None
- self.suppress = self.suppress or app.testing
- self.app = app
-
- #register extension with app
- app.extensions = getattr(app, 'extensions', {})
- app.extensions['sendmail'] = self
-
- def send(self,message):
- """
- Sends message through system's sendmail client.
-
- :param message: Mail Message instance
- """
-
- with self.connect() as connection:
- message.send(connection)
-
- def send_message(self, *args, **kwargs):
- """
- Shortcut for send(msg).
-
- Takes same arguments as Message constructor.
- """
-
- self.send(Message(*args, **kwargs))
-
- def connect(self, max_emails=None):
- """
- Opens a connection to the system's sendmail client.
- """
- return Connection(self, max_emails)
+from .mail import Mail
View
9 flask_sendmail/connection.py
@@ -1,4 +1,4 @@
-from subprocess import PIPE,STDOUT,Popen
+from subprocess import PIPE, STDOUT, Popen
from flask_sendmail.message import Message
@@ -17,12 +17,12 @@ def __init__(self, mail, max_emails=None):
def __enter__(self):
pass
- def send(self,message):
- sm = Popen([self.mailer,self.mailer_flags], stdin=PIPE, stdout=PIPE,
+ def send(self, message):
+ sm = Popen([self.mailer, self.mailer_flags], stdin=PIPE, stdout=PIPE,
stderr=STDOUT)
sm.stdin.write(message.dump())
sm.communicate()
-
+
return sm.returncode
def __exit__(self, exc_type, exc_value, tb):
@@ -36,4 +36,3 @@ def send_message(self, *args, **kwargs):
"""
self.send(Message(*args, **kwargs))
-
View
29 flask_sendmail/message.py
@@ -1,11 +1,13 @@
from email.mime.text import MIMEText
try:
- from flask import _app_ctx_stack as stack
+ from flask import _app_ctx_stack as stack
except ImportError:
- from flask import _request_ctx_stack as stack
+ from flask import _request_ctx_stack as stack
-class BadHeaderError(Exception): pass
+
+class BadHeaderError(Exception):
+ pass
class Message(object):
@@ -25,7 +27,7 @@ class Message(object):
def __init__(self, subject, recipients=None, body=None, html=None,
sender=None, cc=None, bcc=None, attachments=None,
- reply_to=None):
+ reply_to=None):
if sender is None:
app = stack.top.app
@@ -49,7 +51,7 @@ def __init__(self, subject, recipients=None, body=None, html=None,
attachments = []
self.attachments = attachments
-
+
def add_recipient(self, recipient):
"""
Adds another recipient to the message.
@@ -70,28 +72,27 @@ def is_bad_headers(self):
if c in val:
return True
return False
-
+
def dump(self):
if self.html:
- msg = MIMEText(self.html,'html')
+ msg = MIMEText(self.html, 'html')
elif self.body:
msg = MIMEText(self.body)
if isinstance(self.sender, tuple):
# sender can be tuple of (name, address)
self.sender = "%s <%s>" % self.sender
-
-
+
msg['Subject'] = self.subject
- msg['To'] = ', '.join(self.recipients)
+ msg['To'] = ', '.join(self.recipients)
msg['From'] = self.sender
if self.cc:
- if hasattr(self.cc,'__iter__'):
+ if hasattr(self.cc, '__iter__'):
msg['Cc'] = ', '.join(self.cc)
else:
msg['Cc'] = self.cc
if self.bcc:
- if hasattr(self.bcc,'__iter__'):
+ if hasattr(self.bcc, '__iter__'):
msg['Bcc'] = ', '.join(self.bcc)
else:
msg['Bcc'] = self.bcc
@@ -103,7 +104,7 @@ def dump(self):
def send(self, connection):
"""
Verifies and sends the message.
-
+
:param connection: Connection instance
"""
@@ -113,5 +114,5 @@ def send(self, connection):
if self.is_bad_headers():
raise BadHeaderError
-
+
connection.send(self)

0 comments on commit 27da66b

Please sign in to comment.
Something went wrong with that request. Please try again.