import json
import socket
import pwd
+import pipes
+
+from subprocess import Popen, PIPE
from email import encoders
from email.mime.text import MIMEText
from .app import PpApplication
-__version__ = '0.5.3'
+__version__ = '0.5.4'
LOG = logging.getLogger(__name__)
VALID_MAIL_METHODS = ('smtp', 'sendmail')
if self.verbose > 1:
LOG.debug("Mail to send:\n{}".format(mail.as_string(unixfrom=True)))
+ if self.mail_method == 'smtp':
+ self._send_mail_smtp(mail)
+ else:
+ self._send_mail_sendmail(mail)
+
+ # -------------------------------------------------------------------------
+ def _send_mail_smtp(self, mail):
+
+ with smtplib.SMTP(self.mail_server, self.smtp_port) as smtp:
+ if self.verbose > 2:
+ smtp.set_debuglevel(2)
+ elif self.verbose > 1:
+ smtp.set_debuglevel(1)
+
+ smtp.send_message(mail)
+
+ # -------------------------------------------------------------------------
+ def _send_mail_sendmail(self, mail):
+
+ # Searching for the location of sendmail ...
+ paths = (
+ '/usr/sbin/sendmail',
+ '/usr/lib/sendmail',
+ )
+ sendmail = None
+ for path in paths:
+ if os.path.isfile(path) and os.access(path, os.X_OK):
+ sendmail = path
+ break
+
+ if not sendmail:
+ msg = "Did not found sendmail executable."
+ LOG.error(msg)
+ return
+
+ cmd = [sendmail, "-t", "-oi"]
+ cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+ LOG.debug("Executing: {}".format(cmd_str))
+
+ p = Popen(cmd, stdin=PIPE, universal_newlines=True)
+ p.communicate(mail.as_string())
+
# -------------------------------------------------------------------------
def post_init(self):
"""