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