Created
August 24, 2015 14:42
-
-
Save hasufell/a21bb257cb6690ae096f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Copyright (c) 2015, Julian Ospald <hasufell@posteo.de> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of the <organization> nor the | |
# names of its contributors may be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
# Very basic example of using Python and IMAP to iterate over emails in a | |
# gmail folder/label. Supports TLS and starttls. | |
# | |
import sys | |
import getopt | |
import imaplib | |
import getpass | |
import email | |
import email.header | |
import datetime | |
def connect_server(server, port, account, password, folder, title, tls): | |
if tls == "starttls": | |
M = imaplib.IMAP4(server, port) | |
M.starttls() | |
elif tls == "yes": | |
M = imaplib.IMAP4_SSL(server, port) | |
else: | |
M = imaplib.IMAP4(server, port) | |
try: | |
rv, data = M.login(account, password) | |
except imaplib.IMAP4.error: | |
print("LOGIN FAILED!!! ") | |
sys.exit(1) | |
print(rv, data) | |
rv, mailboxes = M.list() | |
if rv == 'OK': | |
print("Mailboxes:") | |
print(mailboxes) | |
rv, data = M.select(folder) | |
if rv == 'OK': | |
print("Processing mailbox...\n") | |
found = process_mailbox(M, title) | |
M.close() | |
else: | |
print("ERROR: Unable to open mailbox ", rv) | |
M.logout() | |
if found == False: | |
sys.exit(3) | |
def process_mailbox(M, want_subj): | |
""" | |
Do something with emails messages in the folder. | |
For the sake of this example, print some headers. | |
""" | |
found = False | |
rv, data = M.search(None, "ALL") | |
if rv != 'OK': | |
print("No messages found!") | |
return | |
for num in data[0].split(): | |
rv, data = M.fetch(num, '(RFC822)') | |
if rv != 'OK': | |
print("ERROR getting message", num) | |
return | |
msg = email.message_from_bytes(data[0][1]) | |
decode = email.header.decode_header(msg['Subject'])[0] | |
subject = str(decode[0]) | |
if subject == want_subj: | |
found = True | |
print('Message %s: %s' % (num, subject)) | |
print('Raw Date:', msg['Date']) | |
# Now convert to local date-time | |
date_tuple = email.utils.parsedate_tz(msg['Date']) | |
if date_tuple: | |
local_date = datetime.datetime.fromtimestamp( | |
email.utils.mktime_tz(date_tuple)) | |
print("Local Date:", local_date.strftime("%a, %d %b %Y %H:%M:%S")) | |
return found | |
def print_help_and_exit(): | |
print(''' | |
Usage: check_ipv6_mail [ options ] mail-title | |
options: | |
--help, -h Show help | |
--server, -s Server host (string) | |
--port, -p Server port (int) | |
--account, -a Full mail account name (string) | |
--password, -c Mail account password (string) | |
--folder, -f Mail folder to check (string) | |
--tls, -t Whether to use tls (yes|starttls|none) | |
''') | |
sys.exit(2) | |
def main(argv): | |
server = "" | |
port = 143 | |
account = "" | |
password = "" | |
folder = "Inbox" | |
title = "" | |
tls = "none" | |
try: | |
opts, args = getopt.getopt(argv, "hs:p:a:c:f:t", ["server=", "port=", "account=", "password=", "folder=", "tls="]) | |
except getopt.GetoptError: | |
print_help_and_exit() | |
for opt, arg in opts: | |
if opt == '-h': | |
print_help_and_exit() | |
elif opt in ("-s", "--server"): | |
server = arg | |
elif opt in ("-p", "--port"): | |
port = arg | |
elif opt in ("-a", "--account"): | |
account = arg | |
elif opt in ("-c", "--password"): | |
password = arg | |
elif opt in ("-f", "--folder"): | |
folder = arg | |
elif opt in ("-t", "--tls"): | |
tls = arg | |
if len(argv) == 0: | |
print_help_and_exit() | |
else: | |
title = argv[-1] | |
connect_server(server, port, account, password, folder, title, tls) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment