Can't create mail account using AppleScript

Hi


I am trying to create new mail account (POP) using Apple script "Create New Mail Account.scpt" which is in "/Library/Scripts/Mail Scripts".


by passing commands like this.


script as follows:


tell application "Mail"


make new pop account with properties {name:"mymail" username:"myun" server name :"pop.xxx.xxx" password: "mypwd" full name : "myname" email addresses"{"myemail.gg.com"}}



Somehow it is not creating account, and getting back response as "Account Creation failed".


Can anyone advice how to resolve this.


Thanks in advance.

iMac, OS X Mountain Lion (10.8.2)

Posted on Mar 28, 2013 6:07 PM

Reply
16 replies

Mar 29, 2013 8:20 AM in response to saran80

Try the following:


tell application "Mail" set theNewAccount to make new pop account with properties {name:theAccountName, user name:theUsername & "@gmail.com", server name:"pop.gmail.com", password:thePassword, full name:theFullName, email addresses:{theUsername & "@gmail.com"}, port:995, uses ssl:true} set theSMTPServer to make new smtp server with properties {server name:smtpServerName, user name:theUsername & "@gmail.com"} tell theSMTPServer set port to 587 set uses ssl to true set authentication to password end tell set smtp server of account theAccountName to smtp server (smtpServerName & ":" & theUsername & "@gmail.com") end tell

Apr 1, 2013 9:54 PM in response to Neville Hillyer

When i run the applescript, Create New Mail Account (Library/Scripts/Mail Scripts)..


I m getting --> error number -1708


This is occuring only when i try to configure email account, when there is no other account configured in Apple Mail.

Can anyone let me know the solution for this issue.


=======

tell application "Mail"


make new pop account with properties {name:"john", user name:"john@example.com", server name:"pop.gmail.com", password:"test123", full name:"john", email addresses:{"john@example.com"}}


--> error number -1708

======

Apr 2, 2013 6:05 AM in response to saran80

two things...


First, sometimes AppleScript gets annoyed when you ask it to do too much at once. It can help to break the pocess down into separate steps like so:


tell application "Mail"


set theNewAccount to makenewpop accountwith properties {name:theAccountName, user name:theUsername, server name:"pop.gmail.com"}

tell theNewAccount

set password to thePassword

set full name to theFullName

set email addresses to {theUsername & "@gmail.com"}

set port to 995

set uses ssl to true

end tell


end tell



Second, for Mail.app deleted accounts are sometimes not quite as deleted as you think. They can leave ghosts of themselves in the mail folder and various plist files, and those ghosts will cause an error if you try to overwrite them, just like a real account would. try adding in the following line before the make command:


if existsaccounttheAccountName then deleteaccounttheAccountName

Apr 3, 2013 8:36 AM in response to saran80

The only thing I can think of is that if you run Mail.app without any accounts configured it launches an account setup dialog. That would certainly interfere with any scripting. I'm hard pressed to think why you would want to run it on an accountless user. Is there a reason for that, or are you creating a testing error that you'll never run into in the real world?

Apr 4, 2013 9:06 AM in response to saran80

I can start Mail without its startup wizard on Leopard by removing ~/Library/Mail (or its contents) and pasting the following text into ~/Library/Preferences/com.apple.mail.plist


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>MailAccounts</key>

<array>

<dict>

<key>AccountName</key>

<string>none</string>

<key>AccountType</key>

<string>POPAccount</string>

</dict>

</array>

</dict>

</plist>


I don't know if this works on later OSs but it could be a starting point for those who wish to experiment.

Apr 10, 2013 8:49 AM in response to Neville Hillyer

It took me a while and the result is more complex than I would have liked but here is a script which appears to work reliably on Leopard. It installs a temporary account if Mail has no existing accounts.


I was determined not to put passwords in the script and this may have added slightly to the complexity as most modern mail clients prefer to check account viability by logging on during setup.


I would be grateful for feedback about performance on other OSs and with IMAP - please backup your email and settings before using this script for the first time.


I have tested the script posted here by copying everything between 'code' to Script Editor and running it.


{code}

-- Script to install a new POP (or IMAP) account on Mail

-- This script has only been tested for POP on Leopard

-- Old Mail folders will be renamed if no account is detected

-- Setting delete mail to false is the safest way to start

-- It is more secure to insert passwords when Mail is first used

-- Password for test account is: dumpty


-- Mail plist file location:

set plistFile to "~/Library/Preferences/com.apple.mail.plist"

-- For Mountain Lion change the above to:

-- ~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.Mail.pli st


-- Account details:

setaccountNameto"lavabit"

setaccountUserto"humpty"

set accountServer to "lavabit.com"

setaccountPortto995-- do not quote

setaccountSSLtotrue-- do not quote

setaccountTypeto"pop"

set fullName to "Humpty Dumpty"

set emailAddress to "humpty@lavabit.com"

setdeleteMailtofalse-- do not quote


-- Optional SMTP account details - not always easy to add later:

set smtpUser to "humpty"

set smtpServer to "lavabit.com"

setsmtpPortto465-- do not quote

setsmtpSSLto"YES"

setuseAuthenticationto"YES"

setsmtpAuthto"PASSWORD"-- do not change this to your password


tellapplication"Mail"


ifnot(firstaccountexists)then


try

do shell script "killall Mail; p=" & plistFile & ";

f(){ mv $1 $1-`date +%Y%m%d%H%M%S` || :; }; f ~/Library/Mail; f $p; printf \"

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>

<plist version='1.0'>

<dict>

<key>MailAccounts</key>

<array>

<dict>

<key>AccountName</key>

<string>delete me</string>

<key>AccountType</key>

<string>POPAccount</string>

</dict>

</array>

</dict>

</plist>

\" >> $p"


endtry


endif


setstopScripttofalse


ifaccountaccountNameexiststhen

display dialog "Account '" & accountName & "' already exists. Either backup its emails and delete it or change one of the names." buttons {"Stop script"} with icon stop


else


repeatwithaccountNumberfrom1tocountofaccounts


if(user nameofaccountaccountNumber=accountUserandserver nameofaccountaccountNumber=accountServer)then

display dialog "Your existing '" & name of account accountNumber & "' account already has user name '" & accountUser & "' and server name '" & accountServer & "'. Either backup this account's emails and delete it or change one of the above names." buttons {"Stop script"} with icon stop


setstopScripttotrue


return


endif


endrepeat


ifstopScriptisfalsethen


ifaccountType="pop"then


setalphatomakenewpop accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress,delete mail on server:deleteMail}

else if accountType = "imap" then


setalphatomakenewimap accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress}


else


setstopScripttotrue

display dialog accountType & " Ensure that 'accountType' is set to \"pop\" or \"imap\"." buttons {"Stop script"} with icon stop


endif


ifstopScriptisfalsethen

if account "delete me" exists then delete account "delete me"

try

do shell script "killall Mail"


endtry


endif


endif


endif

endtell

ifstopScriptisfalsethen


setsmtpDetailsto{|AccountName|:accountName,|AccountType|:"SMTPAccount",|Hostname|:smtpServer,|SSLEnabled|:smtpSSL,|ShouldUseAuthentication|:useAuthentication,|Username|:smtpUser,|AuthenticationScheme|:smtpAuth,|PortNumber|:smtpPort}

tell application "System Events"


tellproperty list fileplistFile

tell property list item "DeliveryAccounts"


makenewproperty list itematbeginningwith properties{kind:record,value:smtpDetails}


endtell


endtell


endtell


tellapplication"Mail"


setsmtp serverofalphatosmtp serveraccountName


endtell

endif


{code}

Apr 10, 2013 9:13 AM in response to Neville Hillyer

oops - silly bug - update here:


{code}

-- Script to install a new POP (or IMAP) account on Mail

-- This script has only been tested for POP on Leopard

-- Old Mail folders will be renamed if no account is detected

-- Setting delete mail to false is the safest way to start

-- It is more secure to insert passwords when Mail is first used

-- Password for test account is: dumpty


-- Mail plist file location:

set plistFile to "~/Library/Preferences/com.apple.mail.plist"

-- For Mountain Lion change the above to:

-- ~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.Mail.pli st


-- Account details:

setaccountNameto"lavabit"

setaccountUserto"humpty"

set accountServer to "lavabit.com"

setaccountPortto995-- do not quote

setaccountSSLtotrue-- do not quote

setaccountTypeto"pop"

set fullName to "Humpty Dumpty"

set emailAddress to "humpty@lavabit.com"

setdeleteMailtofalse-- do not quote


-- Optional SMTP account details - not always easy to add later:

set smtpUser to "humpty"

set smtpServer to "lavabit.com"

setsmtpPortto465-- do not quote

setsmtpSSLto"YES"

setuseAuthenticationto"YES"

setsmtpAuthto"PASSWORD"-- do not change this to your password


tellapplication"Mail"


ifnot(firstaccountexists)then


try

do shell script "killall Mail; p=" & plistFile & ";

f(){ mv $1 $1-`date +%Y%m%d%H%M%S` || :; }; f ~/Library/Mail; f $p; printf \"

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE plist PUBLIC '-//Apple//DTD PLIST 1.0//EN' 'http://www.apple.com/DTDs/PropertyList-1.0.dtd'>

<plist version='1.0'>

<dict>

<key>MailAccounts</key>

<array>

<dict>

<key>AccountName</key>

<string>delete me</string>

<key>AccountType</key>

<string>POPAccount</string>

</dict>

</array>

</dict>

</plist>

\" >> $p"


endtry


endif


setstopScripttofalse


ifaccountaccountNameexiststhen

display dialog "Account '" & accountName & "' already exists. Either backup its emails and delete it or change one of the names." buttons {"Stop script"} with icon stop


setstopScripttotrue


else


repeatwithaccountNumberfrom1tocountofaccounts


if(user nameofaccountaccountNumber=accountUserandserver nameofaccountaccountNumber=accountServer)then

display dialog "Your existing '" & name of account accountNumber & "' account already has user name '" & accountUser & "' and server name '" & accountServer & "'. Either backup this account's emails and delete it or change one of the above names." buttons {"Stop script"} with icon stop


setstopScripttotrue


return


endif


endrepeat


ifstopScriptisfalsethen


ifaccountType="pop"then


setalphatomakenewpop accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress,delete mail on server:deleteMail}

else if accountType = "imap" then


setalphatomakenewimap accountwith properties{name:accountName,user name:accountUser,server name:accountServer,port:accountPort,uses ssl:accountSSL,full name:fullName,email addresses:emailAddress}


else


setstopScripttotrue

display dialog accountType & " Ensure that 'accountType' is set to \"pop\" or \"imap\"." buttons {"Stop script"} with icon stop


endif


ifstopScriptisfalsethen

if account "delete me" exists then delete account "delete me"

try

do shell script "killall Mail"


endtry


endif


endif


endif

endtell

ifstopScriptisfalsethen


setsmtpDetailsto{|AccountName|:accountName,|AccountType|:"SMTPAccount",|Hostname|:smtpServer,|SSLEnabled|:smtpSSL,|ShouldUseAuthentication|:useAuthentication,|Username|:smtpUser,|AuthenticationScheme|:smtpAuth,|PortNumber|:smtpPort}

tell application "System Events"


tellproperty list fileplistFile

tell property list item "DeliveryAccounts"


makenewproperty list itematbeginningwith properties{kind:record,value:smtpDetails}


endtell


endtell


endtell


tellapplication"Mail"


setsmtp serverofalphatosmtp serveraccountName


endtell

endif


{code}

Apr 10, 2013 9:35 AM in response to Neville Hillyer

These 'code tags are not perfect - I can see a space in the middle of 'plist' at the end of the Mountain Lion comment - it is not in my original or the very complex HTML source at the time of posting. As far as I can tell it is only this one comment which is affected.


As a result of this quirk I have now put a copy here:


http://links.zero.eu.org/os-x/mail/mail.script.zip

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't create mail account using AppleScript

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