Files
modoboa/extensions/postfix_autoreply/scripts/autoreply.py
T
tonio c01d6de7d8 fix: return receipts are now displayed correctly.
new: parameters module refactored. The API is more simple now, there is no need to provide a module name anymore, this last is automatically guessed. Also, a new 'label' field can be provide when declaring a new parameter. It will be used instead of the name.

new: email parsing refactored. My first attempt was really ugly. I did not really studied the various RFC describing how messages are formatted or what IMAP fetch queries can do. Now, I hope this new code will face better unknown types. At least, It will be simpler to update.
2010-09-17 09:23:35 +02:00

57 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import smtplib
from email.mime.text import MIMEText
import datetime
from modoboa.lib import parameters
from modoboa.admin.models import Mailbox
from modoboa.extensions.postfix_autoreply import main
from modoboa.extensions.postfix_autoreply.models import ARmessage, ARhistoric
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Not enough arguments, aborting."
sys.exit(1)
sender = sys.argv[1]
mailbox = sys.argv[2]
mbox = Mailbox.objects.get(full_address=mailbox)
try:
armessage = ARmessage.objects.get(mbox=mbox.id, enabled=True)
except ARmessage.DoesNotExist:
sys.exit(0)
if armessage.untildate < datetime.datetime.now():
armessage.enabled = False
armessage.save()
sys.exit(0)
try:
lastar = ARhistoric.objects.get(armessage=armessage.id, sender=sender)
main.init()
timeout = parameters.get_admin("AUTOREPLIES_TIMEOUT",
app="postfix_autoreply")
delta = datetime.timedelta(seconds=int(timeout))
if lastar.last_sent + delta > datetime.datetime.now():
sys.exit(0)
except ARhistoric.DoesNotExist:
lastar = ARhistoric()
lastar.armessage = armessage
lastar.sender = sender
msg = MIMEText(armessage.content.encode('utf-8'), _charset='utf-8')
msg['Subject'] = armessage.subject
msg['From'] = mailbox
msg['To'] = sender
s = smtplib.SMTP()
s.connect()
s.sendmail(mailbox, sender, msg.as_string())
s.quit()
lastar.last_sent = datetime.datetime.now()
lastar.save()
sys.exit(0)