Sending mail while user login and logout in MAC OS

Making postfix mail server start while MAC system startup

and Sending mail while user login and logout in MAC OS
=========================================================

vi /System/Library/LaunchDaemons/org.postfix.master.plist

Edit the file as bellow
————————
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>org.postfix.master</string>
<key>KeepAlive</key>
<true/>
<key>Program</key>
<string>/usr/libexec/postfix/master</string>
<key>ProgramArguments</key>
<array>
<string>master</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/var/spool/postfix/maildrop</string>
</array>
<key>AbandonProcessGroup</key>
<true/>
</dict>
</plist>

Set load the file when MAC system starts up
——————————————-
launchctl load /System/Library/LanuchDaemons/org.postfix.master.plist

Create autodaily directory
————————–
mkdir /usr/libexec/autodaily

Create the log file
———————
vi /usr/libexec/autodaily/log.log
chmod 0777 log.log

Create autodailyLogin.sh and autodailyLogout.sh file
—————————————————-
cd /usr/libexec/autodaily/
vi autodailyLogin.sh

Enter the followings in the file
——————————–
#!/bin/bash

# Created by Saiful Takeshi
# Environment variables #################################################

DEST_EMAIL=”saiful_takeshi@number-1.co.jp,hanamitsu_makoto@number-1.co.jp”

USER_NAME=”CHUMAN SHOUHEI”

TODAY=$(date +%Y/%m/%d)

EVENT_TIME=$(date “+DATE: %Y-%m-%d%nTIME: %H:%M:%S”)

LOGGEDIN_SUBJECT=”[MAC Login] : $TODAY : $USER_NAME”

LOGGEDIN_BODY=”$USER_NAME  has Logged In successfully at \n$EVENT_TIME.”

# Functions ##########

function loggedin(){

echo “$LOGGEDIN_BODY” | mail -s “$LOGGEDIN_SUBJECT” $DEST_EMAIL

return 0

}

loggedin;

echo “Login at $EVENT_TIME” >> log.log

Create autodailyLogin.sh and autodailyLogout.sh file
—————————————————-
vi /usr/libexec/autodaily/autodailyLogout.sh

Enter the followings in this file
————————————-

#!/bin/bash

# Created by Saiful Takeshi
# Environment variables #################################################

DEST_EMAIL=”saiful_takeshi@number-1.co.jp,hanamitsu_makoto@number-1.co.jp”

USER_NAME=”CHUMAN SHOUHEI”

TODAY=$(date +%Y/%m/%d)

#EVENT_TIME=$(date +%Y/%m/%d-%H:%M:%S)

EVENT_TIME=$(date “+DATE: %Y-%m-%d%nTIME: %H:%M:%S”)

LOGGEDOUT_SUBJECT=”[MAC Logout] : $TODAY : $USER_NAME”

LOGGEDOUT_BODY=”$USER_NAME san has Logged Out successfully at \n$EVENT_TIME.”

# Functions ##########

function loggedout(){

echo “$LOGGEDOUT_BODY” | mail -s “$LOGGEDOUT_SUBJECT” $DEST_EMAIL

return 0

}

loggedout;

echo “logout at $EVENT_TIME” >> log.log

Make both file as executable
———————————
chmod +x autodailyLogin.sh
chmod +x autodailyLogout.sh

Checking the script is ok or not(execute)
—————————————–
sh autodailyLogin.sh
sh autodailyLogout.sh

—————————–
Show the default setting for login and logout
———————————————
defaults read

Add the login hook event as execute the autodailyLogin.sh file when login
——————————————————————–
sudo defaults write com.apple.loginwindow LoginHook /usr/libexec/autodaily/autodailyLogin.sh

Add the logout hook event as execute the autodailyLogout.sh file when logout
——————————————————————–
sudo defaults write com.apple.loginwindow LogoutHook /usr/libexec/autodaily/autodailyLogout.sh

Now check the mac machine by login and logout

 

========================================

Summary command

========================================

#launch FTP
sudo launchctl load -w /System/Library/LaunchDaemons/ftp.plist

#copy postfix file
sudo cp /System/Library/LaunchDaemons/org.postfix.master.plist /System/Library/LaunchDaemons/org.postfix.master.plist_bk

#Edit postfix plist file
sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist

#unload load postfix
sudo launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist
sudo launchctl load -w /System/Library/LaunchDaemons/org.postfix.master.plist

 

#autodailylogin.sh and autodailylogout.sh file

cd /usr/libexec
sudo mkdir autodaily
sudo chmod 0777 autodaily/
cd autodaily/
sudo chmod 0777 log.log

#edit files

sudo vi autodailylogin.sh

sudo vi autodailylogout.sh

#permission change to execute

sudo chmod +x autodailylogin.sh

sudo chmod +x autodailylogout.sh

Add the login hook event as execute the autodailyLogin.sh file when login
——————————————————————–
sudo defaults write com.apple.loginwindow LoginHook /usr/libexec/autodaily/autodailyLogin.sh

Add the logout hook event as execute the autodailyLogout.sh file when logout
——————————————————————–
sudo defaults write com.apple.loginwindow LogoutHook /usr/libexec/autodaily/autodailyLogout.sh