Skip to content

Commit

Permalink
more flexible construction of "From" headers
Browse files Browse the repository at this point in the history
... when replying/forwarding mails.
This now respects the new reply_force_realname and reply_force_address
config options and uses regex to match recipients with ones own addresses.
This way one can define aliases matching "plussed" recipient addresses
like this:

 [accounts]
   [[gmail]]
     realname = Patrick Totzke
     address = patricktotzke@gmail.com
     aliases = patricktotzke@googlemail.com, patricktotzke\+.*@gmail.com

cf issue pazz#515
  • Loading branch information
pazz committed Sep 22, 2012
1 parent 4bf2bb9 commit 7536499
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions alot/commands/thread.py
Expand Up @@ -2,6 +2,7 @@
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
import os
import re
import logging
import tempfile
from twisted.internet.defer import inlineCallbacks
Expand Down Expand Up @@ -54,22 +55,36 @@ def recipient_to_from(mail, my_accounts):
if delivered_to is not None:
recipients.append(delivered_to)

logging.debug('recipients: %s' % recipients)
# pick the most important account that has an address in recipients
# and use that accounts realname and the found recipient address
for acc in my_accounts:
acc_addresses = acc.get_addresses()
for rec in recipients:
_, raddress = parseaddr(rec)
raddress = raddress.decode()
if raddress in acc_addresses and realname is None:
realname = acc.realname
address = raddress
for alias_re in acc_addresses:
if realname is not None:
break
regex = re.compile(alias_re)
for rec in recipients:
seen_name, seen_address = parseaddr(rec)
if regex.match(seen_address):
logging.debug("match!: '%s' '%s'" % (seen_address, alias_re))
if settings.get('reply_force_realname'):
realname = acc.realname
else:
realname = seen_name
if settings.get('reply_force_address'):
address = acc.address
else:
address = seen_address

# revert to default account if nothing found
if realname is None:
realname = my_accounts[0].realname
address = my_accounts[0].address
return realname, address
logging.debug('using realname: "%s"' % realname)
logging.debug('using address: %s' % address)

return address if realname == '' else '%s <%s>' % (realname, address)


@registerCommand(MODE, 'reply', arguments=[
Expand Down Expand Up @@ -135,8 +150,7 @@ def apply(self, ui):
envelope.add('Subject', subject)

# set From
realname, address = recipient_to_from(mail, my_accounts)
envelope.add('From', '%s <%s>' % (realname, address))
envelope.add('From', recipient_to_from(mail, my_accounts))

# set To
sender = mail['Reply-To'] or mail['From']
Expand Down Expand Up @@ -258,8 +272,7 @@ def apply(self, ui):
envelope.add('Subject', subject)

# set From
realname, address = recipient_to_from(mail, my_accounts)
envelope.add('From', '%s <%s>' % (realname, address))
envelope.add('From', recipient_to_from(mail, my_accounts))

# continue to compose
ui.apply_command(ComposeCommand(envelope=envelope,
Expand Down

0 comments on commit 7536499

Please sign in to comment.