# Standard modules
import logging
import copy
+import pipes
from email.mime.text import MIMEText
from email import charset
+from subprocess import Popen, PIPE
+
import smtplib
# Own modules
from .mail_config import MailConfigError, MailConfiguration
from .mail_config import VALID_MAIL_METHODS, MAX_PORT_NUMBER
-__version__ = '0.1.1'
+__version__ = '0.2.1'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
if self.verbose > 2:
LOG.debug(_("Got command line arguments:") + '\n' + pp(self.args))
+ # -------------------------------------------------------------------------
+ def send_mail(self, subject, body):
+
+ mail = MIMEText(body, 'plain', 'utf-8')
+ mail['Subject'] = subject
+ mail['From'] = self.cfg.mail_from
+ mail['To'] = ', '.join(self.cfg.mail_recipients)
+ mail['Reply-To'] = self.cfg.reply_to
+ mail['X-Mailer'] = self.cfg.xmailer
+ if self.mail_cc:
+ mail['Cc'] = ', '.join(self.mail_cc)
+
+ if self.verbose > 1:
+ LOG.debug(_("Mail to send:") + '\n' + 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.cfg.mail_server, self.cfg.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())
+
# =============================================================================
if __name__ == "__main__":
from fb_tools.multi_config import MultiConfigError, BaseMultiConfig
from fb_tools.multi_config import DEFAULT_ENCODING
+from . import __version__ as GLOBAL_VERSION
+
from .mailaddress import MailAddress
from .xlate import XLATOR
-__version__ = '0.1.2'
+__version__ = '0.1.3'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
default_reply_to = 'solution@pixelpark.com'
- default_mail_server = 'prd-mail.pixelpark.com'
+ default_mail_server = 'localhost'
current_user_name = pwd.getpwuid(os.getuid()).pw_name
current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos
ensure_privacy=ensure_privacy, initialized=False,
)
+ self.xmailer = "{a} (Admin Tools version {v})".format(
+ a=self.appname, v=GLOBAL_VERSION)
+
if initialized:
self.initialized = True