[Dovecot] Quota notifications: Allow to send to external address (BCC via mailcow UI)

This commit is contained in:
andryyy
2021-05-23 09:49:36 +02:00
parent 9c075af2d9
commit beda649ecf
6 changed files with 36 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ import jinja2
from jinja2 import Template
import redis
import time
import json
import sys
import html2text
from subprocess import Popen, PIPE, STDOUT
@@ -57,6 +58,27 @@ try:
p = Popen(['/usr/lib/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.communicate(input=bytes(msg.as_string(), 'utf-8'))
domain = username.split("@")[-1]
if domain and r.hget('QW_BCC', domain):
bcc_data = json.loads(r.hget('QW_BCC', domain))
bcc_rcpts = bcc_data['bcc_rcpts']
if bcc_data['active'] == 1:
for rcpt in bcc_rcpts:
msg = MIMEMultipart('alternative')
msg['From'] = username
subject = r.get('QW_SUBJ') or "Quota warning"
msg['Subject'] = subject + ' (' + username + ')'
msg['Date'] = formatdate(localtime = True)
text_part = MIMEText(text, 'plain', 'utf-8')
html_part = MIMEText(html, 'html', 'utf-8')
msg.attach(text_part)
msg.attach(html_part)
msg['To'] = rcpt
server = smtplib.SMTP('postfix', 588, 'quarantine')
server.ehlo()
server.sendmail(msg['From'], str(rcpt), msg.as_string())
server.quit()
except Exception as ex:
print('Failed to send quota notification: %s' % (ex))
sys.exit(1)
@@ -69,4 +91,4 @@ except:
try:
sys.stderr.close()
except:
pass
pass