示例#1
0
 def open(self):
     """
     Ensures we have a connection to the email server. Returns whether or
     not a new connection was required (True or False).
     """
     if self.connection:
         # Nothing to do if the connection is already open.
         return False
     try:
         # If local_hostname is not specified, socket.getfqdn() gets used.
         # For performance, we use the cached FQDN for local_hostname.
         if self.timeout is None:
             self.connection = smtplib.SMTP(
                 self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
         else:
             self.connection = smtplib.SMTP(
                 self.host,
                 self.port,
                 local_hostname=DNS_NAME.get_fqdn(),
                 timeout=self.timeout)
         if self.use_tls:
             self.connection.ehlo()
             self.connection.starttls()
             self.connection.ehlo()
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#2
0
文件: smtp.py 项目: MaximusV/django
 def open(self):
     """
     Ensures we have a connection to the email server. Returns whether or
     not a new connection was required (True or False).
     """
     if self.connection:
         # Nothing to do if the connection is already open.
         return False
     try:
         # If local_hostname is not specified, socket.getfqdn() gets used.
         # For performance, we use the cached FQDN for local_hostname.
         if self.use_ssl:
             self.connection = smtplib.SMTP_SSL(
                 self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
         else:
             self.connection = smtplib.SMTP(
                 self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
             # TLS/SSL are mutually exclusive, so only attempt TLS over
             # non-secure connections.
             if self.use_tls:
                 self.connection.ehlo()
                 self.connection.starttls()
                 self.connection.ehlo()
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except smtplib.SMTPException:
         if not self.fail_silently:
             raise
示例#3
0
 def check_spf(self):
     """
     Check if sender is authorised by sender policy framework
     """
     if not ENABLE_SPF:
         return False
     return spf.check(i=socket.gethostbyname(DNS_NAME.get_fqdn()),s=self.email,h=DNS_NAME.get_fqdn())
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            # If local_hostname is not specified, socket.getfqdn() gets used.
            # For performance, we use the cached FQDN for local_hostname.
            if self.use_ssl:
                self.connection = smtplib.SMTP_SSL(self.host, self.port,
                        keyfile=self.keyfile, certfile=self.certfile,
                        local_hostname=DNS_NAME.get_fqdn())
            else:
                self.connection = smtplib.SMTP(self.host, self.port,
                        local_hostname=DNS_NAME.get_fqdn())

            if self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except:
            if not self.fail_silently:
                raise
示例#5
0
 def check_spf(self):
     """
     Check if sender is authorised by sender policy framework
     """
     if not ENABLE_SPF:
         return False
     return spf.check(i=socket.gethostbyname(DNS_NAME.get_fqdn()),
                      s=self.email,
                      h=DNS_NAME.get_fqdn())
示例#6
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            assert self.port == 465, \
                'Sorry we only support smtps connections right now'

            if '.onion' in self.host:
                self.connection = SMTP_SSL_TOR(
                    self.host, self.port, local_hostname='127.0.0.1')
            else:
                # If local_hostname is not specified, socket.getfqdn() gets
                # used.
                # For performance, we use the cached FQDN for local_hostname.
                self.connection = smtplib.SMTP_SSL(
                    self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
            self.connection.set_debuglevel(1)
            if self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except:
            if not self.fail_silently:
                raise
示例#7
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        connection_class = smtplib.SMTP
        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        try:
            self.connection = connection_class(self.host, self.port, **connection_params)
            self.connection.set_debuglevel(settings.DEBUG)

            self.connection.ehlo()
            self.connection.starttls()
            self.connection.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(self.authinfo["auth_string"]))
            
            self.connection.ehlo()
            return True
        except smtplib.SMTPException:
            if not self.fail_silently:
                raise
示例#8
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        connection_class = smtplib.SMTP_SSL if self.use_ssl else smtplib.SMTP
        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        try:
            self.connection = connection_class(self.host, self.port, **connection_params)

            # TLS/SSL are mutually exclusive, so only attempt TLS over
            # non-secure connections.
            if not self.use_ssl and self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()
            if self.username and self.password:
                self.connection.login(self.username, self.password)
        except smtplib.SMTPException:
            if not self.fail_silently:
                raise
示例#9
0
    def open(self) -> bool:
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        try:
            self.connection = smtplib.SMTP(self.host, self.port,
                                           **connection_params)

            # TLS
            context = ssl.SSLContext(self._protocol())
            if self.ssl_certfile:
                context.load_cert_chain(certfile=self.ssl_certfile,
                                        keyfile=self.ssl_keyfile)
            self.connection.ehlo()
            self.connection.starttls(context=context)
            self.connection.ehlo()
            if self.username and self.password:
                self.connection.login(self.username, self.password)
                log.debug("Successful SMTP connection/login")
            else:
                log.debug("Successful SMTP connection (without login)")
            return True
        except smtplib.SMTPException:
            log.debug("SMTP connection and/or login failed")
            if not self.fail_silently:
                raise
示例#10
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            # If local_hostname is not specified, socket.getfqdn() gets used.
            # For performance, we use the cached FQDN for local_hostname.
            self.connection = smtplib.SMTP(self.host,
                                           self.port,
                                           local_hostname=DNS_NAME.get_fqdn())
            if self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()
            if self.username and self.password:

                # force plain authen
                # from http://code.djangoproject.com/ticket/9488
                if not (200 <= self.connection.ehlo()[0] <= 299):
                    (code, resp) = self.connection.helo()
                    if not (200 <= code <= 299):
                        raise SMTPHeloError(code, resp)
                self.connection.esmtp_features["auth"] = "LOGIN PLAIN"
                self.connection.login(self.username, self.password)

                #self.connection.login(self.username, self.password)
            return True
        except:
            if not self.fail_silently:
                raise
示例#11
0
    def open(self):
        """
        Ensure an open connection to the email server. Return whether or not a
        new connection was required (True or False) or None if an exception
        passed silently.
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        if self.use_ssl:
            connection_params.update({
                'keyfile': self.ssl_keyfile,
                'certfile': self.ssl_certfile,
            })
        try:
            self.connection = self.connection_class(self.host, self.port, **connection_params)

            # TLS/SSL are mutually exclusive, so only attempt TLS over
            # non-secure connections.
            if not self.use_ssl and self.use_tls:
                self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except (smtplib.SMTPException, socket.error):
            if not self.fail_silently:
                raise
示例#12
0
def connect(mail_account=False, timeout=False, fail_silently=True, ):
    if not mail_account:
        mail_account = get_mail_account()

    connection_class = SMTP_SSL if mail_account.server.use_ssl_smtp and \
                                   not mail_account.server.use_tls_smtp else SMTP
    connection_params = {'local_hostname': DNS_NAME.get_fqdn()}

    if timeout:
        connection_params['timeout'] = timeout
    try:
        connection = connection_class(host=mail_account.server.server_smtp,
                                      port=mail_account.server.port_smtp,
                                      **connection_params)
        if not mail_account.server.use_ssl_smtp and mail_account.server.use_tls_smtp:
            connection.ehlo()
            connection.starttls()
            connection.ehlo()
        if mail_account.username and mail_account.password:
            connection.login(mail_account.username, mail_account.password, )
        return connection
    except (SMTPException, SMTPServerDisconnected):
        if not fail_silently:
            raise
        else:
            return False
示例#13
0
def connect(
    mail_account=False,
    timeout=False,
    fail_silently=True,
):
    if not mail_account:
        mail_account = get_mail_account()

    connection_class = SMTP_SSL if mail_account.server.use_ssl_smtp and \
                                   not mail_account.server.use_tls_smtp else SMTP
    connection_params = {'local_hostname': DNS_NAME.get_fqdn()}

    if timeout:
        connection_params['timeout'] = timeout
    try:
        connection = connection_class(host=mail_account.server.server_smtp,
                                      port=mail_account.server.port_smtp,
                                      **connection_params)
        if not mail_account.server.use_ssl_smtp and mail_account.server.use_tls_smtp:
            connection.ehlo()
            connection.starttls()
            connection.ehlo()
        if mail_account.username and mail_account.password:
            connection.login(
                mail_account.username,
                mail_account.password,
            )
        return connection
    except (SMTPException, SMTPServerDisconnected):
        if not fail_silently:
            raise
        else:
            return False
示例#14
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        connection_class = smtplib.SMTP_SSL if self.use_ssl else smtplib.SMTP
        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        if self.use_ssl:
            connection_params.update({
                'keyfile': self.ssl_keyfile,
                'certfile': self.ssl_certfile,
            })
        try:
            self.connection = connection_class(self.host, self.port,
                                               **connection_params)

            # TLS/SSL are mutually exclusive, so only attempt TLS over
            # non-secure connections.
            self.connection.starttls()
            self.connection.ehlo()
            self.connection.esmtp_features['auth'] = 'LOGIN PLAIN'
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except smtplib.SMTPException:
            if not self.fail_silently:
                raise
示例#15
0
    def open(self):
        """
        Ensure an open connection to the email server. Return whether or not a
        new connection was required (True or False) or None if an exception
        passed silently.
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        if self.use_ssl:
            connection_params.update({
                'keyfile': self.ssl_keyfile,
                'certfile': self.ssl_certfile,
            })
        try:
            self.connection = self.connection_class(self.host, self.port, **connection_params)

            # TLS/SSL are mutually exclusive, so only attempt TLS over
            # non-secure connections.
            if not self.use_ssl and self.use_tls:
                self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
            if self.username and self.password:
                self.connection.login(self.username, self.password)
            return True
        except OSError:
            if not self.fail_silently:
                raise
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            # If local_hostname is not specified, socket.getfqdn() gets used.
            # For performance, we use the cached FQDN for local_hostname.
            self.connection = smtplib.SMTP(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
            if self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()
            if self.username and self.password:

                # force plain authen
                # from http://code.djangoproject.com/ticket/9488
                if not (200 <= self.connection.ehlo()[0] <= 299):
                    (code, resp) = self.connection.helo()
                    if not (200 <= code <= 299):
                        raise SMTPHeloError(code, resp)
                self.connection.esmtp_features["auth"] = "LOGIN PLAIN"
                self.connection.login(self.username, self.password)

                # self.connection.login(self.username, self.password)
            return True
        except:
            if not self.fail_silently:
                raise
示例#17
0
 def open(self):
     if self.connection:
         return False
     try:
         self.connection = smtplib.SMTP_SSL(self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#18
0
    def open(self):
        if self.connection:
            return False
        try:
            logger.info("sending email via %s" % self.host)
            self.connection = smtplib.SMTP_SSL(
                self.host, self.port, local_hostname=DNS_NAME.get_fqdn())

            return True
        except:
            if not self.fail_silently:
                raise
示例#19
0
文件: smtp.py 项目: vaad2/vest
 def open(self):
     if self.connection:
         return False
     try:
         self.connection = smtplib.SMTP_SSL(
             self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#20
0
 def open (self):
     """
     We will use the same lib as standart django backend
     but will choose SMTP_SSL instead of just SMTP class
     """
     if self.connection:
         return False  # connection already made
     try:
         self.connection = smtplib.SMTP_SSL(self.host, self.port,
                 local_hostname=DNS_NAME.get_fqdn())
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#21
0
 def open(self):
     """
     We will use the same lib as standart django backend
     but will choose SMTP_SSL instead of just SMTP class
     """
     if self.connection:
         return False  # connection already made
     try:
         self.connection = smtplib.SMTP_SSL(
             self.host, self.port, local_hostname=DNS_NAME.get_fqdn())
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#22
0
    def open(self):
        if self.connection:
            return False
        try:
            self.connection = smtplib.SMTP_SSL(
                self.host, self.port, local_hostname=DNS_NAME.get_fqdn())

            if self.username and self.password:
                self.connection.ehlo()
                # Remove CRAM-MD5 authentication method
                self.connection.esmtp_features['auth'] = 'PLAIN LOGIN'
                self.connection.login(self.username, self.password)
            return True
        except smtplib.SMTPException:
            if not self.fail_silently:
                raise
 def open(self):
     if self.connection:
         # Nothing to do if the connection is already open.
         return False
     try:
         # If local_hostname is not specified, socket.getfqdn() gets used.
         # For performance, we use the cached FQDN for local_hostname.
         self.connection = SMTPExt(self.host, self.port,
                                        local_hostname=DNS_NAME.get_fqdn())
         if self.use_tls:
             self.connection.ehlo()
             self.connection.starttls(**self.WRAP_SOCKET_PARAMS)
             self.connection.ehlo()
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#24
0
文件: backends.py 项目: perplext/amon
    def open(self):
        if self.connection:
            return False
        try:

            self.connection = smtplib.SMTP(host=self.host, port=self.port,
                     local_hostname=DNS_NAME.get_fqdn(), timeout=self.timeout)

            if self.use_tls:
                self.connection.ehlo()
                self.connection.starttls()
                self.connection.ehlo()

            if self.username and self.password:
                self.connection.login(self.username, self.password)

            return True
        except:
            if not self.fail_silently:
                raise
 def open(self):
     if self.connection:
         # Nothing to do if the connection is already open.
         return False
     try:
         # If local_hostname is not specified, socket.getfqdn() gets used.
         # For performance, we use the cached FQDN for local_hostname.
         self.connection = SMTPExt(self.host,
                                   self.port,
                                   local_hostname=DNS_NAME.get_fqdn())
         if self.use_tls:
             self.connection.ehlo()
             self.connection.starttls(**self.WRAP_SOCKET_PARAMS)
             self.connection.ehlo()
         if self.username and self.password:
             self.connection.login(self.username, self.password)
         return True
     except:
         if not self.fail_silently:
             raise
示例#26
0
 def connect(self):
     kwargs = {
         'local_hostname': DNS_NAME.get_fqdn()
     }
     if self.use_ssl:
         keyfile = getattr(settings, 'EMAIL_SSL_KEYFILE', None)
         certfile = getattr(settings, 'EMAIL_SSL_CERTFILE', None)
         if keyfile:
             kwargs['keyfile'] = keyfile
         if certfile:
             kwargs['certfile'] = certfile
         self.connection = SMTP_SSL(self.host, self.port, **kwargs)
     else:
         self.connection = SMTP(self.host, self.port, **kwargs)
     if self.use_tls:
         self.connection.ehlo()
         self.connection.starttls()
         self.connection.ehlo()
     if self.username and self.password:
         self.connection.login(self.username, self.password)
     return True
示例#27
0
def forward(msg, recips):
	connection = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT, local_hostname=DNS_NAME.get_fqdn())
	
	text = msg.as_string()
	
	# Really, this should be sent separately to each list so that the bounce is
	# correct, but for now, just pick a random list.
	env_sender_list = msg.mailing_lists.pop()
	env_sender = env_sender_list.address + '-bounce@' + env_sender_list.site.domain

	while len(recips) > MAX_SMTP_RECIPS:
		chunk = set(list(recips)[0:MAX_SMTP_RECIPS])
		connection.sendmail(env_sender, chunk, text)
		recips -= chunk

	refused = connection.sendmail(env_sender, recips, text)
	connection.quit()
	if refused:
		# should I send this back to the sender, as well?
		msg.log.error("Message delivery failed at %s" % ', '.join(refused.keys()))
	msg.log.info("Forwarded message")
示例#28
0
    def connect(self, ):
        connection_class = SMTP_SSL
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}

        try:
            connection = connection_class(
                host=self.sender.server.server_smtp,
                port=self.sender.server.port_smtp,
                **connection_params)

            if self.sender.username and self.sender.password:
                connection.login(self.sender.username, self.sender.password, )
                connection.ehlo()

            return connection

        except (SMTPException, SMTPServerDisconnected) as e:
            print('Exception(SMTPException, SMTPServerDisconnected): ', e)
            return False

        except socket.error as e:
            print('Exception(socket.error): ', e)
            return False
示例#29
0
    def connect(self, ):
        connection_class = SMTP_SSL
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}

        try:
            connection = connection_class(
                host=self.sender.server.server_smtp,
                port=self.sender.server.port_smtp,
                **connection_params)

            if self.sender.username and self.sender.password:
                connection.login(self.sender.username, self.sender.password, )
                connection.ehlo()

            return connection

        except (SMTPException, SMTPServerDisconnected) as e:
            print('Exception(SMTPException, SMTPServerDisconnected): ', e)
            return False

        except socket.error as e:
            print('Exception(socket.error): ', e)
            return False
示例#30
0
    def open(self):
        """
        Ensure an open connection to the email server. Return whether or not a
        new connection was required (True or False) or None if an exception
        passed silently.
        """
        if self.connection:
            # Nothing to do if the connection is already open.
            return False

        # If local_hostname is not specified, socket.getfqdn() gets used.
        # For performance, we use the cached FQDN for local_hostname.
        connection_params = {'local_hostname': DNS_NAME.get_fqdn()}
        if self.timeout is not None:
            connection_params['timeout'] = self.timeout
        if self.use_ssl:
            connection_params.update({
                'keyfile': self.ssl_keyfile,
                'certfile': self.ssl_certfile,
            })
        try:
            self.connection = self.connection_class(self.host, self.port, **connection_params)

            # TLS/SSL are mutually exclusive, so only attempt TLS over
            # non-secure connections.
            if not self.use_ssl and self.use_tls:
                self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
            if self.username and self.oauth_token_refresh:
                self.connection.ehlo_or_helo_if_needed()
                oauth_auth = "user={username}\1auth=Bearer {access_token}\1\1".format(
                        username=self.username,
                        access_token=self.access_token)
                self.connection.auth('XOAUTH2', lambda: oauth_auth, initial_response_ok=True)
            return True
        except OSError:
            if not self.fail_silently:
                raise
示例#31
0
文件: smtp.py 项目: vaad2/vest2
    def open(self):
        if self.connection:
            return False
        try:
            try:
                username = SiteSettings.objects.get(name='EMAIL_HOST_USER').value
                password = SiteSettings.objects.get(name='EMAIL_HOST_PASSWORD').value
                host = SiteSettings.objects.get(name='EMAIL_HOST').value
                port = SiteSettings.objects.get(name='EMAIL_PORT').value


            except Exception, e:
                username = self.username
                password = self.password
                host = self.host
                port = self.port


            self.connection = smtplib.SMTP_SSL(host, port,
                                           local_hostname=DNS_NAME.get_fqdn())

            self.connection.login(username, password)

            return True
示例#32
0
"""SMTP email backend class."""
示例#33
0
    def open(self):
        '''
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        '''
        if self.__connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            # Refresh access token by the refresh token
            gauth = urllib.request.urlopen(self.__request_url, urllib.parse.urlencode(self.__param))
            response = gauth.read()
            gauth.close()
            response = json.loads(response)
            access_token = response['access_token']
            access_token = 'user={0}\1auth=Bearer {1}\1\1'.format(self.__usrname, access_token.encode('utf-8'))

            # If local_hostname is not specified, socket.getfqdn() gets used.
            # For performance, we use the cached FQDN for local_hostname.
            self.__connection = smtplib.SMTP(self.__host, self.__port, local_hostname=DNS_NAME.get_fqdn())
            if self.__use_tls:
                self.__connection.ehlo()
                self.__connection.starttls()
                self.__connection.ehlo()
            self.__connection.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(access_token))
            return True
        except:
            if not self.fail_silently:
                raise
            return False
示例#34
0
    def open(self):
        """
        Ensures we have a connection to the email server. Returns whether or
        not a new connection was required (True or False).
        """
        if self.__connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            # Refresh access token by the refresh token
            gauth = urllib.request.urlopen(self.__request_url, urllib.parse.urlencode(self.__param))
            response = gauth.read()
            gauth.close()
            response = json.loads(response)
            access_token = response["access_token"]
            access_token = "user={0}\1auth=Bearer {1}\1\1".format(self.__usrname, access_token.encode("utf-8"))

            # If local_hostname is not specified, socket.getfqdn() gets used.
            # For performance, we use the cached FQDN for local_hostname.
            self.__connection = smtplib.SMTP(self.__host, self.__port, local_hostname=DNS_NAME.get_fqdn())
            if self.__use_tls:
                self.__connection.ehlo()
                self.__connection.starttls()
                self.__connection.ehlo()
            self.__connection.docmd("AUTH", "XOAUTH2 " + base64.b64encode(access_token))
            return True
        except:
            if not self.fail_silently:
                raise
            return False