Commits

Anonymous committed 2305412

handle max emails

Comments (0)

Files changed (3)

flaskext/mail/connection.py

         
         :param message: Message instance.
         """
-        self.num_emails += 1
 
         if self.host:
             self.host.sendmail(message.sender,
                                message.recipients,
                                str(message.encoded()))
         
-            if self.num_emails >= self.max_emails:
-                
-                self.host = self.configure_host()
 
         if email_dispatched:
             email_dispatched.send(message, app=self.app)
 
+        self.num_emails += 1
+
+        if self.num_emails == self.max_emails:
+            
+            self.num_emails = 0
+            if self.host:
+                self.host = self.configure_host()
 
     def send_message(self, *args, **kwargs):
         """

flaskext/mail/message.py

                 base.attach_text(self.body, 'text/plain')
 
             if self.html:
-                self.base.attach_text(self.html, 'text/html')
+                base.attach_text(self.html, 'text/html')
 
             for attachment in self.attachments:
                 attachment.encoded(base)

tests/__init__.py

 
         assert len(msg.recipients) == 0
 
-
     def test_add_recipient(self):
 
         msg = Message("testing")
 
         base = msg.to_base()
 
-        assert False, base.headers
+        assert base.headers['Subject'] == "testing"
+        assert base.headers['To'] == ['to@example.com']
+        assert base.headers['From'] == 'support@mysite.com'
+        
+        assert base.body == "testing"
+        assert base.content_encoding['Content-Type'][0] == 'text/plain'
+
+    def test_to_base_html(self):
+
+        msg = Message(subject="testing",
+                      recipients=["to@example.com"],
+                      body="testing",
+                      html="<b>testing</b>")
+
+        base = msg.to_base()
+
+        assert base.headers['Subject'] == "testing"
+        assert base.headers['To'] == ['to@example.com']
+        assert base.headers['From'] == 'support@mysite.com'
+        
+        assert base.body is None
+        assert base.content_encoding['Content-Type'][0] == 'multipart/alternative'
 
     def test_attach(self):
 
 
             assert len(outbox) == 100
 
+    def test_max_emails(self):
+        
+        messages = []
+
+        with self.mail.record_messages() as outbox:
+            with self.mail.connect(max_emails=10) as conn:
+                for i in xrange(100):
+                    msg = Message(subject="testing",
+                                  recipients=["to@example.com"],
+                                  body="testing")
+        
+                    conn.send(msg)
+
+                    print conn.num_emails
+                    if i % 10 == 0:
+                        assert conn.num_emails == 1
+
+            assert len(outbox) == 100
 
 class TestEncoding(TestCase):