# I need help with .dovecot mail_location



## hockey97 (Feb 18, 2012)

I need someone to explain to me how to set up this variable: mail_location.


----------



## ecazamir (Feb 18, 2012)

A default postfix + mailbox configuration will store mail in mbox format in files located in /var/mail, in this case 
	
	



```
mail_location = [color="Magenta"]mbox[/color]:/var/mail/%u
```
If the mail is stored in each user's home directory in Maildir format (qmail specific, but Postfix can store in Maildirs too), then 
	
	



```
mail_location = [color="Magenta"]maildir:~[/color]/Maildir
```

Using postfix with dovecot and virtual users, usually requires a root directory for domains and mailboxes and is often used with Maildirs. The passdb and/or userdb backend must supply the home directory for a specified account. I use the following:

```
# store mail in each user's (an user is actually an e-mail account) home directory, maildir format, 
# no ~/Maildir sub-directory is used because this home directory is storing only email.
mail_location = [color="Magenta"]maildir:~[/color]/
```
The "~" above refers to the home directory of the e-mail user (account), it may expand to "/mail/example.domain.net/example.user/" if the logged-in user is [email='example.user@example.domain.net]'example.user@example.domain.net[/email]', the full expanded path depends on how is configured the passdb and/or userdb backend.

Both user_query and password_query return the home directory, below is an example working fine for Postfix + Dovecot + Virtual users + Postfixadmin:

```
connect = host=127.0.0.1 port=3306 user=postfix password=s0m3p4ssWOOrd dbname=postfix
driver = mysql
default_pass_scheme = MD5-CRYPT
password_query = SELECT password,[color="Magenta"]CONCAT('/mail/', maildir) AS userdb_home[/color], '89' AS userdb_uid, '89' AS userdb_gid, NULL as allow_nets FROM mailbox WHERE username = '%u' AND domain = '%d' AND active = '1'
user_query = SELECT [color="Magenta"]CONCAT('/mail/', maildir) AS home[/color], '' as mail, '89' AS uid, '89' AS gid, ROUND ( quota / 1024 ) AS quota FROM mailbox WHERE username = %u AND domain = %d AND active = '1'
```


----------



## hockey97 (Feb 19, 2012)

Thanks for the response. I've been trying to figure out why Thunderbird wasn't working. So, I looked at the mail logs and it took me 2 weeks since I am not expert and this was my first time installing and setting up a mail server. I tried some random ideas of what possibly was wrong and thought of the possible solutions to it. Failed unil I read the mail logs closely and found out that in the mail location it wasn't putting my domain name.

I had this:

```
/etc/local/virtual/%d/%n
```

Now I know that %d was to put in whatever domain name the mail was for like domain.com etc.
So,what I was expecting from the above was this:

```
/etc/local/virtual/domain.com/bob
```

Yet, I got this:


```
/etc/local/virtual//bob
```

No domain name gets written in the path. 

Any ideas as to how to get the domain name posted in like that or at least what can I do to figure out what I need to do or set to pass the domain name of the e-mail address to that %d variable. 

I host more than 1 domain name and do need a setup where I can store the mail for users for each domain name separately. 

I have the domain name as a file name and then have a sub-folder inside that domain name folder of each user. 

I am clueless at how to get the domain name passed to dovecot so the user can grab their mail and not be given an error saying no user exist etc or some random error.

I just googled around and found out someone said that when you use %d it takes the value from:


```
auth_default_realm=domain.com
```

If this is the case how can I grab this via mysqL?

Yet, how can I grab the username they are logging in with to grab that specific domain name?


----------



## ecazamir (Feb 19, 2012)

Why don't you use 

```
password_query = SELECT password,CONCAT('/etc/local/virtual/', maildir) AS userdb_home, '89' AS userdb_uid, '89' AS userdb_gid, NULL as allow_nets FROM mailbox WHERE username = '%u' AND domain = '%d' AND active = '1'
user_query = SELECT CONCAT('/etc/local/virtual/', maildir) AS home, '' as mail, '89' AS uid, '89' AS gid, ROUND ( quota / 1024 ) AS quota FROM mailbox WHERE username = %u AND domain = %d AND active = '1'
```
and

```
mail_location = maildir:~/
```

Dovecot will find user's home directory from the userdb user query, and then will expand maildir:~/ to maildir:/etc/local/virtual/mail.account@domain.com/ at runtime. The home directory is hold by the postfix database, this path is usually relative to the mail root directory (/etc/local/virtual in your case). Depending on postfixadmin config, it will contain path lists including the username and the domain, ie: [user@domain.tld (postfixadmin default), or domain.tld/user if postfixadmin is configured to hold each domain mailboxes into separate directories. 

The CONCAT SQL statement above has the role of converting the relative path (userdir/) stored by postfix database to an absolute path (/etc/local/virtual/userdir/).

You should set auth_realms to your list of domains, and the auth_default_realm will hold the domain for which your users don't need to pass the domain along with the username (john.doe instead john.doe@domain.tld) when logging in.

You can find why dovecot can't find mail by enabling debug.


----------



## hockey97 (Feb 20, 2012)

ecazamir said:
			
		

> Why don't you use
> 
> ```
> password_query = SELECT password,CONCAT('/etc/local/virtual/', maildir) AS userdb_home, '89' AS userdb_uid, '89' AS userdb_gid, NULL as allow_nets FROM mailbox WHERE username = '%u' AND domain = '%d' AND active = '1'
> ...



Yes, I have the debugger on. Right now it works perfectly. I can receive mail. Yet, thunderbird is working. I can see the new mail I get and even old mail.

*W*hat I am saying is that the problem is that %d isn't spitting out the domain name, it's giving nothing.

*S*o my mail_location path is broken when using %d, it gives nothing so a blank is put there. 

*F*or example this is what I want:

```
/etc/local/virtual/domain.com/bob
```

*Y*et, this is what is happening:

```
/etc/local/virtual//bob
```

*W*hat I have now is that I manually type in the domain name and it works perfectly.

Yet, I am hosting mail for multiple domain names. So this way it won't fly. 
Because I would be limited to just one domain name. 

So, I need to use that %d variable to use the curren domain name being used.

In the database I already do have a list of approved domain names.


----------



## ecazamir (Feb 20, 2012)

You don't need to set 
	
	



```
mail_location = [FILE]/path[/FILE]/%d/%u
```
 if the userdb is providing the user's account home directory. I think that auth_default_realm parameter is used only when authenticating the user, not when determining it's home directory. If the domain is not provided at authentication time, then dovecot may have an empty %d, and nothing to expand. 

When you host multiple domains your users will need to authenticate with [email='user@domain.com]'user@domain.com[/email]' instead of 'user', otherwise you can't have a [email='john.doe@domain1.tld]'john.doe@domain1.tld[/email]' and a [email='john.doe@domain2.tld]'john.doe@domain2.tld[/email]'.

Is there any special reason why you don't provide user's directory from the userdb? Can you provide more details about how it's your server configured? How you deliver mail, how are the mailboxes stored on disk, how do authenticate the users and how do you locate users' directories?


----------

