Changeset a7efe4a in rtems-tools for rtemstoolkit/mailer.py


Ignore:
Timestamp:
04/12/21 18:57:00 (2 years ago)
Author:
Alex White <alex.white@…>
Branches:
5
Children:
f7f1a3e
Parents:
6759c3c
git-author:
Alex White <alex.white@…> (04/12/21 18:57:00)
git-committer:
Joel Sherrill <joel@…> (12/16/21 21:26:39)
Message:

rtemstoolkit/mailer.py: Add SMTP login options

This adds more options so that the user can authenticate with the SMTP
server.

Updates #4553

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rtemstoolkit/mailer.py

    r6759c3c ra7efe4a  
    4444
    4545_options = {
    46     '--mail'     : 'Send email report or results.',
    47     '--smtp-host': 'SMTP host to send via.',
    48     '--mail-to'  : 'Email address to send the email too.',
    49     '--mail-from': 'Email address the report is from.'
     46    '--mail'         : 'Send email report or results.',
     47    '--mail-to'      : 'Email address to send the email to.',
     48    '--mail-from'    : 'Email address the report is from.',
     49    '--smtp-host'    : 'SMTP host to send via.',
     50    '--smtp-port'    : 'SMTP port to send via.',
     51    '--smtp-user'    : 'User for SMTP authentication.',
     52    '--smtp-password': 'Password for SMTP authentication.'
    5053}
    5154
     
    5659def add_arguments(argsp):
    5760    argsp.add_argument('--mail', help = _options['--mail'], action = 'store_true')
    58     for o in ['--smtp-host', '--mail-to', '--mail-from']:
     61    for o in list(_options)[1:]:
    5962        argsp.add_argument(o, help = _options[o], type = str)
    6063
     
    131134        return 'localhost'
    132135
     136    def smtp_port(self):
     137        port = self._get_arg('--smtp-port')
     138        if port is not None:
     139            return port
     140        if self._args_are_macros():
     141            port = self.opts.defaults.get_value('%{_mail_smtp_port}')
     142        return port
     143
     144    def smtp_user(self):
     145        return self._get_arg('--smtp-user')
     146
     147    def smtp_password(self):
     148        return self._get_arg('--smtp-password')
     149
    133150    def send(self, to_addr, subject, body):
    134151        from_addr = self.from_address()
    135152        msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
    136153            (from_addr, to_addr, subject) + body
     154        port = self.smtp_port()
     155
    137156        try:
    138             s = smtplib.SMTP(self.smtp_host())
     157            s = smtplib.SMTP(self.smtp_host(), port, timeout=10)
     158
     159            password = self.smtp_password()
     160            # If a password is provided, assume that authentication is required.
     161            if password is not None:
     162                user = self.smtp_user()
     163                if user is None:
     164                    user = from_addr
     165                s.starttls()
     166                s.login(user, password)
     167
    139168            s.sendmail(from_addr, [to_addr], msg)
    140169        except smtplib.SMTPException as se:
Note: See TracChangeset for help on using the changeset viewer.