Skip to content

Commit

Permalink
Adds optional reply_to email reporter config option (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorshannon authored Feb 15, 2024
1 parent 30653a3 commit b9c6d88
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/
### Changed

- Remove EOL'd Python 3.7 (new minimum requirement is Python 3.8), add Python 3.12 testing
- Adds optional `reply_to` option for email reporters (#794 by trevorshannon)

### Fixed

Expand Down
8 changes: 6 additions & 2 deletions lib/urlwatch/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,25 @@ class Mailer(object):
def send(self, msg):
raise NotImplementedError

def msg_plain(self, from_email, to_email, subject, body):
def msg_plain(self, from_email, to_email, reply_to_email, subject, body):
msg = email.mime.text.MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
msg['Date'] = email.utils.formatdate()
if reply_to_email:
msg['Reply-To'] = reply_to_email

return msg

def msg_html(self, from_email, to_email, subject, body_text, body_html):
def msg_html(self, from_email, to_email, reply_to_email, subject, body_text, body_html):
msg = email.mime.multipart.MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
msg['Date'] = email.utils.formatdate()
if reply_to_email:
msg['Reply-To'] = reply_to_email

msg.attach(email.mime.text.MIMEText(body_text, 'plain', 'utf-8'))
msg.attach(email.mime.text.MIMEText(body_html, 'html', 'utf-8'))
Expand Down
5 changes: 3 additions & 2 deletions lib/urlwatch/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,13 @@ def submit(self):
else:
logger.error('Invalid entry for method {method}'.format(method=self.config['method']))

reply_to = self.config.get('reply_to', '')
if self.config['html']:
body_html = '\n'.join(self.convert(HtmlReporter).submit())

msg = mailer.msg_html(self.config['from'], self.config['to'], subject, body_text, body_html)
msg = mailer.msg_html(self.config['from'], self.config['to'], reply_to, subject, body_text, body_html)
else:
msg = mailer.msg_plain(self.config['from'], self.config['to'], subject, body_text)
msg = mailer.msg_plain(self.config['from'], self.config['to'], reply_to, subject, body_text)

mailer.send(msg)

Expand Down
1 change: 1 addition & 0 deletions lib/urlwatch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
'html': False,
'to': '',
'from': '',
'reply_to': '',
'subject': '{count} changes: {jobs}',
'method': 'smtp',
'smtp': {
Expand Down

0 comments on commit b9c6d88

Please sign in to comment.