Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to switch Mail's SMTP servers Apps
Here is an Apple Script that will let you change your SMTP servers depending on your location. As you might have guessed from the examples below I am forced to use my ISP's SMTP server when I am at home.
on run
set myResult to display dialog "Where are you?"¬
buttons {"Home", "Work"}
tell application "Mail"
if the button returned of myResult is "Work" then
set smtp server of account "Work Email" to ¬
smtp server "mail.work.com"
set smtp server of account "Home Email" to¬
smtp server "mail.home.net"
else
set smtp server of account "Work Email" to ¬
smtp server "mail.isp.com"
set smtp server of account "Home Email" to ¬
smtp server "mail.isp.com"
end if
end tell
end run
[Editor's note: I have not tested this myself.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,220 views]  

An AppleScript to switch Mail's SMTP servers | 14 comments | Create New Account
Click here to return to the 'An AppleScript to switch Mail's SMTP servers' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically when you change location
Authored by: mattw on Jan 20, '03 11:15:04AM

I wrote a script to change the SMTP servers automatically when you change location (combo AppleScript, Perl and scutil). I have a number of email accounts and 3 locations and it works well. There's a short howto and explanation here:

http://www.interconnected.org/src/autosmtp/



[ Reply to This | # ]
Getting the Current Network Location
Authored by: lx on Jan 20, '03 01:36:30PM

Thanks for writing up your autosmtp configuration Matt. I don't have this particular issue with the SMTP since my ISP allows me to use theirs from anywhere with authentication, but I was looking for a way to get the current network location setting so a few of my applescripts could do the right thing from the right location. If anyone else is interested, here's an applescript handler culled from Matt's Perl script:

on networklocation()
return (do shell script "scutil <<EOF | sed -n 's/.*UserDefinedName : \\(.*$\\)/\\1/p'
open
show Setup:/
close
EOF")
end networklocation

If somebody knows a better way to do this, then please let me know!



[ Reply to This | # ]
Getting the Current Network Location
Authored by: mattw on Jan 20, '03 04:45:23PM
It's not necessarily an easier way (because it still has to be regexp'd to extract the current location), but this also lets you change location from the command line:
bash-2.05a$ scselect 
usage: scselect [-n] new-set-name

Defined sets include: (* == current set)
   0    (Automatic)
 * 1    (...etc)


[ Reply to This | # ]
Same Idea
Authored by: haironfire on Jan 20, '03 11:35:07AM
I did something like this for my POP server between work and home. However, I added in a step to activate the application, since I don't always remember to have it running before switching.

Is there anyway to link this type of switch to the Location menu? Like, once I change locations, the POP (or SMTP) address is changed automatically?

I read the previous post contributed by mattw but it gave me a headache. A bit too complicated for me.

[ Reply to This | # ]
Same Idea
Authored by: mattw on Jan 20, '03 12:28:33PM

As far as I could figure out, it's possible to have applications run when the location changes... but only if you do it from the command line or maybe have a Cocoa app hook into the SystemConfiguration framework.

Otherwise, I don't think there's an easy way -- but I'd love to be wrong. An AppleScript event, like Folder Actions, would be ideal.



[ Reply to This | # ]
Use a local sendmail.
Authored by: signals on Jan 20, '03 01:14:33PM

I had a similar problem. I can't use the SMTP server at work, when I am at home. It claims that I am trying to relay mail and fails, and I don't have a "home" e-mail server. So... I setup a local "sendmail" and pointed Mail.app at localhost for SMTP. Now, it passes the e-mail to my local sendmail that routes it to each recipient's SMTP server. Now I am not bothered by firewall issues or "I don't relay." messages and I can send mail from anywhere...

-Signals



[ Reply to This | # ]
incoming mail server
Authored by: liedra on Jan 20, '03 05:52:05PM

How would you change this Applescript to switch the incoming mail server?

Thanks,
- liedra



[ Reply to This | # ]
incoming mail server
Authored by: haironfire on Jan 20, '03 07:44:44PM

If you have a POP mail account, just change the references to 'smtp' server in the original post to 'pop' server (without the quotes). Then change the server addresses to suit your situation.

That should do it.



[ Reply to This | # ]
incoming mail server
Authored by: liedra on Jan 21, '03 03:03:38AM

Nope! That didn't work. (Also I use IMAP, not POP, but the same should apply with POP).
To give you a little bit of background, I have a linux imap server running on the internal network at home, and a port forward through a linux proxy server that forwards my IMAP from outside the network. For some unrelated reason, I haven't been able to access the server from the one outside address, so I need something like this.
Here's my script (after hunting through the sample applescripts that work with Mail.app) NOTE: *should* work with POP too. But I don't have POP so I can't test it. YMMV, use at your own risk :-)

on run
set myResult to display dialog "Where are you?" buttons {"Home", "Outside"}
tell application "Mail"
if the button returned of myResult is "Outside" then
set server name of account "Default" to "outside.domain"
else
set server name of account "Default" to "internal.server"
end if
end tell
end run

(bah, tried to add in whitespace there but it didn't really work. it's basically the same formatting as the one on the original comment, just change the "set server" lines (and I cut out a bit of the other unnecessary stuff)).



[ Reply to This | # ]
incoming mail server
Authored by: haironfire on Jan 21, '03 07:38:20PM

My previous comments pertained to using the script for Entourage. Apparently, Mail uses a different set of naming conventions. Open Script Editor, the open the dictionary for Mail to be sure.



[ Reply to This | # ]
incoming mail server
Authored by: shreddiez on Jan 21, '03 03:51:07PM
Based on mattw's smtp server change, here's my script for pop server change, which also changes the port. The main differnce is that the applescript parses the settings to spilt up the server and port, then also sets the port using "set server port", but not "set port" as the Mail dictionary would have you believe :/
#!/usr/bin/perl -w use strict; # The Network Location name to POP server hostname mapping: my $pop_for_location = { Home" => "popserver.domain.com:110", "Uni" => "localhost:1110" }; # These are the account names to change my $accounts_to_change = [ "Default" ]; my $location = &get_location_from_scutil() || ""; unless(exists $pop_for_location->{$location}) { die "The location is unknown"; } my $le = "r"; open(APPLESCRIPT, "| osascript"); while(my $line = ) { chomp $line; if($line =~ m/property/) { if($line =~ m/%popserver%/) { my $newserver = '"' . $pop_for_location->{$location} . '"'; $line =~ s/%popserver%/$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 /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 newPOPAddress : %popserver% property accountsToChange : {%accounts%} set theDelimiter to AppleScript's text item delimiters set AppleScript's text item delimiters to ":" set the item_list to every text item of newPOPAddress set newPOPServer to item 1 of item_list set newPOPPort to item 2 of item_list as integer set AppleScript's text item delimiters to theDelimiter 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 (CurrentServer as string is equal to newServer as string) then set theResult to true end if return theResult end theAccountAlreadyHasThisServer 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 currentPOPServer to server name of eachAccount if (my theAccountAlreadyHasThisServer(currentPOPServer, newPOPServer)) then -- do nothing else set server name of eachAccount to newPOPServer set server port of eachAccount to newPOPPort --display dialog "change " & name of eachAccount & " to " & name of theSMTPServer set anAccountWasUpdated to name of eachAccount end if end if end repeat end tell --end if return anAccountWasUpdated


[ Reply to This | # ]
Why not just set the search domain?
Authored by: etwoy on Jan 20, '03 06:37:01PM

Maybe I'm missing something here, but this is what I use the search domain for.

I just set my smtp server to be "mail", and when I'm at home, I have my search domain set to "home.domain.com", and at when I'm at work I set my search domain to "work.domain.com".

Thus when I'm at home, the smtp server gets set as "mail.home.domain.com" and when I'm at work it's "mail.work.domain.com".

Most places have 'mail.domain" as their smtp, so this works pretty much everywhere so long as you set the search domain right.


(search domain is in Network Prefs)



[ Reply to This | # ]
An AppleScript to switch Mail's SMTP servers
Authored by: jdborchert on May 17, '05 04:51:56PM
I wrote a similiar script to deal with my situation. I am a graduate student at the University of Utah. When on campus I must send email without SMTP authentication. When off campus I must use authentication. I set up two different SMTP servers in mail, one with and one without authentication. This script checks my IP address and if I am on the campus network it sets the SMTP server to the one without authentication and if I'm not on the campus network it sets the SMTP server to the one with authentication. I then saved it as an application and put it in my login items so I don't ever have to hassle with changing the server to send mail again!

set check_ip to do shell script "ipconfig getifaddr en0" as string
if check_ip starts with "155.100" then
	tell application "Mail" to set smtp server of account "Account Name" to smtp server "Server Name (without auth)"
else
	tell application "Mail" to set smtp server of account "Account Name" to smtp server "Server Name (with auth)"
end if


[ Reply to This | # ]
An AppleScript to switch Mail's SMTP servers
Authored by: soj on Apr 19, '06 07:54:52PM
(n00b alert triggered)

Hi everyone,

I thought I'd be a smart-arse and modify liedra's code to change both the incoming and outgoing servers at once:


on run
	set myResult to display dialog "Choose profile to apply:" buttons {"Work", "Dial-up"}
	tell application "Mail"
		if the button returned of myResult is "Work" then
			set server name of account "myaccount" to "IP1.xx.xxx.xxx"
			set smtp server of account "myaccount" to smtp server "IP1.xx.xxx.xxx"
		else
			set server name of account "myaccount" to "mail.isp.com.au"
			set smtp server of account "myaccount" to smtp server "mail.isp.com.au"
		end if
	end tell
end run

... which seemed to work fine except for the strange problem that when I quit out of Mail after using the script to change server settings and restart the program, all my mail in the Inbox and Sent Mail folders will have vanished. Thankfully, they reappear after executing Mailbox>Rebuild from the drop-down menu.

The problem only occurs when the script is used to change servers from what they were before the script is run.
What doesn't cause the problem:

- changing servers manually (using the same server names)
- using the script but not changing the servers i.e. ‘re-applying' the existing settings, e.g. selecting ‘Work' when the Work settings are already in effect
- using the script to change servers to the alternate configuration, then using the script to change it back to the original configuration without quitting Mail, e.g. from ‘Work' to ‘Dial-Up' and then back to ‘Work' again. After doing this, when Mail has been quitted and restarted again, things are fine.

Any ideas?

[ Reply to This | # ]