コード例 #1
0
 def test(self, from_addr):
     try:
         self.open()
         self.connection.ehlo_or_helo_if_needed()
         (code, resp) = self.connection.mail(from_addr, [])
         if code != 250:
             logger.warning(
                 f'Error testing mail settings, code {code}, resp: {resp}')
             raise SMTPSenderRefused(code, resp)
         (code, resp) = self.connection.rcpt('*****@*****.**')
         if code not in (250, 251):
             logger.warning(
                 f'Error testing mail settings, code {code}, resp: {resp}')
             raise SMTPSenderRefused(code, resp)
     finally:
         self.close()
コード例 #2
0
ファイル: mail.py プロジェクト: AthinaDavari/pretalx
 def test(self, from_addr):
     try:  # pragma: no cover
         self.open()
         self.connection.ehlo_or_helo_if_needed()
         (code, resp) = self.connection.mail(from_addr, [])
         if code != 250:
             logger.warning(
                 f"Error testing mail settings, code {code}, resp: {resp}")
             raise SMTPSenderRefused(code, resp, sender=from_addr)
         (code, resp) = self.connection.rcpt("*****@*****.**")
         if code not in (250, 251):
             logger.warning(
                 f"Error testing mail settings, code {code}, resp: {resp}")
             raise SMTPSenderRefused(code, resp, sender=from_addr)
     finally:
         self.close()
コード例 #3
0
    def test__email_sender__EmailSender__send_message__RaisesSMTPSenderRefused__WhenTLSConnectionNotEstablished(
            self, mock_send_mail):

        exception = SMTPSenderRefused(1, 'some_message', 'some_sender')
        mock_send_mail.side_effect = exception

        sender = NHSMailEmailSender()

        self.assertRaises(SMTPSenderRefused, sender.send_message,
                          self.test_message)
コード例 #4
0
    def test__message_handler__MessageHandler__send__logsError__whenSMTPSenderRefusedIsRaised(
            self, mock_logger):

        mock_sender = MagicMock(spec=NHSMailEmailSender).return_value
        handler = MessageHandler(mock_sender)
        dummy_message = {}
        handler.sender.send_message.side_effect = SMTPSenderRefused(
            1, 'some_message', 'some_sender')

        handler.send(dummy_message)

        assert mock_logger.called
コード例 #5
0
 def test(self, from_addr):
     try:
         self.open()
         self.connection.ehlo_or_helo_if_needed()
         self.connection.rcpt("*****@*****.**")
         (code, resp) = self.connection.mail(from_addr, [])
         if code != 250:
             raise SMTPSenderRefused(code, resp, from_addr)
         senderrs = {}
         (code, resp) = self.connection.rcpt('')
         if (code != 250) and (code != 251):
             raise SMTPRecipientsRefused(senderrs)
     finally:
         self.close()
コード例 #6
0
    def test_emails_that_are_too_large_stop_after_first_failure(
            self, mock_export_report):
        self.mock_send_email.side_effect = SMTPSenderRefused(
            code=LARGE_FILE_SIZE_ERROR_CODE,
            msg='test error',
            sender='*****@*****.**')
        report = ReportNotification(_id='5', domain='test-domain', uuid='uuid')

        report._send_emails('Test Report',
                            'Report Text',
                            ['*****@*****.**', '*****@*****.**'],
                            excel_files=[])

        self.mock_send_email.assert_called_once()
コード例 #7
0
 def test(self, from_addr):
     try:
         self.open()
         self.connection.ehlo_or_helo_if_needed()
         self.connection.rcpt("*****@*****.**")
         (code, resp) = self.connection.mail(from_addr, [])
         if code != 250:
             logger.warn('Error testing mail settings, code %d, resp: %s' %
                         (code, resp))
             raise SMTPSenderRefused(code, resp, from_addr)
         senderrs = {}
         (code, resp) = self.connection.rcpt('*****@*****.**')
         if (code != 250) and (code != 251):
             logger.warn('Error testing mail settings, code %d, resp: %s' %
                         (code, resp))
             raise SMTPRecipientsRefused(senderrs)
     finally:
         self.close()
コード例 #8
0
    def test_huge_reports_with_attachments_resend_only_attachments(self):
        # trigger the exception on the first call, success on the second
        # (a failure needs to occur to trigger the re-send)
        self.mock_send_email.side_effect = [
            SMTPSenderRefused(code=LARGE_FILE_SIZE_ERROR_CODE,
                              msg='test error',
                              sender='*****@*****.**'), None
        ]
        report = ReportNotification(_id='5', domain='test-domain', uuid='uuid')
        report._send_emails('Test Report',
                            'Report Text',
                            ['*****@*****.**', '*****@*****.**'],
                            excel_files=['abba'])

        self.mock_send_email.assert_called_with(
            'Test Report', ['*****@*****.**', '*****@*****.**'],
            'Unable to generate email report. Excel files are attached.',
            email_from=settings.DEFAULT_FROM_EMAIL,
            file_attachments=['abba'])
コード例 #9
0
    def sendmail(self,
                 from_addr,
                 to_addrs,
                 msg,
                 mail_options=[],
                 rcpt_options=[]):
        """This command performs an entire mail transaction.

        The arguments are:
            - from_addr    : The address sending this mail.
            - to_addrs     : A list of addresses to send this mail to.  A bare
                             string will be treated as a list with 1 address.
            - msg          : The message to send.
            - mail_options : List of ESMTP options (such as 8bitmime) for the
                             mail command.
            - rcpt_options : List of ESMTP options (such as DSN commands) for
                             all the rcpt commands.

        msg may be a string containing characters in the ASCII range, or a byte
        string.  A string is encoded to bytes using the ascii codec, and lone
        \\r and \\n characters are converted to \\r\\n characters.

        If there has been no previous EHLO or HELO command this session, this
        method tries ESMTP EHLO first.  If the server does ESMTP, message size
        and each of the specified options will be passed to it.  If EHLO
        fails, HELO will be tried and ESMTP options suppressed.

        This method will return normally if the mail is accepted for at least
        one recipient.  It returns a dictionary, with one entry for each
        recipient that was refused.  Each entry contains a tuple of the SMTP
        error code and the accompanying error message sent by the server.

        This method may raise the following exceptions:

         SMTPHeloError          The server didn't reply properly to
                                the helo greeting.
         SMTPRecipientsRefused  The server rejected ALL recipients
                                (no mail was sent).
         SMTPSenderRefused      The server didn't accept the from_addr.
         SMTPDataError          The server replied with an unexpected
                                error code (other than a refusal of
                                a recipient).
         SMTPNotSupportedError  The mail_options parameter includes 'SMTPUTF8'
                                but the SMTPUTF8 extension is not supported by
                                the server.

        Note: the connection will be open even after an exception is raised.

        Example:

         >>> import smtplib
         >>> s=smtplib.SMTP("localhost")
         >>> tolist=["*****@*****.**","*****@*****.**","*****@*****.**","*****@*****.**"]
         >>> msg = '''\\
         ... From: [email protected]
         ... Subject: testin'...
         ...
         ... This is a test '''
         >>> s.sendmail("*****@*****.**",tolist,msg)
         { "*****@*****.**" : ( 550 ,"User unknown" ) }
         >>> s.quit()

        In the above example, the message was accepted for delivery to three
        of the four addresses, and one was rejected, with the error code
        550.  If all addresses are accepted, then the method will return an
        empty dictionary.

        """
        self.ehlo_or_helo_if_needed()
        esmtp_opts = []
        if isinstance(msg, str):
            msg = _fix_eols(msg).encode('ascii')
        if self.does_esmtp:
            if self.has_extn('size'):
                esmtp_opts.append("size=%d" % len(msg))
            for option in mail_options:
                esmtp_opts.append(option)
        (code, resp) = self.mail(from_addr, esmtp_opts)
        if code != 250:
            if code == 421:
                self.close()
            else:
                self._rset()
            raise SMTPSenderRefused(code, resp, from_addr)
        senderrs = {}
        if isinstance(to_addrs, str):
            to_addrs = [to_addrs]
        for each in to_addrs:
            (code, resp) = self.rcpt(each, rcpt_options)
            if (code != 250) and (code != 251):
                senderrs[each] = (code, resp)
            if code == 421:
                self.close()
                raise SMTPRecipientsRefused(senderrs)
        if len(senderrs) == len(to_addrs):
            # the server refused all our recipients
            self._rset()
            raise SMTPRecipientsRefused(senderrs)
        (code, resp) = self.data(msg)
        if code != 250:
            if code == 421:
                self.close()
            else:
                self._rset()
            raise SMTPDataError(code, resp)
        #if we got here then somebody got our mail
        return senderrs
コード例 #10
0
ファイル: test_tasks.py プロジェクト: uetuluk/edx-platform
 def test_retry_after_smtp_sender_refused_error(self):
     self._test_retry_after_unlimited_retry_error(
         SMTPSenderRefused(421, "Throttling: Sending rate exceeded", self.instructor.email)
     )