Mysql

如何使用 MySQL 在 Postfix 中指定目標目錄?

  • September 14, 2015

我已經用 Postfix、Dovecot 和 MySQL 設置了一個郵件伺服器。Dovecot 完美執行。Postfix 發送郵件,但我在接收郵件時遇到問題。

經過2天的調試和調整配置,我現在可以成功接收郵件了。唯一的問題是 Postfix 將郵件保存在錯誤的位置。我希望將郵件保存在/var/mail/vhosts/domain.tld/user/ 目錄中,user@domain.tld但郵件保存在/var/mail/vhosts/user@domain.tld 文件中。

/ v a r / m a i l / v h o s t s / u s e r @ d o m a i n . t l d
-------------------------------   -----------------------------
^    virtual_mailbox_base     ^   ^     sql result from       ^
                                 |   virtual_mailbox_maps    |

後綴配置

# (postconf -d; postconf -d; postconf -n;) | sort | uniq -u
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
broken_sasl_auth_clients = yes
local_recipient_maps = $virtual_mailbox_maps
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
mydestination = localhost.localdomain, localhost
myhostname = {censored}
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
process_id = 6297
process_id = 6298
readme_directory = no
recipient_delimiter = +
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = private/auth
smtpd_sasl_tls_security_options = noanonymous
smtpd_sasl_type = dovecot
smtpd_tls_CAfile = /etc/ssl/certs/cacert.pem
smtpd_tls_cert_file = /etc/ssl/certs/mail.roofworkshop.com.crt
smtpd_tls_key_file = /etc/ssl/private/mail.roofworkshop.com.key
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_security_level = may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_note_starttls_offer = yes
smtp_tls_security_level = may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
virtual_gid_maps = static:5000
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_uid_maps = static:5000

mysql-virtual-mailbox-domains.cf

user = mailuser
password = {censored}
hosts = 127.0.0.1
dbname = mailserver
query = SELECT name FROM virtual_domains WHERE name='%s'

mysql-virtual-mailbox-maps.cf

user = mailuser
password = {censored}
hosts = 127.0.0.1
dbname = mailserver
query = SELECT email FROM virtual_users WHERE email='%s'

另一種方法是在 postfix 中使用 mysql 擴展功能。見man 5 mysql_table

這是您查詢的替代版本

query = SELECT '%d/%u/' FROM virtual_users WHERE email='%s';

當輸入為user@example.com時,字元串%d/%u/將擴展為example.com/user/. 這部WHERE email='%s'分會將此查詢限制為僅有效的使用者名。

另請參閱此部落格文章以獲取此擴展的範例。


文件頁面的相關摘錄

query 用於搜尋數據庫的 SQL 查詢模板,其中 %s 是 Postfix 試圖解析的地址的替代品,例如 query = SELECT replacement FROM aliases WHERE mailbox = ‘%s’

此參數支持以下 ‘%’ 擴展:

%% 這被替換為文字 ‘%’ 字元。

%s 這被輸入鍵代替。SQL 引用用於確保輸入鍵不會添加意外的元字元。

%u 當輸入鍵是 user@domain 形式的地址時,%u 被替換為 SQL 引用的地址的本地部分。否則,%u 將被整個搜尋字元串替換。如果 localpart 為空,則查詢被抑制並且不返回任何結果。

%d 當輸入鍵是 user@domain 形式的地址時,%d 將替換為地址的 SQL 引用域部分。否則,查詢將被抑制並且不返回任何結果。

引用自:https://serverfault.com/questions/722137