def remind(self, auth_token): """ Sends remind mail. If only_if_noreply is true, it is only sent if there was no reply. """ if self.state != 'queued': logging.warning('ignoring remind call for un-queued job {}'.format( self.key)) return logging.info('processing remind for job {}'.format(self.key)) mailman = gmail.Mailman(self.user_email, auth_token) try: if self.only_if_noreply: reply = self.find_reply(mailman) if reply is not None: # reply detected, don't send reminder but set state to # done logging.info('reply detected, not sending reminder') self.state = 'done' self.disabled_reply = DisabledReply.from_gmail_dict(reply) self.put() return logging.info('sending reminder') mail = mailnotify.build_remind_message(self) mail = mailman.build_reply(self.thread_id_int, mail) mailman.send_mail(mail) self.state = 'done' self.put() finally: mailman.quit()
def setUp(self): imap_patcher = mock.patch('sndlatr.gmail.IMAPSession') smtp_patcher = mock.patch('sndlatr.gmail.SMTPSession') self.imap_mock = imap_patcher.start().return_value self.smtp_mock = smtp_patcher.start().return_value self.addCleanup(imap_patcher.stop) self.addCleanup(smtp_patcher.stop) self.mailman = gmail.Mailman('testuser', 'testauth')
def test_smtp(user, msgid): credentials = get_credentials() mailman = gmail.Mailman(user, credentials.access_token) # print('boxes are') # print(json.dumps(imap.boxes_flags, indent=4)) message_id = int(msgid, 16) print "sending..." rfc_id = gmail.make_message_id() mail = mailman.send_draft(message_id, rfc_id) print "marking as sent." mailman.mark_as_sent(message_id, rfc_id, mail) print('done')
def test_reply(user, msgid): credentials = get_credentials() mailman = gmail.Mailman(user, credentials.access_token) thread_id = int(msgid, 16) print mailman.get_thread(thread_id) return msg = mailman.build_reply(thread_id) msg.sender = user msg.to = user msg.body = 'this is your reminder' msg.html = '<html><body>this is <b>your</b> reminder</body></html>' mailman.send_mail(msg)
def send_mail(self, auth_token): """ Sends mail in to steps (sending, and marking as sent) that both can fail and be retried independently to minimize the effect of failures that lead to double sendings. The mail is guranteed to have been sent when state is sent or done. It is guranteed to have been marked as sent when state equals 'done'. """ logging.debug('send_mail called mail: {}, job:{}'.format( self.message_id, self.key.id())) mailman = gmail.Mailman(self.user_email, auth_token) mail = None try: # step 1, send mail if self.state == 'queued': logging.debug('sending mail') self.sent_mail_rfc_id = gmail.make_message_id() mail = mailman.send_draft(self.message_id_int, self.sent_mail_rfc_id) logging.debug('mail was sent ' + self.sent_mail_rfc_id) self.state = 'sent' self.put() # step 2, move to sent folder if self.state == 'sent': logging.debug('marking mail as sent') try: mailman.mark_as_sent(self.message_id_int, self.sent_mail_rfc_id, mail) logging.debug('mail marked as sent:') except gmail.MailNotFound: logging.debug('mail not found, ignoring') self.state = 'done' self.put() finally: mailman.quit()
def disable_if_replied(self, auth_token): """ Checks if there was a reply and disables job if there was. """ logging.info('processing disable_if_replied for {}'.format(self.key)) if self.state != 'checking': logging.warn('job not in checking state, skipping') return if not self.only_if_noreply: logging.warn('only_if_noreply not configured, skipping') self.state = 'scheduled' self.put() return mailman = gmail.Mailman(self.user_email, auth_token) try: reply = self.find_reply(mailman) finally: mailman.quit() if reply is not None: logging.info('reply found, disabling job') self.state = 'disabled' self.disabled_reply = DisabledReply.from_gmail_dict(reply) self.put() else: self.state = 'scheduled' self.put()