Ejemplo n.º 1
0
    def _route_msg(self):
        """
        Sends the msg using the ESMTPSenderFactory.
        """
        log.msg("Connecting to SMTP server %s:%s" % (self._host, self._port))
        msg = self._msg.as_string(False)

        # we construct a defer to pass to the ESMTPSenderFactory
        d = defer.Deferred()
        d.addCallbacks(self.sendSuccess, self.sendError)
        # we don't pass an ssl context factory to the ESMTPSenderFactory
        # because ssl will be handled by reactor.connectSSL() below.
        factory = smtp.ESMTPSenderFactory(
            "",  # username is blank because server does not use auth.
            "",  # password is blank because server does not use auth.
            self._fromAddress.addrstr,
            self._user.dest.addrstr,
            StringIO(msg),
            d,
            heloFallback=True,
            requireAuthentication=False,
            requireTransportSecurity=True)
        factory.domain = __version__
        signal(proto.SMTP_SEND_MESSAGE_START, self._user.dest.addrstr)
        reactor.connectSSL(self._host,
                           self._port,
                           factory,
                           contextFactory=SSLContextFactory(
                               self._cert, self._key))
Ejemplo n.º 2
0
    def sendMessage(self):
        """
        Send the message.

        This method will prepare the message (headers and possibly encrypted
        body) and send it using the ESMTPSenderFactory.

        @return: A deferred with callbacks for error and success of this
            message send.
        @rtype: twisted.internet.defer.Deferred
        """
        msg = self._message.as_string(False)
        d = defer.Deferred()
        factory = smtp.ESMTPSenderFactory(
            self._config[USERNAME_KEY],
            self._config[PASSWORD_KEY],
            self._fromAddress.addrstr,
            self._user.dest.addrstr,
            StringIO(msg),
            d,
            requireAuthentication=False,  # for now do unauth, see issue #2474
        )
        # TODO: Change this to connectSSL when cert auth is in place in the platform
        reactor.connectTCP(
            self._config[HOST_KEY],
            self._config[PORT_KEY],
            factory
        )
        d.addCallback(self.sendSuccess)
        d.addErrback(self.sendError)
        return d
Ejemplo n.º 3
0
    def _route_msg(self, encrypt_and_sign_result):
        """
        Sends the msg using the ESMTPSenderFactory.

        :param encrypt_and_sign_result: A tuple containing the 'maybe'
                                        encrypted message and the recipient
        :type encrypt_and_sign_result: tuple
        """
        message, recipient = encrypt_and_sign_result
        log.msg("Connecting to SMTP server %s:%s" % (self._host, self._port))
        msg = message.as_string(False)

        # we construct a defer to pass to the ESMTPSenderFactory
        d = defer.Deferred()
        d.addCallbacks(self.sendSuccess, self.sendError)
        # we don't pass an ssl context factory to the ESMTPSenderFactory
        # because ssl will be handled by reactor.connectSSL() below.
        factory = smtp.ESMTPSenderFactory(
            "",  # username is blank, no client auth here
            "",  # password is blank, no client auth here
            self._from_address,
            recipient.dest.addrstr,
            StringIO(msg),
            d,
            heloFallback=True,
            requireAuthentication=False,
            requireTransportSecurity=True)
        factory.domain = __version__
        emit(catalog.SMTP_SEND_MESSAGE_START, recipient.dest.addrstr)
        reactor.connectSSL(self._host,
                           self._port,
                           factory,
                           contextFactory=SSLContextFactory(
                               self._cert, self._key))
Ejemplo n.º 4
0
    def send(self, recipient, message):
        self.log.info('Connecting to SMTP server %s:%s' %
                      (self._host, self._port))

        # we construct a defer to pass to the ESMTPSenderFactory
        d = defer.Deferred()
        # we don't pass an ssl context factory to the ESMTPSenderFactory
        # because ssl will be handled by reactor.connectSSL() below.
        factory = smtp.ESMTPSenderFactory(
            "",  # username is blank, no client auth here
            "",  # password is blank, no client auth here
            self._from_address,
            recipient.dest.addrstr,
            StringIO(message),
            d,
            heloFallback=True,
            requireAuthentication=False,
            requireTransportSecurity=True)
        factory.domain = bytes('leap.bitmask.mail-' + __version__)
        reactor.connectSSL(self._host,
                           self._port,
                           factory,
                           contextFactory=SSLContextFactory(
                               self._key, self._key))
        d.addCallback(lambda result: result[1][0][0])
        d.addErrback(self._send_errback)
        return d
Ejemplo n.º 5
0
    def __call__(self, sender, recipients, msg):
        print "Hello!"
        deferred = Deferred()

        msg['From'] = sender
        msg['To'] = ', '.join(recipients)

        factory = smtp.ESMTPSenderFactory(
            self.config.username,
            self.config.password,
            sender,
            recipients,
            cStringIO.StringIO(msg.as_string()),
            deferred,
            contextFactory=self.context,
            requireTransportSecurity=False,
            retries=0,
        )

        deferred.addErrback(self._on_error)

        if self.config.use_ssl:
            reactor.connectSSL(
                self.config.host,
                self.config.port,
                factory,
                self.context
            )
        else:
            reactor.connectTCP(
                self.config.host,
                self.config.port,
                factory
            )
Ejemplo n.º 6
0
    def send(self, sender, recipient, content):
        d = defer.Deferred()

        args = self._authentication + [sender, recipient, content, d]
        factory = smtp.ESMTPSenderFactory(*args, **self._factoryKwargs)

        return self.endpoint.connect(factory).addCallback(lambda _ign: d)
Ejemplo n.º 7
0
 def testESMTPClient(self):
     onDone = defer.Deferred()
     clientFactory = smtp.ESMTPSenderFactory('username',
                                             'password',
                                             'source@address',
                                             'recipient@address',
                                             StringIO("Message body"),
                                             onDone,
                                             retries=0,
                                             timeout=0.5)
     return self._timeoutTest(onDone, clientFactory)
Ejemplo n.º 8
0
 def test_ESMTPClient(self):
     """
     Test timeout for L{smtp.ESMTPSenderFactory}: the response L{Deferred}
     should be errback with a L{smtp.SMTPTimeoutError}.
     """
     onDone = defer.Deferred()
     clientFactory = smtp.ESMTPSenderFactory(
         'username', 'password',
         'source@address', 'recipient@address',
         StringIO("Message body"), onDone,
         retries=0, timeout=0.5)
     return self._timeoutTest(onDone, clientFactory)
Ejemplo n.º 9
0
 def send(host, user=None, pw=None, require_security=False):
     msg = MIMEText(msg_)
     msg['From'] = "\"{}\" <{}>".format(from_name, from_)
     msg['To'] = to
     msg['Subject'] = subject
     msgfile = StringIO(msg.as_string())
     d = Deferred()
     factory = smtp.ESMTPSenderFactory(user, pw, from_, to, msgfile, d,
                                       requireAuthentication=(user is not None),
                                       requireTransportSecurity=require_security)
     reactor.connectTCP(host, self.smtp_port, factory)
     self.wait(d)
     return d
Ejemplo n.º 10
0
    def __sendMail(self, from_addr, to_addrs, messageText, deferred, testing=False):
        if __debug__:
            self.parent.printCurrentView("transport.__sendMail")

        account = self.parent.account

        username         = None
        password         = None
        authRequired     = False
        securityRequired = False
        heloFallback     = True

        if account.useAuth:
            username     = account.username
            password     = account.password
            authRequired = True
            heloFallback = False

        if testing:
            retries = 0
            timeout = constants.TESTING_TIMEOUT
        else:
            retries = account.numRetries
            timeout = account.timeout

        if account.connectionSecurity == 'TLS':
            securityRequired = True

        msg = StringIO.StringIO(messageText)

        # Note that we cheat with the context factory here (value=1),
        # because ssl.connectSSL does it automatically, and in the
        # case of STARTTLS we override esmtpState_starttls above
        # to supply the correct SSL context.
        factory = smtp.ESMTPSenderFactory(username, password, from_addr, 
                                          to_addrs, msg,
                                          deferred, retries, timeout,
                                          1, heloFallback, authRequired, 
                                          securityRequired)

        factory.protocol   = _TwistedESMTPSender
        factory.testing    = testing

        if account.connectionSecurity == 'SSL':
            ssl.connectSSL(account.host, account.port, factory, 
                           self.parent.view)
        else:
            ssl.connectTCP(account.host, account.port, factory, 
                           self.parent.view)
Ejemplo n.º 11
0
        def callback(password):
            # Note that we cheat with the context factory here (value=1),
            # because ssl.connectSSL does it automatically, and in the
            # case of STARTTLS we override esmtpState_starttls above
            # to supply the correct SSL context.
            factory = smtp.ESMTPSenderFactory(username, password, from_addr,
                                              to_addrs, msg, deferred, retries,
                                              timeout, 1, heloFallback,
                                              authRequired, securityRequired)

            factory.protocol = _TwistedESMTPSender
            factory.testing = testing

            # Convert the Unicode hostname to an str
            # before passing to twisted since twisted uses
            # a repr of the hostname in error messages.
            host = self.account.host.encode("utf-8", "ignore")

            if self.account.connectionSecurity == 'SSL':
                ssl.connectSSL(host, self.account.port, factory, self.view)
            else:
                ssl.connectTCP(host, self.account.port, factory, self.view)
Ejemplo n.º 12
0
def _esmtpSendmail(username,
                   password,
                   smtphost,
                   port,
                   from_addr,
                   to_addrs,
                   msg,
                   reactor=None):
    """
    This should be the only function in this module that uses the reactor.
    """
    d = defer.Deferred()
    f = smtp.ESMTPSenderFactory(username,
                                password,
                                from_addr,
                                to_addrs,
                                msg,
                                d,
                                requireTransportSecurity=False)
    if reactor is None:
        from twisted.internet import reactor
    reactor.connectTCP(smtphost, port, f)
    return d