Exemplo n.º 1
0
 def testGmailSend_p07_clearBody_HTML(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setTextBody('Field is populated')
     m.setHtmlBody('Field is populated')
     m.clearBody('HTML')
     self.assertEqual(m._textBody, 'Field is populated')
     self.assertEqual(m._htmlBody, None)
Exemplo n.º 2
0
 def testGmailSend_p08_clearBody_invalid_value(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setTextBody('Field is populated')
     m.setHtmlBody('Field is populated')
     m.clearBody('invalid')
     self.assertEqual(m._textBody, 'Field is populated')
     self.assertEqual(m._htmlBody, 'Field is populated')
Exemplo n.º 3
0
 def testGmailSend_p01_clearRecipients_none(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addRecipients(['*****@*****.**', '*****@*****.**'])
     m.addRecipients(['*****@*****.**', '*****@*****.**'], 'cc')
     m.addRecipients(['*****@*****.**', '*****@*****.**'], 'bcc')
     m.clearRecipients()
     self.assertEqual(m._to, [])
     self.assertEqual(m._cc, [])
     self.assertEqual(m._bcc, [])
Exemplo n.º 4
0
 def testGmailSend_p02_init_populated(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     self.assertEqual(m._sendfrom, '*****@*****.**')
     self.assertEqual(m._sendpass, 'password')
     self.assertEqual(m._replyto, None)
     self.assertEqual(m._textBody, None)
     self.assertEqual(m._htmlBody, None)
     self.assertEqual(m._subject, '')
     self.assertEqual(m._to, [])
     self.assertEqual(m._cc, [])
     self.assertEqual(m._bcc, [])
     self.assertEqual(m._attach, [])
Exemplo n.º 5
0
 def testGmailSend_p02_addRecipients_to(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addRecipients(['*****@*****.**', '*****@*****.**'], 'to')
     self.assertEqual(m._to, ['*****@*****.**', '*****@*****.**'])
     self.assertEqual(m._cc, [])
     self.assertEqual(m._bcc, [])
Exemplo n.º 6
0
 def testGmailSend_p02_clearAttachments_has_values(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addAttachment('ken.txt', 'ken.txt')
     m.addAttachment('ken2.txt', 'ken2.txt')
     m.clearAttachments()
     self.assertEqual(m._attach, [])
Exemplo n.º 7
0
 def testGmailSend_p01_init(self):
     self.assertEqual(
         kvgmailsend.GmailSend('*****@*****.**', 'password')._from,
         '*****@*****.**')
Exemplo n.º 8
0
 def testGmailSend_p04_addRecipient_bcc(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addRecipient('*****@*****.**', 'bcc')
     self.assertEqual(m._to, [])
     self.assertEqual(m._cc, [])
     self.assertEqual(m._bcc, ['*****@*****.**'])
Exemplo n.º 9
0
 def testGmailSend_p01_setFrom(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setFrom('*****@*****.**')
     self.assertEqual(m._from, '*****@*****.**')
Exemplo n.º 10
0
 def testGmailSend_p01_setReplyTo(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setReplyTo('*****@*****.**')
     self.assertEqual(m._replyto, '*****@*****.**')
Exemplo n.º 11
0
 def testGmailSend_f01_send_no_recipient(self):
     with self.assertRaises(Exception) as context:
         m = kvgmailsend.GmailSend('*****@*****.**', 'password')
         m.setTextBody('Field is populated')
         m.send()
Exemplo n.º 12
0
 def testGmailSend_p01_setSubject(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setSubject('this is my subject')
     self.assertEqual(m._subject, 'this is my subject')
Exemplo n.º 13
0
def gmail_poll_by_function(base_email_settings,
                           winesel_storelist,
                           wine2_storelist,
                           sleepseconds,
                           debug=False):
    # debug
    if debug: print('init-gmailrcv')

    # create the IMAP object
    mail = kvgmailrcv.init(base_email_settings)

    # this should be a do while True statement but we are testing
    #for i in range(30):
    while True:
        # debugging
        logger.debug('select_folder:%s', base_email_settings['imap_folder'])
        if debug:
            print('select_folder:', base_email_settings['imap_folder'])

        # get the message
        try:
            mailids = kvgmailrcv.select_folder(
                mail, base_email_settings['imap_folder'])
        except Exception as e:
            logger.warning('select_folder:failed:')
            logger.warning('str(e):%s', str(e))
            logger.warning('type(e):%s', type(e))
            logger.warning('e.args:%s', e.args)
            if isinstance(e, imaplib.IMAP4.abort):
                if str(e) in mailerrors:
                    logger.warning(
                        'rebuild the mail object by calling  kvgmailrcv.init')
                    mail = kvgmailrcv.init(base_email_settings)
                else:
                    logger.error('terminating application')
                    # restart the mail receiving object
                    sys.exit(1)
            else:
                logger.error('terminating application')
                # restart the mail receiving object
                sys.exit(1)

        # action if we found a record
        if mailids:
            # set the flag
            msgProcessed = True

            # grab the oldest message to process first
            msgid = mailids[0]

            # get the imap object
            mparse = kvgmailrcv.get_imap_msg(mail, msgid)

            # print the subject
            logger.info('msgid:%s:subject:%s', msgid, mparse.subject)
            logger.info('from:%s', mparse.from_email)

            # process this email message
            try:
                # setup the email sender
                m = kvgmailsend.GmailSend(base_email_settings['user'],
                                          base_email_settings['password'])
                # check if undeliverable is in the subject
                if reUndeliverable.search(mparse.subject):
                    logger.info(
                        'Undeliverable in the subject - skippping this message'
                    )
                elif 'wine_rpt.py' in mparse.subject:
                    # check the subject if it is from wine_email.py - then
                    # don't process this message just forward it to someone
                    # that can deal with it
                    logger.info('reply to wine_rpt.py - save messages in:%s',
                                optiondict['mime_dir'])
                    msg_dir = kvgmailrcv.save_message(mparse,
                                                      optiondict['mime_dir'])
                    logger.info('reply to wine_rpt.py - message save in:%s',
                                msg_dir)
                    logger.info('reply to wine_rpt.py - forward message to:%s',
                                optiondict['email_forward'])
                    m.addRecipients([optiondict['email_forward']])
                    m.setSubject(
                        '[email protected] Forwarded Email:{}'.format(
                            msg_dir))
                    m.setHtmlBody(mparse.body)
                    m.send()
                    logger.info('forwarded message sent')
                elif mparse.from_email[0] not in optiondict['valid_emails']:
                    # check who this message came from and assure it is from a valid list
                    # don't process this message just forward it to someone
                    # that can deal with it
                    logger.info('invalid from_email:%s:save messages in:%s',
                                mparse.from_email[0], optiondict['mime_dir'])
                    msg_dir = kvgmailrcv.save_message(mparse,
                                                      optiondict['mime_dir'])
                    logger.info('invalid from_email - message save in:%s',
                                msg_dir)
                    logger.info('invalid from_email - forward message to:%s',
                                optiondict['email_forward'])
                    m.addRecipients([optiondict['email_forward']])
                    m.setSubject(
                        '[email protected] Forwarded Email:{}'.format(
                            msg_dir))
                    m.setHtmlBody(mparse.body)
                    m.send()
                    logger.info('forwarded message sent')
                else:
                    logger.info('Processing email subject:%s', mparse.subject)
                    logger.info('Reply to:%s', mparse.from_email)
                    m.addRecipients(mparse.from_email)
                    if mparse.cc_mail:
                        m.addRecipients(mparse.cc_email, 'cc')
                    m.setSubject('WineLookup:' + mparse.subject)
                    # get the message body by doing the message lookup
                    m.setHtmlBody(
                        wineutil.html_body_from_email_subject(
                            mparse.subject,
                            winesel_storelist,
                            winereq_storelist,
                            debug=debug))
                    m.send()
                    logger.info('response emailed')
            except Exception as e:
                logger.error('Failed to process msg[%s]-error:%s', msgid,
                             str(e))
                msgProcessed = False
                # i think we need to fail here rather than continue to loop
                sys.exit(1)

            # finally delete this message
            if msgProcessed:
                logger.info('moving message to folder:Processed')
                kvgmailrcv.move_message(mail, msgid, 'Processed')
        else:
            logger.info('Sleeping %d seconds - and checking again',
                        sleepseconds)
            time.sleep(sleepseconds)
Exemplo n.º 14
0
 def testGmailSend_p01_setHtmlBody_value(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setHtmlBody('Field is populated')
     self.assertEqual(m._textBody, None)
     self.assertEqual(m._htmlBody, 'Field is populated')
Exemplo n.º 15
0
 def testGmailSend_p01_clearAttachments_empty(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.clearAttachments()
     self.assertEqual(m._attach, [])
Exemplo n.º 16
0
 def testGmailSend_p03_addAttachment_fname_and_attachname(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addAttachment('ken.txt', 'ken.txt')
     self.assertEqual(m._attach, [('ken.txt', 'ken.txt')])
Exemplo n.º 17
0
 def testGmailSend_f01_init_invalid_sendfrom(self):
     with self.assertRaises(Exception) as context:
         m = kvgmailsend.GmailSend('invalid.email.com', 'password')
Exemplo n.º 18
0
 def testGmailSend_p01_addAttachment_fname_none(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.addAttachment(None, None)
     self.assertEqual(m._attach, [])
Exemplo n.º 19
0
 def testGmailSend_p02_setHtmlBody_None(self):
     m = kvgmailsend.GmailSend('*****@*****.**', 'password')
     m.setHtmlBody(None)
     self.assertEqual(m._textBody, None)
     self.assertEqual(m._htmlBody, None)
Exemplo n.º 20
0
    # load and proces the files
    results = load_and_process_files_notAvail(debug=False)

    # load and proces the files
    results = load_and_process_files_missing_stores(debug=False)
    if optiondict['winestoreproximity']:
        results = load_and_process_files_missing_stores(
            in_store_reqts=optiondict['winestoreproximity'], debug=False)

    # final summary
    with open('email_body.html', 'w') as t:
        t.write(gen_email_body_html())

    # setup the email sender
    if not optiondict['nomail']:
        m = kvgmailsend.GmailSend(optiondict['email_user'],
                                  optiondict['email_password'])

        m.addRecipients(optiondict['email_to'])
        if optiondict['email_subject']:
            m.setSubject('wine_rpt.py (v' + optiondict['AppVersion'] +
                         ') run at ' + str(datetime.datetime.now()) +
                         ' from [' + str(gethostname()) + '] ' +
                         optiondict['email_subject'])
        else:
            m.setSubject('wine_rpt.py (v' + optiondict['AppVersion'] +
                         ') run at ' + str(datetime.datetime.now()) +
                         ' from [' + str(gethostname()) + ']')
        #m.setTextBody(gen_email_body_text())
        m.setHtmlBody(gen_email_body_html())
        m.addAttachment('winetoday.csv')
        m.send()
Exemplo n.º 21
0
 def testGmailSend_f01_send_bodys_none(self):
     with self.assertRaises(Exception) as context:
         m = kvgmailsend.GmailSend('*****@*****.**', 'password')
         m.send()