Ver Fonte

Remove 'mailer' dependency. Thanks to zen4ever

Ask Solem há 14 anos atrás
pai
commit
f4ad4f1cb6
4 ficheiros alterados com 51 adições e 12 exclusões
  1. 51 9
      celery/utils/mail.py
  2. 0 1
      contrib/requirements/default.txt
  3. 0 1
      setup.cfg
  4. 0 1
      setup.py

+ 51 - 9
celery/utils/mail.py

@@ -1,4 +1,48 @@
-from mailer import Message, Mailer
+import smtplib
+
+try:
+    from email.mime.text import MIMEText
+except ImportError:
+    from email.MIMEText import MIMEText
+
+class Message(object):
+
+    def __init__(self, to=None, sender=None, subject=None, body=None,
+            charset="us-ascii"):
+        self.to = to
+        self.sender = sender
+        self.subject = subject
+        self.body = body
+
+        if not isinstance(self.to, (list, tuple)):
+            self.to = [self.to]
+
+    def __str__(self):
+        msg = MIMEText(self.body, "plain", self.charset)
+        msg["Subject"] = self.subject
+        msg["From"] = self.sender
+        msg["To"] = ", ".join(self.to)
+        return msg.as_string()
+
+
+class Mailer(object):
+
+    def __init__(self, host="localhost", port=0, user=None, password=None):
+        self.host = host
+        self.port = port
+        self.user = user
+        self.password = password
+
+    def send(self, message):
+        client = smtplib.SMTP(self.host, self.port)
+
+        if self.user and self.password:
+            server.login(self.user, self.password)
+
+        client.sendmail(message.sender, message.to, str(message))
+        client.quit()
+
+
 
 def mail_admins(subject, message, fail_silently=False):
     """Send a message to the admins in conf.ADMINS."""
@@ -7,16 +51,14 @@ def mail_admins(subject, message, fail_silently=False):
     if not conf.ADMINS:
         return
 
-    to = ", ".join(admin_email for _, admin_email in conf.ADMINS)
-    username = conf.EMAIL_HOST_USER
-    password = conf.EMAIL_HOST_PASSWORD
-
-    message = Message(From=conf.SERVER_EMAIL, To=to,
-                      Subject=subject, Body=message)
+    to = [admin_email for _, admin_email in conf.ADMINS]
+    message = Message(sender=conf.SERVER_EMAIL, to=to,
+                      subject=subject, body=message)
 
     try:
-        mailer = Mailer(conf.EMAIL_HOST, conf.EMAIL_PORT)
-        username and mailer.login(username, password)
+        mailer = Mailer(conf.EMAIL_HOST, conf.EMAIL_PORT,
+                        conf.EMAIL_HOST_USER,
+                        conf.EMAIL_HOST_PASSWORD)
         mailer.send(message)
     except Exception:
         if not fail_silently:

+ 0 - 1
contrib/requirements/default.txt

@@ -1,4 +1,3 @@
-mailer
 python-dateutil
 sqlalchemy
 anyjson

+ 0 - 1
setup.cfg

@@ -38,6 +38,5 @@ requires = uuid
            python-dateutil
            anyjson
            carrot >= 0.10.5
-           mailer
            SQLAlchemy
            pyparsing

+ 0 - 1
setup.py

@@ -36,7 +36,6 @@ except ImportError:
 
 install_requires.extend([
     "python-dateutil",
-    "mailer",
     "sqlalchemy",
     "anyjson",
     "carrot>=0.10.5",