Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Changed structure of files #1

Merged
merged 3 commits into from

2 participants

@alekzvik
  • Moved the Mail class from __init__.py to mail.py.
  • Added .gitignore
  • Fixed some typos
@ajford ajford merged commit b278e28 into ajford:master
@ajford
Owner

Thanks

@ajford ajford referenced this pull request from a commit
@ajford Closes #1 with workaround ref'd by mitar 787832a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 .gitignore
@@ -0,0 +1,7 @@
+*.pyc
+
+# build artifacts from python setup.py build or python setup.py install
+build/
+dist/
+*egg*/
+docs/_build
View
59 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,6 @@
: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
+from .message import Message, BadHeaderError
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
57 flask_sendmail/mail.py
@@ -0,0 +1,57 @@
+from .message import Message
+from .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)
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)
Something went wrong with that request. Please try again.