Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.utils import make_msgid
- from imaplib import IMAP4, IMAP4_PORT
- from smtplib import SMTP, SMTP_PORT
- from subprocess import call
- from textwrap import dedent
- from time import sleep
- class AutoReplyer:
- refresh_delay = 5
- imap_server = None
- imap_port = IMAP4_PORT
- imap_user = None
- imap_password = None
- smtp_server = None
- smtp_port = SMTP_PORT
- smtp_user = None
- smtp_password = None
- from_address = None
- body = None
- body_html = None
- def __init__(self):
- self.imap = IMAP4(self.imap_server, self.imap_port)
- self.imap.login(self.imap_user, self.imap_password)
- self.smtp = SMTP(self.smtp_server, self.smtp_port)
- self.smtp.login(self.smtp_user, self.smtp_password)
- def close(self):
- self.smtp.close()
- self.imap.logout()
- def create_auto_reply(self, original):
- mail = MIMEMultipart('alternative')
- mail['Message-ID'] = make_msgid()
- mail['References'] = mail['In-Reply-To'] = original['Message-ID']
- mail['Subject'] = 'Re: ' + original['Subject']
- mail['From'] = self.from_address
- mail['To'] = original['Reply-To'] or original['From']
- mail.attach(MIMEText(dedent(self.body), 'plain'))
- mail.attach(MIMEText(self.body_html, 'html'))
- return mail
- def send_auto_reply(self, original):
- self.smtp.sendmail(
- self.from_address, [original['From']],
- self.create_auto_reply(original).as_bytes())
- log = 'Replied to ā%sā for the mail ā%sā' % (original['From'],
- original['Subject'])
- print(log)
- try:
- call(['notify-send', log])
- except FileNotFoundError:
- pass
- def reply(self, mail_number):
- self.imap.select(readonly=True)
- _, data = self.imap.fetch(mail_number, '(RFC822)')
- self.imap.close()
- self.send_auto_reply(email.message_from_bytes(data[0][1]))
- self.imap.select(readonly=False)
- self.imap.store(mail_number, '+FLAGS', '\\Answered')
- self.imap.close()
- def check_mails(self):
- self.imap.select(readonly=False)
- _, data = self.imap.search(None, '(UNSEEN UNANSWERED)')
- self.imap.close()
- for mail_number in data[0].split():
- self.reply(mail_number)
- def run(self):
- try:
- while True:
- self.check_mails()
- sleep(self.refresh_delay)
- finally:
- self.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement