Skip to content

Commit 8c2ef44

Browse files
committed
Adding some fixes
1 parent e0de39c commit 8c2ef44

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

flask_sendmail/__init__.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
"""
2-
Module provides an interface to the system's sendmail client.
2+
flask_sendmail
3+
~~~~~~~~~~~~~~
4+
5+
Module provides an interface to the system's sendmail client.
36
4-
It's based heavily off of Flask-Mail (originaly by danjac),
5-
and owes a majority of its code to him.
7+
It's based heavily off of Flask-Mail (written by danjac),
8+
and owes a majority of its code to it.
69
10+
It's also designed to be a nearly complete drop-in replacement
11+
for Flask-Mail.
12+
13+
:copyright: (c) 2012 by Anthony Ford.
14+
:license: None, see LICENSE for more details.
715
"""
8-
from flask.ext.sendmail.message import Message
9-
from flask.ext.sendmail.connection import Connection
16+
from flask_sendmail.message import Message, BadHeaderError
17+
from flask_sendmail.connection import Connection
1018

11-
class Mailer(object):
19+
class Mail(object):
1220

1321
def __init__(self,app=None):
1422
if app is not None:
@@ -18,7 +26,7 @@ def init_app(self, app):
1826
"""
1927
Initializes your mail settings from app.config
2028
21-
Can be used to set up the Mailer at configuration time
29+
Can be used to set up Mail at configuration time
2230
2331
:param app: Flask application instance
2432
"""
@@ -28,19 +36,19 @@ def init_app(self, app):
2836
self.mailer_flags = app.config.get('MAIL_MAILER_FLAGS','-t')
2937
self.suppress = app.config.get('MAIL_SUPPRESS_SEND', False)
3038
self.fail_silently = app.config.get('MAIL_FAIL_SILENTLY', True)
31-
39+
self.max_emails=None
3240
self.suppress = self.suppress or app.testing
3341
self.app = app
3442

35-
#register extenshion with app
43+
#register extension with app
3644
app.extensions = getattr(app, 'extensions', {})
3745
app.extensions['sendmail'] = self
3846

3947
def send(self,message):
4048
"""
4149
Sends message through system's sendmail client.
4250
43-
:param message: Mailer Message instance
51+
:param message: Mail Message instance
4452
"""
4553

4654
with self.connect() as connection:
@@ -60,4 +68,4 @@ def connect(self, max_emails=None):
6068
Opens a connection to the system's sendmail client.
6169
"""
6270

63-
return Connection(self, max_email)
71+
return Connection(self, max_emails)

flask_sendmail/connection.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from subprocess import PIPE,STDOUT,Popen
22

3+
from flask_sendmail.message import Message
4+
35

46
class Connection(object):
57
"""Handles connection to host."""
@@ -12,10 +14,26 @@ def __init__(self, mail, max_emails=None):
1214
self.max_emails = max_emails or self.mail.max_emails or 0
1315
self.fail_silently = self.mail.fail_silently
1416

17+
def __enter__(self):
18+
pass
19+
1520
def send(self,message):
1621
sm = Popen([self.mailer,self.mailer_flags], stdin=PIPE, stdout=PIPE,
1722
stderr=STDOUT)
1823
sm.stdin.write(message.dump())
1924
sm.communicate()
2025

2126
return sm.returncode
27+
28+
def __exit__(self, exc_type, exc_value, tb):
29+
pass
30+
31+
def send_message(self, *args, **kwargs):
32+
"""
33+
Shortcut for send(msg).
34+
35+
Takes same arguments as Message constructor.
36+
"""
37+
38+
self.send(Message(*args, **kwargs))
39+

flask_sendmail/message.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ def __init__(self, subject, recipients=None, body=None, html=None,
3131
app = stack.top.app
3232
sender = app.config.get("DEFAULT_MAIL_SENDER")
3333

34-
if isinstance(sender, tuple):
35-
# sender can be tuple of (name, address)
36-
sender = "%s <%s>" % sender
37-
3834
self.subject = subject
3935
self.sender = sender
4036
self.body = body
@@ -58,7 +54,7 @@ def add_recipient(self, recipient):
5854
"""
5955
Adds another recipient to the message.
6056
61-
:param recipient: email address of recipient.
57+
:param recipient: email address.
6258
"""
6359

6460
self.recipients.append(recipient)
@@ -74,20 +70,31 @@ def is_bad_headers(self):
7470
if c in val:
7571
return True
7672
return False
77-
73+
7874
def dump(self):
7975
if self.html:
8076
msg = MIMEText(self.html,'html')
8177
elif self.body:
8278
msg = MIMEText(self.body)
79+
80+
if isinstance(self.sender, tuple):
81+
# sender can be tuple of (name, address)
82+
self.sender = "%s <%s>" % self.sender
83+
8384

8485
msg['Subject'] = self.subject
8586
msg['To'] = ', '.join(self.recipients)
8687
msg['From'] = self.sender
8788
if self.cc:
88-
msg['CC'] = self.cc
89+
if hasattr(self.cc,'__iter__'):
90+
msg['Cc'] = ', '.join(self.cc)
91+
else:
92+
msg['Cc'] = self.cc
8993
if self.bcc:
90-
msg['BCC'] = self.bcc
94+
if hasattr(self.bcc,'__iter__'):
95+
msg['Bcc'] = ', '.join(self.bcc)
96+
else:
97+
msg['Bcc'] = self.bcc
9198
if self.reply_to:
9299
msg['Reply-To'] = self.reply_to
93100

0 commit comments

Comments
 (0)