Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Group emails

While this may not be a central feature , mac os X server had in its previous implementation (up to 10.4) the capability of sending emails to groups setup in WGM , this feature was sorely lacking in case you wanted the emails backed accross several accounts. After some searches i came across the following at : http://www.snipplr.com/view/42328/osx-server--group-email-enabler/

while not a perfect solution it works with local OD domains and you are free to use it to your needs all the credit shall go to the original author , i just put some modifications in order to get it in working order with SLS.

!/bin/bash
Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
by allowing each WGM group to automatically become an email list for it's members.
#

Jeff Johnson jdj@mac.com
#

To install
1. save this file as
/usr/sbin/group_email.sh
2. sudo chmod +x /usr/sbin/group_email.sh
3. touch /private/var/log/group_email.log
4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
hash:/etc/postfix/group_aliases
5. add the following to /etc/crontab (you may need to create /etc/crontab)
*/15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log
To use
When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
within the comment field. Postfix aliases with group members are then created.
#

If you create a group called 'employees' and put all staff in that group, you will
then have an email setup of employees@domain.com
#

Be sure all mail groups contain at least one member
Would like to add/modify
1. needs error trapping. a group without members returns an error.
2. support for non-local LDAP
clear current aliases
echo "" > /etc/postfix/group_aliases

Get list of groups with 'mail' in the comment field
gr=dscl . -list /Groups Comment | grep mail | awk '{print $1}'

for group in $gr do echo $group: dscl . -read /Groups/$group 'GroupMembership' | cut -d : -f 2 | grep -v "No such key" >> /etc/postfix/group_aliases done

/usr/sbin/postalias /etc/postfix/group_aliases /usr/bin/newaliases exit 0

Hoping this helps you as much as it helped me.
Best Regards,
Bertrand STERN

MacPro Octocore, Mac OS X (10.6.6)

Posted on Jan 7, 2011 5:06 AM

Reply
12 replies

Jan 10, 2011 12:10 PM in response to OSX.Wyoming

[Introduction to Command Line Administration|http://www.apple.com/server/macosx/resources/documentation.html] and [Advanced Bash Scripting|http://tldp.org/LDP/abs/abs-guide.pdf].

The former is specific to its title, while the latter is more advanced and more generic to bash on most any platform.

There are about a gazillion examples of bash scripts in Google, or you can target Google to the Apple discussion forums with the /site:discussions.apple.com whatever keywords you are searching for here/ or analogous syntax.

And the [Postfix|http://www.postfix.org> web site is overrun with stuff, various of which you'll find interesting for working within Postfix.

The OP didn't use the { code } tags around that bash script, so that stuff is going to be a problem to unpack and get to run; that formatting is definitely off.

And cron works if you're careful with your invocation (use +sudo crontab -e+ here) and mind your tab character field delimiters, but launchd and launchctl is preferred way to start tasks these days. (And FWIW, the Lingon tool - a launchd editor - is back and updated and in the Mac app store, too.)

Jan 10, 2011 4:16 PM in response to MrHoffman

Thank you for the pointer Mr. Hoffman,

I think I have much of it figured. One sticky part is the initial invocation of dscl which pulls the list of groups containing mail in the comment. The Group I want to scan is an OD Group, so the invocation is:

gr=dscl /LDAPv3/127.0.0.1 -list .....

And this behaves as expected from the command line and interactively. However, from within the script, it errors at that line with "no such file or directory."

Any ideas?

Barnaby

Jan 11, 2011 4:16 PM in response to MrHoffman

MrHoffman wrote:
We need to see your (failing) script, inside { code } tags.

I'm including spaces here to allow the tags to be viewed.

{ code }
your script
{ code }

Hello Brodie, Mr Hoffman,

Thank you for the help thus far--the suggestions have been spot on. I think this is working in that the following is appearing in /etc/postfix/newaliases:

group: user1, user2

however, the only way I could get e-mail to be delivered to the group was to copy this string into the aliases portion of /etc/aliases and then run newaliases, so I'm missing something. The script also complains with "mail: /var/mail/ladmin: No such file or directory"

{ code }
#!/bin/bash

# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >>
#/private/var/log/group_email.log

# clear current aliases
echo "" > /etc/postfix/group_aliases

# Get list of groups with 'mail' in the comment field
gr=`/usr/bin/dscl /LDAPv3/127.0.0.1 -list /Groups Comment | grep mail | awk '{print $1}'`

for group in $gr
do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group dsAttrTypeNative:memberUid |
cut -d : -f 3 | grep -v "No such key"` >> /etc/postfix/group_aliases
done

/usr/sbin/postalias /etc/postfix/group_aliases
/usr/bin/newaliases
exit 0

{ code }

Jan 11, 2011 8:00 PM in response to OSX.Wyoming

OSX.Wyoming,

Please reread MrHoffman's post on using { code } tags and post your script.
(Hint: remove spaces between { c and e })

Okay now for my guess as to what you have wrong:

Your listed code shows:

do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group dsAttrTypeNative:memberUid |
cut -d : -f 3 | grep -v "No such key"` >> /etc/postfix/group_aliases
done


and it should be one line

do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group dsAttrTypeNative:memberUid | cut -d : -f 3 | grep -v "No such key"` >> /etc/postfix/group_aliases
done


Did you edit /etc/postfix/main.cf and add "alias_maps = hash:/etc/postfix/group_aliases" ?

Hope this helps,

Boddie

Jan 12, 2011 11:08 PM in response to iindie


#!/bin/bash
# Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
# by allowing each WGM group to automatically become an email list for it's members.
#
# Jeff Johnson jdj@mac.com
#
# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log
# To use
# When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
# within the comment field. Postfix aliases with group members are then created.
#
# If you create a group called 'employees' and put all staff in that group, you will
# then have an email setup of employees@domain.com
#
# Be sure all mail groups contain at least one member
# Would like to add/modify
# 1. needs error trapping. a group without members returns an error.
# 2. support for non-local LDAP
# clear current aliases
echo "" > /etc/postfix/group_aliases
# Get list of groups with 'mail' in the comment field
gr=`dscl . -list /Groups Comment | grep mail | awk '{print $1}'`
for group in $gr
do
echo $group: `dscl . -read /Groups/$group 'GroupMembership' | cut -d : -f 2 | grep -v "No such key"` >> /etc/postfix/group_aliases
done
/usr/sbin/postalias /etc/postfix/group_aliases
/usr/bin/newaliases
exit 0



The following is a working version of the same script for 10.6.x SLS using Open Directory.



#!/bin/bash
# Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
# by allowing each WGM group to automatically become an email list for it's members.
#
# Jeff Johnson jdj@mac.com
#
# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log
# To use
# When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
# within the comment field. Postfix aliases with group members are then created.
#
# If you create a group called 'employees' and put all staff in that group, you will
# then have an email setup of employees@domain.com
#
# Be sure all mail groups contain at least one member
# Would like to add/modify
# 1. needs error trapping. a group without members returns an error.
# 2. support for non-local LDAP
# clear current aliases
echo "" > /etc/postfix/group_aliases
# Get list of groups with 'mail' in the comment field
gr=`dscl /LDAPv3/127.0.0.1 -list /Groups Comment | grep mail | awk '{print $1}'`
for group in $gr
do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group 'GroupMembership' | cut -d : -f 2 | grep -v "No such key"` >> /etc/postfix/group_aliases
done
/usr/sbin/postalias /etc/postfix/group_aliases
/usr/bin/newaliases
exit 0


I have tested both version and so far it outputs just fine on SLS both on local and OD versions the script i did not explore the use of dsAttrTypeNative:memberuid but it also seems to work so i guess the code shall be the following for the OD version of the script..



#!/bin/bash

# Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
# by allowing each WGM group to automatically become an email list for it's members.
#
# Jeff Johnson jdj@mac.com
#

# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log

# To use
# When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
# within the comment field. Postfix aliases with group members are then created.
#
# If you create a group called 'employees' and put all staff in that group, you will
# then have an email setup of employees@domain.com
#
# Be sure all mail groups contain at least one member

# Would like to add/modify
# 1. needs error trapping. a group without members returns an error.
# 2. support for non-local LDAP


# clear current aliases
echo "" > /etc/postfix/group_aliases

# Get list of groups with 'mail' in the comment field
gr=`dscl /LDAPv3/127.0.0.1 -list /Groups Comment | grep mail | awk '{print $1}'`

for group in $gr
do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group dsAttrTypeNative:memberUid | cut -d : -f 3 | grep -v "No such key"` >> /etc/postfix/group_aliases
done

/usr/sbin/postalias /etc/postfix/group_aliases
/usr/bin/newaliases
exit 0

Jan 17, 2011 12:03 PM in response to iindie

Many thanks to everyone. These scripts seem to work exactly as advertised. I am getting the string:

\n
group: user1 user2 user3

in the /etc/postfix/group_aliases file. However, even after "postfix reload the" e-mail still bounces--SMTP log rejects the address with 550 5.1.1.

Could this be due to the comma not being present (group: user1, user2, ...)?

I have added the line alias maps=hash:/etc/postfix/groupaliases to /etc/postfix/man.cf at line.

A nice way to check this would be a dump of /etc/postfix/group_aliases, but the man page for hash is somewhat generic.

Again, that you so much to everyone.

Jan 17, 2011 6:22 PM in response to OSX.Wyoming

You said...
"I have added the line aliasmaps=hash:/etc/postfix/groupaliases to /etc/postfix/man.cf at line."


You don't want to add a new line... instead you want to change the existing 'alias_maps' and add hash:/etc/postfix/group_aliases to it.

For example, if you have:
alias_maps = hash:/etc/aliases


Then you would change it to:
alias_maps = hash:/etc/aliases,hash:/etc/postfix/group_aliases


When done, issue:
sudo postfix reload

Feb 24, 2011 11:15 PM in response to UptimeJeff

Definitive versions of the two scripts .


#!/bin/bash
# Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
# by allowing each WGM group to automatically become an email list for it's members.
#
# Jeff Johnson jdj@mac.com
#
# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log
# To use
# When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
# within the comment field. Postfix aliases with group members are then created.
#
# If you create a group called 'employees' and put all staff in that group, you will
# then have an email setup of employees@domain.com
#
# Be sure all mail groups contain at least one member
# Would like to add/modify
# 1. needs error trapping. a group without members returns an error.
# 2. support for non-local LDAP
# clear current aliases
echo "" > /etc/postfix/group_aliases;
# Get list of groups with 'mail' in the comment field
gr=`dscl . -list /Groups Comment | grep mail | awk '{print $1}'`;
for group in $gr
do
echo $group: `dscl . -read /Groups/$group 'GroupMembership' | cut -d : -f 2 | grep -v "No such key"` | awk '{gsub(/[ ]/,",");print}' | sed 's/,//' >>/etc/postfix/group_aliases
done
/usr/sbin/postalias /etc/postfix/group_aliases;
/usr/bin/newaliases;
/usr/sbin/postfix reload;
exit 0



Now for the OD version:


#!/bin/bash
# Group Email Enabler is a simple script which fixes a shortcoming in OS X Server 10.6
# by allowing each WGM group to automatically become an email list for it's members.
#
# Jeff Johnson jdj@mac.com
#
# To install
# 1. save this file as
# /usr/sbin/group_email.sh
# 2. sudo chmod +x /usr/sbin/group_email.sh
# 3. touch /private/var/log/group_email.log
# 4. adjust /etc/postfix/main.cf, add the following to "alias_maps="
# hash:/etc/postfix/group_aliases
# 5. add the following to /etc/crontab (you may need to create /etc/crontab)
# */15 * * * * root /usr/sbin/group_email.sh >> /private/var/log/group_email.log
# To use
# When the script runs, it searches for any WGM group in /LDAPv3/127.0.0.1 the word "mail"
# within the comment field. Postfix aliases with group members are then created.
#
# If you create a group called 'employees' and put all staff in that group, you will
# then have an email setup of employees@domain.com
#
# Be sure all mail groups contain at least one member
# Would like to add/modify
# 1. needs error trapping. a group without members returns an error.
# 2. support for non-local LDAP
# clear current aliases
echo "" > /etc/postfix/group_aliases
# Get list of groups with 'mail' in the comment field
gr=`dscl /LDAPv3/127.0.0.1 -list /Groups Comment | grep mail | awk '{print $1}'`
for group in $gr
do
echo $group: `dscl /LDAPv3/127.0.0.1 -read /Groups/$group 'GroupMembership' | cut -d : -f 2 | grep -v "No such key"` | awk '{gsub(/[ ]/,",");print}' | sed 's/,//' >>/etc/postfix/group_aliases
done
/usr/sbin/postalias /etc/postfix/group_aliases
/usr/bin/newaliases
sudo postfix reload
exit 0


the modifications will end up adding the commas where they should be as well as reloading the postfix daemon , so in this version it shall be hands free.

Group emails

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.