#!/usr/bin/perl -w # changeSMTPServers.pl # 26aug2002 # Matt Webb matt@interconnected.org # # Change SMTP servers for Mail.app on Mac OS X using a gruesome combination of # notification from configd, grep'ing the output of scutil, osascript, and an # Applescript. Keep in mind that today is the first time I have wielded any of # these things, and all I can really say is "Well, it works for me". use strict; # The Network Location name to SMTP server hostname mapping: my $smtp_for_location = { LocationOne => "smtp.somewhere.eg", LocationTwo => "smtp.somewhereelse.eg" }; # These are the account names to change my $accounts_to_change = [ "MyEmailAccount" ]; my $location = &get_location_from_scutil() || ""; unless(exists $smtp_for_location->{$location}) { die "The location <$location> is unknown"; } my $le = "\r"; open(APPLESCRIPT, "| osascript"); while(my $line = ) { chomp $line; if($line =~ m/property/) { if($line =~ m/%smtpserver%/) { my $newserver = '"' . $smtp_for_location->{$location} . '"'; $line =~ s/%smtpserver%/$newserver/; } elsif($line =~ m/%accounts%/) { my $account_string = '"'. join('","', @{$accounts_to_change}) .'"'; $line =~ s/%accounts%/$account_string/; } } print APPLESCRIPT $line, $le; } close(APPLESCRIPT); sub get_location_from_scutil { my @scutil = `scutil <<- end_scutil 2> /dev/null open show Setup:/ close end_scutil`; my @matches = map { m/UserDefinedName : (.*)/ } @scutil; if(@matches == 1) { return $matches[0]; } else { return undef; } } __DATA__ property newSMTPServer : %smtpserver% property accountsToChange : {%accounts%} on isThisTheSMTPServerWeAreLookingFor(theServer) set theResult to false -- set theServerName to name of theServer -- display dialog theServerName if (name of theServer is equal to newSMTPServer as string) then set theResult to true end if return theResult end isThisTheSMTPServerWeAreLookingFor on weWantToChangeThisAccount(theAccount) set theResult to false --set theAccountName to name of theAccount --display dialog "this is " & theAccountName repeat with eachAccount in accountsToChange if (name of theAccount is equal to eachAccount as string) then set theResult to true end if end repeat return theResult end weWantToChangeThisAccount on theAccountAlreadyHasThisServer(currentServer, newServer) set theResult to false if (name of currentServer as string is equal to name of newServer as string) then set theResult to true end if return theResult end theAccountAlreadyHasThisServer tell application "Mail" to set everySMTPServer to every smtp server set theSMTPServer to false repeat with eachSMTPServer in everySMTPServer set nameOfEachSMTPServer to name of eachSMTPServer --display dialog nameOfEachSMTPServer if (my isThisTheSMTPServerWeAreLookingFor(eachSMTPServer)) then set theSMTPServer to eachSMTPServer end if end repeat set anAccountWasUpdated to "no" if (theSMTPServer is not equal to false) then tell application "Mail" set everyAccount to every account repeat with eachAccount in everyAccount if (my weWantToChangeThisAccount(eachAccount)) then set currentSMTPServer to smtp server of eachAccount if (my theAccountAlreadyHasThisServer(currentSMTPServer, theSMTPServer)) then -- do nothing else set smtp server of eachAccount to theSMTPServer --display dialog "change " & name of eachAccount & " to " & name of theSMTPServer set anAccountWasUpdated to name of eachAccount & " given " & name of theSMTPServer end if end if end repeat end tell end if return anAccountWasUpdated