Tuesday, June 03, 2008

Ad hoc mailing list using LDAP query and sendmail aliases

Scenario: someone wants to send out mail to everyone in your organization's directory, except for one person. A birthday party, for example. Here's an example of how to search an LDAP server for a group of people's email addresses. We then filter the result through some utilities to reduce this to a plain list of email addresses. The following is only one way (and not the cleanest) to do it.

# ldapsearch -x groupMembership=cn=contactlist,ou=orgunit,o=org mail \ # return mail attrib
| grep "^mail:" \
# Look for 'mail:' at the beginning of the line
| cut -b 7- \
# cut the first six characters ('mail: ') from the line
| sort \
# sort the addresses
| uniq > /tmp/outputlist
# remove duplicates and save to /tmp/outputlist

Look through /tmp/outputlist. Remove the user you don't need using your favorite text editor. Once you've decided that it's got what you want, move it to /opt/scripts/person-event or whatever you want to call the list.

Now, edit /etc/aliases and add this line:

# Mailing list for everyone from contactlist except "person"
person-event: :include:/opt/scripts/person-event

Note that there is a colon character on each side of the "include" keyword. Now run newaliases to update the aliases.db file.

Test that the alias is working:

#/usr/lib/sendmail -bv person-event

This should spit out a long list of resolved aliases.

That's it. Now email to person-event@example.com to have the message delivered to everyone but the one person. Obviously, this will work for any sort of small email list where you don't need subscription or bounce management. For lists that need more sophisticated management, use Mailman.

No comments: