Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

AppleScript to configure an IMAP/SMTP mail account

Hi,


I'm trying to use AppleScript to set up a mail account using an IMAP/SMTP server setup. Seems like it is extremely difficult to get right. The script I've got so far works so and so:

tell application "System Events"
  set theUserName to name of current user
end tell

tell application "Mail"
  set theAccountName to "AppleScript" & theUserName
  set thePassword to “empty”
  set theFullName to “empty”
  set smtpServerName to “smtp.example.com”

  set theSMTPServer to make new smtp server with properties {server name:smtpServerName, user name:theUserName & “@example.com", port:25, uses ssl:true, authentication:kerberos 5}
  set theNewAccount to make new imap account with properties {name:theAccountName, user name:theUserName, server name:"imapuser.example.com", password:thePassword, full name:theFullName, email addresses:{theUserName & "@example.com"}, port:143, uses ssl:true, enabled:true, authentication:kerberos 5, store junk mail on server:false, empty sent messages frequency:-1, empty junk messages on quit:false, empty trash frequency:30, store deleted messages on server:true, empty sent messages on quit:false, smtp server:smtp server “example” of application "Mail", empty trash on quit:false, compact mailboxes when closing:true, store sent messages on server:true, move deleted messages to trash:true, include when getting new mail:true, class:imap account, empty junk messages frequency:-1, store drafts on server:false, message caching:all messages and their attachments} 
  set smtp server of account theAccountName to smtp server smtpServerName
end tell


The reason(s) why I say it works so and so:

  • I don't see any account named "AppleScript" & theUserName appear in internet account. Maybe I'm telling stuff to the wrong app (Mail instead of System Prefs?)
  • It only works if there is already an existing account configured.
  • If there is no (mail) account configured and I run this script, the Mail.app icon jumps in the dock and I get a wizard to setup a new account.
  • If I have an existing account and I run the script, the account I configure appears correctly in Mail > Preferences > Accounts (not in Internet Accounts) but there are a few problems:
    • It does not seem to create/catch the correct SMTP server. I can create one but the "description" is empty and it seems to be a copy of the already existing SMTP server.
    • The account seems to be "volatile". After some time it just disappears. I guess because there is a discrepancy between the Mail app accounts and the Internet accounts?
    • Sometimes I even get a full crash of my mac (kernel panic?) when I mess with accounts like this.


I think the key to solving my problem is setting the account up in Internet Accounts and create an SMTP server before I do that.


Any help is greatly appreciated!


Thanks

end tell

MacBook Pro (Retina, 13-inch,Early 2015), macOS Sierra (10.12), null

Posted on Jul 26, 2017 3:46 AM

Reply
1 reply

Jul 26, 2017 5:47 AM in response to buco89

Update, I've got the script to be a bit better. Now I can create a working account with the script. Only thing that is still wrong:

* Still need at least one mail account to pre-exist before the script runs with expected result.

* I still don't see the account appear in "Internet Accounts" while I do see the pre existing account.

* When I quit the Mail.app, the account I just created vanishes, the pre-existing account is still there.

* My full name is not filled in in the preferences.


Here's the script that does work better than the one in the opening post:


tell application "System Events"
  set theUserName to name of current user
end tell

tell application "Mail"
  set theAccountName to “example.com mail " & theUserName

  set thePassword to the text returned of (display dialog "Please enter your password:" with title "Password" with icon caution default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 295 with hidden answer)
  set theFullName to “my name“
  set smtpServerName to "smtp.example.com"

  set smtpusername to “myusername”
  set addsmtp to make new smtp server with properties {server name:smtpServerName}
  tell addsmtp
  set authentication to kerberos 5
  set password to thePassword
  set uses ssl to true
  set port to 25
  set user name to theUserName
  end tell
  set theNewAccount to make new imap account with properties {name:theAccountName, user name:theUserName, server name:"imap.example.com", password:thePassword, full name:theFullName, email addresses:{theUserName & "@example.com"}, port:143, uses ssl:true, enabled:true, authentication:kerberos 5, store junk mail on server:false, empty sent messages frequency:-1, empty junk messages on quit:true, empty trash frequency:30, store deleted messages on server:true, empty sent messages on quit:true, smtp server:smtp server "smtp.example.com" of application "Mail", empty trash on quit:false, compact mailboxes when closing:true, store sent messages on server:true, move deleted messages to trash:true, include when getting new mail:true, class:imap account, empty junk messages frequency:30, store drafts on server:false, message caching:all messages and their attachments} 
  set smtp server of account theAccountName to smtp server "smtp.example.com"
end tell

AppleScript to configure an IMAP/SMTP mail account

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