def save_attachments(in_folder):
    # Saves attachments from messages in the global message_folder var
    # in_folder var is the start point to search for message_folder
    global message_folder
    global subject_search

    tn = UTC_NOW()
    earliest = tn - timedelta(days=30)
    for dirname, dirnames, filenames in os.walk('./tmp'):
        for filename in filenames:
            os.remove(os.path.join(dirname, filename))
    search_folder = [f for f in in_folder.walk()
                     if f.name == message_folder][0]
    filtered_folder = search_folder.filter(subject__contains=subject_search)
    if not os.path.exists('./tmp'):
        os.mkdir('./tmp')
    for item in filtered_folder:
        if item.datetime_received >= earliest:
            for attachment in item.attachments:
                if isinstance(attachment, FileAttachment):
                    local_path = os.path.join('./tmp', attachment.name)
                    with open(local_path, 'wb') as f:
                        f.write(attachment.content)
                    print('Saved attachment to', local_path)
        item.is_read = True
        item.save()
Exemple #2
0
 def store_next_events(self):
     """
     Get next events interesting fields. Next = between now and 1 day
     """
     self.next_events = [
         {
             "subject": item.subject,
             "start": item.start,
             "location": item.location,
             "remaining": item.start - UTC_NOW(),
             "sensitive": item.sensitivity in {"Private", "Confidential"},
             "sensitivity": item.sensitivity,
             "id": item.id,
         }
         # pylint: disable=no-member
         for item in self.account.calendar.filter(
             start__gt=UTC_NOW()).filter(
                 start__lt=UTC_NOW() + timedelta(days=1)).order_by("start")
     ]
def save_attach(attach_name, path_to_save, name_to_save):
    #generate a time difference variable for the recency of hours that messages have arrived
    since = UTC_NOW() - timedelta(hours=4)
    #generate a timestamp for the print message
    timestr = time.strftime("%Y%m%d-%H%M_")
    #filter the items in the general inbox by date received less than since variable (1 hour)
    for item in a.inbox.all().filter(
            datetime_received__gt=since).order_by('-datetime_received'):
        #look at each of the attachments
        for attachment in item.attachments:
            #check if the attachment is a FileAttachment (class type)
            if isinstance(attachment, FileAttachment):
                #check if the attach_name is in the file attachment
                if attach_name in attachment.name:
                    print('first print statement: ' + attachment.name)
                    #define the path and name of the file to save as
                    local_path = Path(path_to_save, name_to_save)
                    #open the path to save
                    with open(local_path, 'wb') as f:
                        #write the attachment
                        f.write(attachment.content)
                    print('saved attachment to', local_path)
                    #move the message to the auto_folder
                    item.move(auto_folder)
Exemple #4
0
    from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
    BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
    # Disable insecure TLS warnings
    warnings.filterwarnings("ignore")

# Use notify-send for email notifications and zenity for calendar notifications
notify = sh.Command('/usr/bin/notify-send')
zenity = sh.Command('/usr/bin/zenity')

# Get the local timezone
tz = EWSTimeZone.localzone()

sleep = int(
    sys.argv[1]
)  # 1st arg to this script is the number of seconds to look back in the inbox
now = UTC_NOW()
emails_since = now - timedelta(seconds=sleep)
cal_items_before = now + timedelta(
    seconds=sleep *
    4)  # Longer notice of upcoming appointments than new emails
username, _, password = netrc().authenticators('office365')
c = Credentials(username, password)
a = Account(primary_smtp_address=c.username,
            credentials=c,
            access_type=DELEGATE,
            autodiscover=True)

for msg in a.calendar.view(start=now, end=cal_items_before)\
        .only('start', 'end', 'subject', 'location')\
        .order_by('start', 'end'):
    if msg.start < now:
Exemple #5
0
two_hours_later = localized_dt + timedelta(hours=2)
two_hours = two_hours_later - localized_dt
two_hours_later += timedelta(hours=2)

# Dates
my_date = EWSDate(2017, 9, 5)
today = EWSDate.today()
also_today = right_now.date()
also_today += timedelta(days=10)

# UTC helpers. 'UTC' is the UTC timezone as an EWSTimeZone instance.
# 'UTC_NOW' returns a timezone-aware UTC timestamp of current time.
from exchangelib import UTC, UTC_NOW

right_now_in_utc = UTC.localize(EWSDateTime.now())
right_now_in_utc = UTC_NOW()

# Already have a Python datetime object you want to use? Make sure it's localized. Then pass
# it to from_datetime().
pytz_tz = pytz.timezone('Europe/Copenhagen')
py_dt = pytz_tz.localize(datetime(2017, 12, 11, 10, 9, 8))
ews_now = EWSDateTime.from_datetime(py_dt)

###Creating, updating, deleting, sending, moving, archiving

# Here's an example of creating a calendar item in the user's standard calendar.  If you want to
# access a non-standard calendar, choose a different one from account.folders[Calendar].
#
# You can create, update and delete single items:
from exchangelib import Account, CalendarItem, Message, Mailbox, FileAttachment, HTMLBody
from exchangelib.items import SEND_ONLY_TO_ALL, SEND_ONLY_TO_CHANGED