You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2018/09/12 13:57:55 UTC

[GitHub] feluelle commented on a change in pull request #3661: [AIRFLOW-2780] Adds IMAP Hook to interact with a mail server

feluelle commented on a change in pull request #3661: [AIRFLOW-2780] Adds IMAP Hook to interact with a mail server
URL: https://github.com/apache/incubator-airflow/pull/3661#discussion_r217041753
 
 

 ##########
 File path: airflow/contrib/hooks/imap_hook.py
 ##########
 @@ -0,0 +1,262 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import email
+import imaplib
+import re
+
+from airflow import LoggingMixin
+from airflow.hooks.base_hook import BaseHook
+
+
+class ImapHook(BaseHook):
+    """
+    This hook connects to a mail server by using the imap protocol.
+
+    :param imap_conn_id: The connection id that contains the information
+                         used to authenticate the client.
+                         The default value is 'imap_default'.
+    :type imap_conn_id: str
+    """
+
+    def __init__(self, imap_conn_id='imap_default'):
+        super(ImapHook, self).__init__(imap_conn_id)
+        self.conn = self.get_connection(imap_conn_id)
+        self.mail_client = imaplib.IMAP4_SSL(self.conn.host)
+
+    def __enter__(self):
+        self.mail_client.login(self.conn.login, self.conn.password)
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.mail_client.logout()
 
 Review comment:
   According to [this](https://docs.python.org/2/library/imaplib.html#imaplib.IMAP4.logout) it shutdowns the connection to the server.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services