首页 > Linux > 基于Debian简单配置Postfix+Courier+MySQL+SquirrelMail邮件服务器

基于Debian简单配置Postfix+Courier+MySQL+SquirrelMail邮件服务器

2011年5月7日 LTNS     访问次数 1,454 发表评论 阅读评论

上月试过 基于Debian简单配置Exim4+Courier+SquirrelMail邮件服务器 ,在此基础上参考文章(1),前些天把exim4换成了postfix,以通过虚拟域和mysql数据库来管理邮箱用户,另外还通过SquirrelMail的change_sqlpass插件实现了Webmail用户自行修改密码的功能,在此做个记录。

主要配置如下:

MTA: Postfix
SMTP: Saslauthd SSL
POP/IMAP: Courier SSL
Database: MySQL
WebMail: SquirrelMail


还是因vps空间和内存有限,所以反垃圾、杀毒、内容过滤等仍不考虑。

 

1. 用putty登录VPS后,安装相应的软件包

apt-get update
apt-get install postfix postfix-mysql postfix-doc courier-authdaemon courier-authlib-mysql courier-pop courier-pop-ssl courier-imap courier-imap-ssl libsasl2-modules-sql libpam-mysql libpam-smbpass

我的VPS预装了Openvz的debian 5.0模板,之后升级至6.0(Squeeze),然后安装了 LNMP 0.7一键安装包 ,所以这里就没有如参考文章(1)那样安装某些已有的软件包如mysql-client、php5-mysql等,或者不需要的软件包如apache2,请根据自己系统的实际情况选择需要安装的软件包。

软件包安装的过程中会提问一些配置方面的问题,一般默认即可

General type of mail configuration: Internet Site
System mail name: server1.example.com
SSL certificate required? Ok
Workgroup/Domain Name: WORKGROUP
Modify smb.conf to use WINS settings from DHCP? No

 

2. 对Postfix做Quota patch(配额补丁)

apt-get build-dep postfix
cd /usr/src
apt-get source postfix
postconf -d | grep mail_version  #检查一下当前的postfix版本,我的是2.7.1
wget  http://vda.sourceforge.net/VDA/postfix-vda-2.7.1.patch  #下载对应版本的补丁
cd postfix-2.7.1
patch -p1 < ../postfix-vda-2.7.1.patch  #打补丁
dpkg-buildpackage
cd ..
ls -l

这时应显示如下文件清单

openvz:/usr/src# ls -l
total 6328
drwxr-xr-x 19 root root 4096 May 5 01:36 postfix-2.7.1
-rw-r–r– 1 root src 42286 May 5 01:37 postfix-cdb_2.7.1-1_i386.deb
-rw-r–r– 1 root src 144992 May 5 01:37 postfix-dev_2.7.1-1_all.deb
-rw-r–r– 1 root src 1001226 May 5 01:37 postfix-doc_2.7.1-1_all.deb
-rw-r–r– 1 root src 49516 May 5 01:37 postfix-ldap_2.7.1-1_i386.deb
-rw-r–r– 1 root src 44274 May 5 01:37 postfix-mysql_2.7.1-1_i386.deb
-rw-r–r– 1 root src 43968 May 5 01:37 postfix-pcre_2.7.1-1_i386.deb
-rw-r–r– 1 root src 44294 May 5 01:37 postfix-pgsql_2.7.1-1_i386.deb
-rw-r–r– 1 root src 59667 Nov 5 22:05 postfix-vda-2.7.1.patch
-rw-r–r– 1 root src 234277 May 5 01:33 postfix_2.7.1-1.diff.gz
-rw-r–r– 1 root src 1181 May 5 01:33 postfix_2.7.1-1.dsc
-rw-r–r– 1 root src 3961 May 5 01:37 postfix_2.7.1-1_i386.changes
-rw-r–r– 1 root src 1332980 May 5 01:37 postfix_2.7.1-1_i386.deb
-rw-r–r– 1 root src 3418747 Jun 22 2010 postfix_2.7.1.orig.tar.gz

完成补丁作业

dpkg -i postfix_2.7.1-1_i386.deb postfix-mysql_2.7.1-1_i386.deb

 

3. 给Postfix/Courier创建MySQL数据库

mysqladmin -u root -p create mail  #创建数据库,库名mail
mysql -u root –p  #以root身份登录MySQL,输入密码
#在 mysql> 提示符下输入如下几段内容
 
#MySQL中创建访问数据库mail的用户 mail_admin 及其密码(请自行替换mail_admin_password字段)
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost' IDENTIFIED BY 'mail_admin_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO 'mail_admin'@'localhost.localdomain' IDENTIFIED BY 'mail_admin_password';
FLUSH PRIVILEGES;
 
#创建mail数据库的表
USE mail;
 
CREATE TABLE domains (
domain varchar(50) NOT NULL,
PRIMARY KEY (domain) )
TYPE=MyISAM;
 
CREATE TABLE forwardings (
source varchar(80) NOT NULL,
destination TEXT NOT NULL,
PRIMARY KEY (source) )
TYPE=MyISAM;
 
CREATE TABLE users (
email varchar(80) NOT NULL,
password varchar(20) NOT NULL,
quota bigint(20) DEFAULT '10485760',
PRIMARY KEY (email)
) TYPE=MyISAM;
 
CREATE TABLE transport (
domain varchar(128) NOT NULL default '',
transport varchar(128) NOT NULL default '',
UNIQUE KEY domain (domain)
) TYPE=MyISAM;
 
quit;  #退出MySQL Shell界面

然后用vi或在Winscp中修改MySQL的配置文件 /etc/my.cnf ,确认其中必须有一行

...
bind-address = 127.0.0.1
...

注意不是参考文章(1)中提及的/etc/mysql/my.cnf而是/etc/my.cnf,后者是LNMP一键包安装后MySQL的配置文件的路径,但由于步骤1完成后MySQL配置文件的路径被自动改成了/etc/mysql/my.cnf,为避免MySQL运行出错,需做如下处理

cd /etc/mysql
mv my.cnf my.cnf.org  #原配置文件改名备份一下
ln -s ../my.cnf my.cnf  #制作一个软链接,引用上一级目录的配置文件
/etc/init.d/mysql restart  #重启MySQL
netstat -tap | grep mysql  #检查MySQL的监听端口
#应类似如下显示
  tcp       0    0 *:mysql         *:*         LISTEN     19671/mysqld

/etc/postfix 目录下创建如下六个文件,注意其中的mail_admin_password字段需自行替换。

/etc/postfix/mysql-virtual_domains.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT domain AS virtual FROM domains WHERE domain='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_forwardings.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_mailboxes.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_email2email.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT email FROM users WHERE email='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_transports.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT transport FROM transport WHERE domain='%s'
hosts = 127.0.0.1

/etc/postfix/mysql-virtual_mailbox_limit_maps.cf

user = mail_admin
password = mail_admin_password
dbname = mail
query = SELECT quota FROM users WHERE email='%s'
hosts = 127.0.0.1

接下来修改这六个文件的权限和所属组等

chmod o= /etc/postfix/mysql-virtual_*.cf
chgrp postfix /etc/postfix/mysql-virtual_*.cf
groupadd -g 5000 vmail  #创建拥有邮箱目录的组
useradd -g vmail -u 5000 vmail -d /home/vmail -m  #创建拥有邮箱目录的用户

依次执行如下postconf命令以修改Postfix的配置,注意server1.example.com字段需替换成实际的邮箱域名

postconf -e 'myhostname = server1.example.com'
postconf -e 'mydestination = server1.example.com, localhost, localhost.localdomain'
postconf -e 'mynetworks = 127.0.0.0/8'
postconf -e 'message_size_limit = 30720000'
postconf -e 'virtual_alias_domains ='
postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf'
postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf'
postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf'
postconf -e 'virtual_mailbox_base = /home/vmail'
postconf -e 'virtual_uid_maps = static:5000'
postconf -e 'virtual_gid_maps = static:5000'
postconf -e 'smtpd_sasl_auth_enable = yes'
postconf -e 'broken_sasl_auth_clients = yes'
postconf -e 'smtpd_sasl_authenticated_header = yes'
postconf -e 'smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination'
postconf -e 'smtpd_use_tls = yes'
postconf -e 'smtpd_tls_cert_file = /etc/postfix/smtpd.cert'
postconf -e 'smtpd_tls_key_file = /etc/postfix/smtpd.key'
postconf -e 'transport_maps = proxy:mysql:/etc/postfix/mysql-virtual_transports.cf'
postconf -e 'virtual_create_maildirsize = yes'
postconf -e 'virtual_maildir_extended = yes'
postconf -e 'virtual_mailbox_limit_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_limit_maps.cf'
postconf -e 'virtual_mailbox_limit_override = yes'
postconf -e 'virtual_maildir_limit_message = "The user you are trying to reach is over quota."'
postconf -e 'virtual_overquota_bounce = yes'
postconf -e 'proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps$virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps$relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps$transport_maps $mynetworks $virtual_mailbox_limit_maps'
注:完成后检查一下 /etc/postfix/main.cf 这个配置文件,确认内容如上所示。

创建SSL证书

cd /etc/postfix
#创建证书,回答提问用默认的即可
openssl req -new -outform PEM -out smtpd.cert -newkey rsa:2048 -nodes -keyout smtpd.key -keyform PEM -days 365 -x509
chmod o= /etc/postfix/smtpd.key  #修改证书访问权限

 

4. 配置Saslauthd

先创建一个目录

mkdir -p /var/spool/postfix/var/run/saslauthd

然后修改 /etc/default/saslauthd 文件为如下内容,以实现开机时启动

...
# Should saslauthd run automatically on startup? (default: no)
START=yes
...
#OPTIONS="-c -m /var/run/saslauthd"
OPTIONS="-c -m /var/spool/postfix/var/run/saslauthd -r"

备份 /etc/pam.d/smtp 文件

cp /etc/pam.d/smtp /etc/pam.d/smtp.org

然后修改 /etc/pam.d/smtp 内容如下(注意只能是如下二行,且注意mail_admin_password字段的替换)

auth required pam_mysql.so user=mail_admin passwd=mail_admin_password host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1
account sufficient pam_mysql.so user=mail_admin passwd=mail_admin_password host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=1

创建 /etc/postfix/sasl/smtpd.conf 文件,内容如下,同样mail_admin_password字段需替换

pwcheck_method: saslauthd
mech_list: plain login
allow_plaintext: true
auxprop_plugin: mysql
sql_hostnames: 127.0.0.1
sql_user: mail_admin
sql_passwd: mail_admin_password
sql_database: mail
sql_select: select password from users where email = '%u'

然后重启

adduser postfix sasl  #添加sasl组的用户postfix
/etc/init.d/postfix restart  #重启postfix
/etc/init.d/saslauthd restart  #重启saslauthd

 

5. 配置Courier

修改 /etc/courier/authdaemonrc 文件为如下内容

...
#authmodulelist="authpam"
authmodulelist="authmysql"
...

然后备份并清空 /etc/courier/authmysqlrc 文件

cp /etc/courier/authmysqlrc /etc/courier/authmysqlrc_orig  #复制作备份
cat /dev/null > /etc/courier/authmysqlrc  #清空

/etc/courier/authmysqlrc 文件中填写如下内容,注意mail_admin_password字段的替换

MYSQL_SERVER localhost
MYSQL_USERNAME mail_admin
MYSQL_PASSWORD mail_admin_password
MYSQL_PORT 0
MYSQL_DATABASE mail
MYSQL_USER_TABLE users
MYSQL_CRYPT_PWFIELD password
#MYSQL_CLEAR_PWFIELD password
MYSQL_UID_FIELD 5000
MYSQL_GID_FIELD 5000
MYSQL_LOGIN_FIELD email
MYSQL_HOME_FIELD "/home/vmail"
MYSQL_MAILDIR_FIELD CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/')
#MYSQL_NAME_FIELD
MYSQL_QUOTA_FIELD quota

然后删除原证书

cd /etc/courier
rm -f /etc/courier/imapd.pem  #删除原证书
rm -f /etc/courier/pop3d.pem

修改 /etc/courier/imapd.cnf/etc/courier/pop3d.cnf 这两个文件,把server1.example.com字段替换成实际的邮箱域名

...
CN=server1.example.com
...

创建新证书

mkimapdcert  #创建证书
mkpop3dcert
/etc/init.d/courier-authdaemon restart  #重启各软件
/etc/init.d/courier-imap restart
/etc/init.d/courier-imap-ssl restart
/etc/init.d/courier-pop restart
/etc/init.d/courier-pop-ssl restart

测试一下pop3是否正常,键入quit退出

telnet localhost pop3  #测试
  Trying ::1...
  Connected to localhost.localdomain.
  Escape character is '^]'.
  +OK Hello there.
quit  #退出
  +OK Better luck next time.
  Connection closed by foreign host.

 

6. 修改 /etc/aliases 文件,把postmaster@exapmle.com字段替换成实际的邮件管理者邮箱(待步骤9时需创建该邮箱用户)

...
postmaster: root
# root: postmaster@yourdomain.tld
root: postmaster@exapmle.com
...

然后重启aliases和postfix

newaliases
/etc/init.d/postfix restart

 

7. 设置邮箱超配额提示功能

按如下内容创建 /usr/local/sbin/quota_notify 脚本文件,并把其中三行的postmaster@example.com字段如步骤6那样替换为实际的邮件管理者邮箱

#!/usr/bin/perl -w

# Author
#
# This script assumes that virtual_mailbox_base in defined
# in postfix's main.cf file. This directory is assumed to contain
# directories which themselves contain your virtual user's maildirs.
# For example:
#
# -----------/
#            |
#            |
#    home/vmail/domains/
#        |          |
#        |          |
#  example.com/  foo.com/
#                   |
#                   |
#           -----------------
#           |       |       |
#           |       |       |
#         user1/   user2/  user3/
#                           |
#                           |
#                        maildirsize
#

use strict;

my $POSTFIX_CF = "/etc/postfix/main.cf";
my $MAILPROG = "/usr/sbin/sendmail -t";
my $WARNPERCENT = 80;
# my @POSTMASTERS = ('postmaster@domain.tld');
my @POSTMASTERS = ('postmaster@example.com');
my $CONAME = 'My Company';
# my $COADDR = 'postmaster@domain.tld';
my $COADDR = 'postmaster@example.com';
# my $SUADDR = 'postmaster@domain.tld';
my $SUADDR = 'postmaster@example.com';
my $MAIL_REPORT = 1;
my $MAIL_WARNING = 1;

#get virtual mailbox base from postfix config
open(PCF, "< $POSTFIX_CF") or die $!;
my $mboxBase;
while () {
   next unless /virtual_mailbox_base\s*=\s*(.*)\s*/;
   $mboxBase = $1;
}
close(PCF);

#assume one level of subdirectories for domain names
my @domains;
opendir(DIR, $mboxBase) or die $!;
while (defined(my $name = readdir(DIR))) {
   next if $name =~ /^\.\.?$/;        #skip '.' and '..'
   next unless (-d "$mboxBase/$name");
   push(@domains, $name);
}
closedir(DIR);
#iterate through domains for username/maildirsize files
my @users;
chdir($mboxBase);
foreach my $domain (@domains) {
        opendir(DIR, $domain) or die $!;
        while (defined(my $name = readdir(DIR))) {
           next if $name =~ /^\.\.?$/;        #skip '.' and '..'
           next unless (-d "$domain/$name");
      push(@users, {"$name\@$domain" => "$mboxBase/$domain/$name"});
        }
}
closedir(DIR);

#get user quotas and percent used
my (%lusers, $report);
foreach my $href (@users) {
   foreach my $user (keys %$href) {
      my $quotafile = "$href->{$user}/maildirsize";
      next unless (-f $quotafile);
      open(QF, "< $quotafile") or die $!;
      my ($firstln, $quota, $used);
      while () {
         my $line = $_;
              if (! $firstln) {
                 $firstln = 1;
                 die "Error: corrupt quotafile $quotafile"
                    unless ($line =~ /^(\d+)S/);
                 $quota = $1;
            last if (! $quota);
            next;
         }
         die "Error: corrupt quotafile $quotafile"
            unless ($line =~ /\s*(-?\d+)/);
         $used += $1;
      }
      close(QF);
      next if (! $used);
      my $percent = int($used / $quota * 100);
      $lusers{$user} = $percent unless not $percent;
   }
}

#send a report to the postmasters
if ($MAIL_REPORT) {
   open(MAIL, "| $MAILPROG");
   select(MAIL);
   map {print "To: $_\n"} @POSTMASTERS;
   print "From: $COADDR\n";
   print "Subject: Daily Quota Report.\n";
   print "DAILY QUOTA REPORT:\n\n";
   print "----------------------------------------------\n";
   print "| % USAGE |            ACCOUNT NAME          |\n";
   print "----------------------------------------------\n";
   foreach my $luser ( sort { $lusers{$b} <=> $lusers{$a} } keys %lusers ) {
      printf("|   %3d   | %32s |\n", $lusers{$luser}, $luser);
      print "---------------------------------------------\n";
   }
        print "\n--\n";
        print "$CONAME\n";
        close(MAIL);
}

#email a warning to people over quota
if ($MAIL_WARNING) {
        foreach my $luser (keys (%lusers)) {
           next unless $lusers{$luser} >= $WARNPERCENT;       # skip those under quota
           open(MAIL, "| $MAILPROG");
           select(MAIL);
           print "To: $luser\n";
      map {print "BCC: $_\n"} @POSTMASTERS;
           print "From: $SUADDR\n";
           print "Subject: WARNING: Your mailbox is $lusers{$luser}% full.\n";
           print "Reply-to: $SUADDR\n";
           print "Your mailbox: $luser is $lusers{$luser}% full.\n\n";
           print "Once your e-mail box has exceeded your monthly storage quota\n";
      print "your monthly billing will be automatically adjusted.\n";
      print "Please consider deleting e-mail and emptying your trash folder to clear some space.\n\n";
           print "Contact <$SUADDR> for further assistance.\n\n";
           print "Thank You.\n\n";
           print "--\n";
           print "$CONAME\n";
           close(MAIL);
        }
}

修改 /usr/local/sbin/quota_notify 脚本文件的可执行权限

cd /usr/local/sbin/
chmod 755 quota_notify  #修改权限
crontab -e  #进入定时功能脚本编辑状态

在定时功能脚本最后添加如下一行,然后保存退出

0 0 * * * /usr/local/sbin/quota_notify &> /dev/null

 

8. 测试Postfix

telnet localhost 25  #测试命令
  Trying ::1...
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  220 example.com ESMTP Postfix (Debian/GNU)
ehlo localhost  #键入命令
  250-example.com
  250-PIPELINING
  250-SIZE 30720000
  250-VRFY
  250-ETRN
  250-STARTTLS
  250-AUTH PLAIN LOGIN
  250-AUTH=PLAIN LOGIN
  250-ENHANCEDSTATUSCODES
  250-8BITMIME
  250 DSN
quit  #退出测试
  221 2.0.0 Bye
  Connection closed by foreign host.

 

9. 给数据库mail添加数据

mysql -u root -p  #以root身份登录MySQL,输入密码
#在 mysql> 提示符下输入如下几段内容
 
#创建两个邮箱用户info@example.com和postmaster@exapmle.com,注意三个example.com字段需替换为实际的邮箱域名,密码字段secret也请自行替换
USE mail;
 
INSERT INTO `domains` (`domain`) VALUES ('example.com');
 
INSERT INTO `users` (`email`, `password`, `quota`) VALUES ('info@example.com', ENCRYPT('secret'), 10485760);
 
INSERT INTO `users` (`email`, `password`, `quota`) VALUES ('postmaster@exapmle.com', ENCRYPT('secret'), 10485760);
 
quit;  #退出

其实LNMP一键包内有phpMyAdmin软件包,利用该软件可以方便地进行MySQL数据库的操作,比如本步骤内的邮箱用户添加步骤如下图所示,注意password项需使用ENCRYPT功能以实现密码数据的加密保存

 

10. 创建邮箱用户对应的邮箱目录,注意example.com字段的替换

apt-get install mailx  #没有mailx命令的话需要安装,可能会提示三个选项
apt-get install bsd-mailx  #那么就随便选一个比如bsd-mailx来安装mailx
mailx info@example.com  #创建info@example.com对应的邮箱目录
Subject: Welcome  #标题随便填写,比如Welcome,然后回车
Welcome! Have fun with your new mail account.  #内容随便填写,然后回车
#同时按一下CTRL+D
Cc:  #直接回车退出

这时就会自动生成邮箱用户info@example.com对应的 /home/vmail/example.com/info 邮箱目录了,至于邮件管理者postmaster@example.com是否需要生成对应的邮箱目录请自行安排。

也可以用另一种方式手工创建邮箱目录,注意example.com字段的替换

maildirmake /etc/skel/Maildir  #建立模板
maildirmake /home/vmail/example.com/info  #创建邮箱目录
chown –hR vmail:vmail /home/vmail/example.com/info  #修改目录的所有者

 

11. 安装SquirrelMail及改密码插件change_sqlpass

apt-get install squirrelmail php-pear
apt-get install squirrelmail-locales squirrelmail-decode  #中文界面用的
pear install DB  #SquirrelMail的插件change_sqlpass用的
cd /usr/share/squirrelmail/plugins
wget http://www.squirrelmail.org/countdl.php?fileurl=http%3A%2F%2Fwww.squirrelmail.org%2Fplugins%2Fchange_sqlpass-3.3-1.2.tar.gz
mv *ns%2Fchange_sqlpass-3.3-1.2.tar.gz change_sqlpass-3.3-1.2.tar.gz  #改名
tar xvfz change_sqlpass-3.3-1.2.tar.gz
cd change_sqlpass
cp config.php.sample config.php

修改 /usr/share/squirrelmail/plugins/change_sqlpass/config.php 文件中的数行内容为如下所示,注意字段mail_admin_password的替换

...
// $csp_dsn = 'mysql://mail_admin:mail_admin_password@localhost/email_users';
$csp_dsn = 'mysql://mail_admin:mail_admin_password@localhost/mail';
...
// $lookup_password_query = 'SELECT count(*) FROM users WHERE username = "%1" AND crypt_password = %4';
$lookup_password_query = 'SELECT count(*) FROM users WHERE email = "%1" AND password = %4';
...
$password_update_queries = array(
 'UPDATE users SET password = %4 WHERE email = "%1"',
//            'UPDATE users SET crypt_password = %4 WHERE username = "%1"',
...
// $password_encryption = 'MYSQLPWD';
$password_encryption = 'MYSQLENCRYPT';
...
// $csp_salt_static = '';
$csp_salt_static = 'LEFT(password, 2)';
...
// $csp_salt_query = 'SELECT salt FROM users WHERE username = "%1"';
...
// $csp_delimiter = '|';
$csp_delimiter = '@';
...

安装兼容插件Compatibility

cd /usr/share/squirrelmail/plugins
wget http://www.squirrelmail.org/countdl.php?fileurl=http%3A%2F%2Fwww.squirrelmail.org%2Fplugins%2Fcompatibility-2.0.16-1.0.tar.gz
mv *ns%2Fcompatibility-2.0.16-1.0.tar.gz compatibility-2.0.16-1.0.tar.gz  #改名
tar xvfz compatibility-2.0.16-1.0.tar.gz
/usr/sbin/squirrelmail-configure  #进入SquirrelMail配置界面
#亦可执行如下命令进入配置界面
/etc/squirrelmail/conf.pl

设置中文化和添加插件

键入 10 进入Languages菜单
1. Default Language : zh_CN
2. Default Charset : UTF-8
按r键返回上一级目录
键入 8 进入Plugins菜单
分别键入compatibility和change_sqlpass对应的数字以添加插件
按s键、然后任意键保存
按q键退出配置界面

如无法显示中文则需要添加字库

dpkg-reconfigure locales

按空格键选中如下四个字库

zh_CN GB2312
zh_CN.GB18030
zh_CN.GBK
zh_CN.UTF-8

在提示用什么locale为默认时,选默认的None或zh_CN.UTF-8,然后执行 reboot 命令重启系统。

 

12. 可访问 “已有域名/squirrelmail/src/configtest.php” 地址检查一下SquirrelMail的配置是否正常

如有类似 “/var/lib/squirrelmail/data/ should be writable by user www” 的出错提示,就修改相应目录的访问权限。

chmod 777 /var/lib/squirrelmail/data
chmod 777 /var/spool/squirrelmail/attach

LNMP一键包安装的是nginx解译引擎,建议将 /usr/share/squirrelmail/ 目录软链接至某个已有的虚拟空间的根目录下,或在VPS上新建的虚拟空间配置文件中指向它以便通过邮箱域名访问,在SquirrelMail登录页面中输入info@example.com和密码

如登录SquirrelMail出错,请跟踪日志以确认问题所在

tail -f /var/log/mail.log &

登录正常后,如在 选项 > change password 界面中修改密码后提示出错 “Could not find Pear DB library”,则修改 /usr/local/php/etc/php.ini 文件(这是LNMP一键包安装的PHP配置文件路径,请按实际的路径去修改),添加PEAR的路径

...
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
include_path = ".:/usr/share/php:/usr/share/php/PEAR:/usr/local/php"
...

再次登陆SquirrelMail修改密码,这时修改密码成功后可能会提示 “This page request could not be verified and appears to have expired”, 按参考文章(2)修改一下 /usr/share/squirrelmail/plugins/change_sqlpass/functions.php 文件就解决了。

--- change_sqlpass/functions_orig.php   2011-03-28 22:34:25.000000000 +0200
+++ change_sqlpass/functions.php    2011-03-28 22:37:29.000000000 +0200
@@ -474,7 +474,7 @@
    sqgetGlobalVar('csp_was_already_encrypted_port', $csp_was_already_encrypted_port, SQ_SESSION);
    sqgetGlobalVar('HTTP_HOST', $HTTP_HOST, SQ_SERVER);
    if (empty($base_uri)) $base_uri = sqm_baseuri();
-   $loc = $base_uri . 'src/options.php?optpage=xx&optmode=submit&csp_change_success=no';
+   $loc = $base_uri . 'src/options.php?optpage=xx&optmode=submit&csp_change_success=no&smtoken=' . sm_generate_security_token();
    if ($csp_was_already_encrypted_port == 443)
       $cancel_location = 'https://' . $HTTP_HOST . $loc;
    else if ($csp_was_already_encrypted_port)
@@ -538,6 +538,7 @@
       <tr>
          <td align="right" colspan="2">
             <input type="hidden" name="csp_submit_change" value="1">
+       <input type="hidden" name="smtoken" value="<?php echo sm_generate_security_token(); ?>">
             <input type="submit" value="<?php echo _("Submit"); ?>">
 
@@ -927,7 +928,7 @@
          if (!empty($csp_was_force_mode))
             $loc = $base_uri . 'src/right_main.php';
          else
-            $loc = $base_uri . 'src/options.php?optpage=xx&optmode=submit&csp_change_success=yes';
+            $loc = $base_uri . 'src/options.php?optpage=xx&optmode=submit&csp_change_success=yes&smtoken=' . sm_generate_security_token();
          if ($csp_was_already_encrypted_port == 443)
             $redirect_location = 'https://' . $HTTP_HOST . $loc;
          else if ($csp_was_already_encrypted_port)

最后到邮件客户端程序(比如outlook express、foxmail等)设置一下,POP(995)/IMAP(993)和SMTP(25)都需要SSL认证,帐户名info@example.com(请替换为实际的邮箱域名),SMTP还需要发信身份认证,现在就可以在客户端进行邮件收发了。

如收不到邮件,请跟踪一下日志,如有提示 “postfix/trivial-rewrite[3092]: warning: do not list domain example.com in BOTH mydestination and virtual_mailbox_domains”,则按如下内容修改一下 /etc/postfix/main.cf 文件就解决了

...
# mydestination = server1.example.com, localhost, localhost.localdomain
mydestination =
...

 
 

补充:
安装可显示当前用户邮箱地址、ip和上次登录时间等信息的插件

cd /usr/share/squirrelmail/plugins
wget http://www.squirrelmail.org/countdl.php?fileurl=http%3A%2F%2Fwww.squirrelmail.org%2Fplugins%2Fshow_user_and_ip-3.3-re-1.2.2.tar.gz
mv *ns%2Fshow_user_and_ip-3.3-re-1.2.2.tar.gz show_user_and_ip-3.3-re-1.2.2.tar.gz  #改名
tar show_user_and_ip-3.3-re-1.2.2.tar.gz
cd show_user_and_ip
cp config.php.sample config.php
/etc/squirrelmail/conf.pl  #进入SquirrelMail配置界面

登录后到选项里面勾选在左边栏显示用户邮箱地址等信息,另外还可以修改配置文件 /usr/share/squirrelmail/plugins/show_user_and_ip/config.php 以改变字体大小等参数

...
/*
 * Font size for username, IP and hostname displayed
 * on the left pane.
 *
 * Default: -2
*/

$sui_font_size = 2;
...

登录后显示如下

更多插件请访问 SquirrelMail官方网站

 
 

参考文章
(1) http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-debian-lenny
(2) http://blog.rtfm.co.hu/2011/03/squirrelmail-change_sqlpass-this-page-request-could-not-be-verified-and-appears-to-have-expired/

 

  1. 2011年7月19日19:09 | #1

    Squirrelmail实在太烂,推荐用Roundcube

    • LTNS
      2011年7月20日20:55 | #2

      谢谢,回头有空试试Roundcube

  2. pachongli
    2011年11月29日11:10 | #3

    你好:我在看你的debian下安装Postfix+Courier+Mysql+SquirrelMail文档,其中在安装sasl的时候报错请能帮忙看看啥问题吗

  3. LTNS
    2011年11月29日12:03 | #4

    @pachongli
    什么样的出错提示呢?
    文章里的命令行或引用内容在有的浏览器比如Firefox里面显示不全,可先复制粘贴到记事本再使用。
    顺便建议先看一下老外的博客原文,里面有不少跟贴提及安装过程中的出错及解决办法。

  4. 2015年1月1日16:36 | #5

    请问一下:我是按照上面步骤做的,但是在创建有目录的时候,
    Vmail可以创建,但是用Mailx 给 MYSQL中登录的用户发邮件时,用户的文件夹不能自动生成在/home/vmail/example.com/的目录下。其他都很正常。所以导致最后登录到Squirrelmail界面的时候,出现ERROR:
    ERROR: Connection dropped by IMAP server.

    如果手动生成文件的时候,里面内容也没有自动添加。所以请问您是怎么解决的。

  5. LTNS
    2015年1月7日15:06 | #6

    @itaohan
    不好意思,很久没折腾了。

    我记得当时用mailx自动创建、或者手工创建邮件帐户的邮箱目录都是可以的,不行的话请重装一遍mailx再试试。

  1. 本文目前尚无任何 trackbacks 和 pingbacks.
6 - 3 = (必填)