Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an heartbeat functionnality #91

Open
Natim opened this issue Jan 29, 2019 · 1 comment
Open

Add an heartbeat functionnality #91

Natim opened this issue Jan 29, 2019 · 1 comment

Comments

@Natim
Copy link

Natim commented Jan 29, 2019

Is there a way to get if the pyramid_mailer is correctly configured (connection with the SMTP server is possible with given credentials).

I know I could do that by sending an email to a wrong mailbox, but I would like to avoid doing that if possible.
Is it possible to add a ping() method to the mailer that would answer true or false depending of is the connection was successful or not?

@Natim
Copy link
Author

Natim commented Jan 29, 2019

Something like that could do it:

    def ping(self):
        connection = self.smtp_factory()

        # send EHLO
        code, response = connection.ehlo()
        if code < 200 or code >= 300:
            code, response = connection.helo()
            if code < 200 or code >= 300:
                raise RuntimeError(
                        'Error sending HELO to the SMTP server '
                        '(code=%s, response=%s)' % (code, response))

        # encryption support
        have_tls = connection.has_extn('starttls')
        if not have_tls and self.force_tls:
            raise RuntimeError('TLS is not available but TLS is required')

        if have_tls and HAVE_SSL and not self.no_tls:
            connection.starttls()
            connection.ehlo()

        if connection.does_esmtp:
            if self.username is not None and self.password is not None:
                connection.login(self.username, self.password)
        elif self.username:
            raise RuntimeError(
                    'Mailhost does not support ESMTP but a username '
                    'is configured')

        # Do not send a message
        try:
            connection.quit()
        except SSLError:
            # something weird happened while quiting
            connection.close()

Every other mailer (than SMTP related ones) should return True.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant