예제 #1
0
def send_email(email, template, kwargs):
    """Send an email via the mailgun service."""
    maildir = os.path.join(topdir, 'emails')
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(maildir))
    template = env.get_template(template)
    rendered = template.render(**kwargs)
    headers, message = parse_email(rendered)
    mailargs = {'to': email,
                'from': app.config['MAIL_FROM'],
                'bcc': app.config.get('MAIL_BCC'),
                'text': message}
    mailargs.update(headers)
    conn = HTTPSConnection('api.mailgun.net', 443)
    conn.connect()
    auth = b64enc('api:{0[MAILGUN_KEY]}'.format(app.config))
    headers = {'Authorization': 'Basic {0}'.format(auth),
               'Accept': 'application/json',
               'Content-type': 'application/x-www-form-urlencoded'}
    url = '/v2/{0[MAILGUN_DOMAIN]}/messages'.format(app.config)
    body = urlencode(mailargs)
    conn.request('POST', url, body, headers)
    resp = conn.getresponse()
    if resp.status != 200:
        raise RuntimeError('could not send email')
    conn.close()
예제 #2
0
    def connect(self):
        """
        Override the connect() function to intercept calls to certain
        host/ports.

        If no app at host/port has been registered for interception then
        a normal HTTPSConnection is made.
        """
        if debuglevel:
            sys.stderr.write('connect: %s, %s\n' % (
                self.host,
                self.port,
            ))

        try:
            (app, script_name) = self.get_app(self.host, self.port)
            if app:
                if debuglevel:
                    sys.stderr.write('INTERCEPTING call to %s:%s\n' % (
                        self.host,
                        self.port,
                    ))
                self.sock = wsgi_fake_socket(app,
                                             self.host,
                                             self.port,
                                             script_name,
                                             https=True)
            else:
                try:
                    import ssl
                    if not hasattr(self, 'key_file'):
                        self.key_file = None
                    if not hasattr(self, 'cert_file'):
                        self.cert_file = None
                    if not hasattr(self, '_context'):
                        try:
                            self._context = ssl.create_default_context()
                        except AttributeError:
                            self._context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                            self._context.options |= ssl.OP_NO_SSLv2
                            if not hasattr(self, 'check_hostname'):
                                self._check_hostname = (
                                    self._context.verify_mode != ssl.CERT_NONE)
                            else:
                                self._check_hostname = self.check_hostname
                except (ImportError, AttributeError):
                    pass
                HTTPSConnection.connect(self)

        except Exception:
            if debuglevel:  # intercept & print out tracebacks
                traceback.print_exc()
            raise
예제 #3
0
    def connect(self):
        """
        Override the connect() function to intercept calls to certain
        host/ports.

        If no app at host/port has been registered for interception then
        a normal HTTPSConnection is made.
        """
        if debuglevel:
            sys.stderr.write('connect: %s, %s\n' % (self.host, self.port,))

        try:
            (app, script_name) = self.get_app(self.host, self.port)
            if app:
                if debuglevel:
                    sys.stderr.write('INTERCEPTING call to %s:%s\n' %
                                     (self.host, self.port,))
                self.sock = wsgi_fake_socket(app, self.host, self.port,
                                             script_name, https=True)
            else:
                try:
                    import ssl
                    if not hasattr(self, 'key_file'):
                        self.key_file = None
                    if not hasattr(self, 'cert_file'):
                        self.cert_file = None
                    if not hasattr(self, '_context'):
                        try:
                            self._context = ssl.create_default_context()
                        except AttributeError:
                            self._context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
                            self._context.options |= ssl.OP_NO_SSLv2
                            if not hasattr(self, 'check_hostname'):
                                self._check_hostname = (
                                    self._context.verify_mode != ssl.CERT_NONE
                                )
                            else:
                                self._check_hostname = self.check_hostname
                except (ImportError, AttributeError):
                    pass
                HTTPSConnection.connect(self)

        except Exception:
            if debuglevel:              # intercept & print out tracebacks
                traceback.print_exc()
            raise