creating a mailbox via imap

Paul Kudla (SCOM.CA Internet Services Inc.) paul at scom.ca
Thu Feb 23 20:41:53 UTC 2023


Ok basically (please read the entire post - its techy),

You need the username/password in the database before doing this

Then make sure dovecot config carries

_______________________________________________________________

namespace inbox {
   inbox = yes
   location =
   mailbox Drafts {
     auto = subscribe
     special_use = \Drafts
   }
   mailbox Sent {
     auto = subscribe
     special_use = \Sent
   }
   mailbox Trash {
     auto = subscribe
     special_use = \Trash
   }
   prefix =
   separator = /
}
_______________________________________________________________

note auto = subscribe above.

When you use (in python as per this example) the imap library calls to 
create a mailbox dovecot (like cyrus) should create the account etc

In my case I do a

CM (create mailbox) for

/INBOX
/Sent
/Trash
/Drafts

I make all of them by default, some mail clients will make these my 
default and some do not.

It's overkill but should work.

Also note this needs to be carried out on the actual mail server to
get around os system rights etc.

Please note i run a django for my admin system and ended up writing a 
"Listener" to communicate with the server over tcpip

the listener does use the create mailbox function with dovecot

I dont remember the specifics but feel free to ask if the code below has 
issues.

Listener sits in the background on a dovecot server waiting to do something.


[15:36:24] mail18.scom.ca [root:0] ~
# psx list
Displaying One Conditional ... list


  6005  -  Is       0:00.19 /usr/local/bin/python2 
/sbin/scripts/dovecot.listen (python2.7)

run in unix with the & (background command)

dovecot.listen as follows :

_______________________________________________________________________
# cat /sbin/scripts/dovecot.listen
#!/usr/local/bin/python2

import os,sys
import socket
import commands
import time

from lib import *

a = onlyone ('dovecot.listen')
if a.status == 'BAD' :
         print 'Another Process Is running ....'
         sys.exit()

TCP_IP = '10.220.0.18'
TCP_PORT = 8444
BUFFER_SIZE = 1024  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

while 1 : #Process received data?
         conn, addr = s.accept() #Wait for data
         print 'Connection address:', addr
         data = conn.recv(BUFFER_SIZE)
         if not data: break
         print "received data:", data
         data = data.split (' ')
         command = data[0]
         print command

         #If CM (Create Mailbox)
         if command == 'CM' :
                 username = data[1]
                 print 'Creating Email Account : %s' % (username)
                 #Now create the mail box
                 #Now go make the email account
                 message = ''
                 for n in range (0,100) :
                         command1 = 
commands.getoutput('/usr/local/bin/doveadm mailbox create -s -u %s 
INBOX' %str(username))
                         print 'Command1 : %s' %command1
                         if 'Mailbox already exists' in command1 :
                                 message = 'BAD'
                                 conn.send( str(message) )
                                 break

                         else :
                                 if message == '' :
                                         message = 'OK'
                                         conn.send(message)
                                         message = 'SENT'

                         if "User doesn't exist" in command1 :
                                 time.sleep(2)
                                 continue
                         else :
                                 print 'Command1 : %s' %command1
                                 message = 'SENT'
                                 break

                 if message == 'SENT' : #Create the rest
                         command2 = 
commands.getoutput('/usr/local/bin/doveadm mailbox create -s -u %s Sent' 
%str(username))
                         print 'Command2 : %s' %command2
                         command3 = 
commands.getoutput('/usr/local/bin/doveadm mailbox create -s -u %s 
Trash' %str(username))
                         print 'Command3 : %s' %command3
                         command4 = 
commands.getoutput('/usr/local/bin/doveadm mailbox create -s -u %s 
Drafts' %str(username))
                         print 'Command4 : %s' %command4




         if command == 'INFO' :
                 username = data[1]
                 print 'Getting Email Account Info : %s' % ( username )
                 command1 = commands.getoutput("/usr/local/bin/doveadm 
mailbox status -t all -u %s '*' " %str(username))
                 if 'Error' in command1 :
                         message = 'BAD'
                 else :
                         message = 'OK : ' + command1

                 print message
                 conn.send( str(message) )  # echo


         if command == 'DM' :
                 data = data[1]
                 data = data.split('@')
                 print 'Deleting Email Account : user/%s@%s' % ( 
str(data[0]), str(data[1]) )

                 message = 'BAD'

                 conn.send(message)  # echo
                 print message



conn.close()
s.close()




#Go Back Around
_____________________________________________________________________
the code is not finished but does create the mbox and waits for it to be 
completed before returning ?

it is accessed with this code (see python sockets)

_____________________________________________________________________

       imap_test = Dovecot_Command ('INFO',self.username) #do i have 
this account ?

       if 'BAD' in imap_test.answer :
         try : #Try to Create the account, note that the db must be 
updated properly before it will work
           imap_create = Dovecot_Command ('CM',self.username)
           if 'OK' in imap_create.answer :
             send_subject = 'Email Account Created : %s' 
%(str(self.username) )

         except :
           send_subject = 'Error Account : %s' %(str(self.username) )
           pass

       else :
         send_subject = 'Email Account Updated : %s' %(self.username)

_______________________________________________________________________

and

_______________________________________________________________________
class Dovecot_Command :
   def __init__(self,command,username) :
     self.command = command
     self.username = username
     self.answer = ''
     import socket
     TCP_IP = '10.220.0.18'
     TCP_PORT = 8444
     BUFFER_SIZE = 1024
     MESSAGE = '%s %s' %(self.command,self.username)
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect((TCP_IP, TCP_PORT))
     s.send(MESSAGE)
     self.answer = s.recv(BUFFER_SIZE)
     s.close()

________________________________________________________________________

this is crude code but does get the job done.

I went to this extent to eventually create, get info on the account, 
delete etc back into my django admin project (like mbox size, last 
accessed etc)

basically everything you need to handle accounts on the serer side.

I also run replication and i think that is what lead to this being a 
little more complex. (ie a simple cm imap command was insufficent?)




Happy Thursday !!!
Thanks - paul

Paul Kudla


Scom.ca Internet Services <http://www.scom.ca>
004-1009 Byron Street South
Whitby, Ontario - Canada
L1N 4S3

Toronto 416.642.7266
Main 1.866.411.7266
Fax 1.888.892.7266
Email paul at scom.ca

On 2023-02-23 12:56 p.m., dovecot-bounces at dovecot.org wrote:
> 
>> is there any way with Dovecot to open an admin imap connection and 
>> create a brand new mailbox?
>> With Cyrus imapd I can do this by connecting as the Cyrus admin user 
>> and then create a folder "user/newuser at domain.tld".
> 
> 
> Wouldn't that be dependent on how Dovecot auth worker verifies a user 
> exist and is valid? Such as for one method, database queries. How would 
> Dovecot know what query to run to add another user to your database? 
> Plus update any other related DB tables needed for your custom setup? I 
> would imagine too many edge cases for Dovecot to worry about for 
> creating accounts.
> 


More information about the dovecot mailing list