Showing posts with label Email Server. Show all posts
Showing posts with label Email Server. Show all posts

July 22, 2015

How do I delete thousands of e-mail messages with cPanel or Horde's webmail interface?

Answer: How do I delete thousands of e-mail messages with cPanel or Horde's webmail interface?
If you run a website, you may find out one day that you have a mail account containing thousands of messages — or possibly even hundreds of thousands. The most common reason this happens is because the default e-mail account is set up as a "catch-all" address. This will direct all e-mail sent to invalid usernames at your domain to the default Inbox. Because spammers send out spammessages to invalid e-mail addresses all the time, the number of e-mails received by the default mailbox can grow by thousands of messages each day.
The best solution to this problem is to prevent it. If you have a Web server that is running cPanel, you can turn off the "catch-all" address by selecting the "Default Address" panel within the Mail section of the cPanel interface. Then choose the option that says "Discard with error to sender (at SMTP time)." This will bounce all invalid e-mails back to the sender and will keep the messages from piling up in your default mailbox. Over time, this can help save a lot of hard disk space on your server.
Of course, if you're like me, you caught this problem after thousands of messages had already filled your default e-mail account's Inbox. In my case it was over 500,000 messages. I realized that deleting these messages one page at a time via the Horde webmail interface would take awhile (as in several days). So I figured there must be some easier way to delete the messages. Fortunately, after doing some research, I found that there is indeed a simple way to remove all the messages in the Inbox.
Just follow these basic steps:
  1. Log into the Horde Mail system.
  2. Click the "Folders" icon at the top of the window.
  3. Check the box next to "Inbox" or the mailbox that contains the messages you want to delete.
  4. From the "Choose Action:" drop-down menu, select "Empty Folder(s)."
  5. You will be prompted to confirm the action, as it is not undoable.
  6. Click "Empty Selected Folders" to clean out the mailbox.
Depending on how many messages are stored in your Inbox, this operation may take a few seconds to several minutes to complete. The only catch is that if the mailbox contains too many messages, the operation will time out and will not empty the mailbox. This is what happened to me with my 500,000+ messages.
If this is also your situation, you may have to delete the message folders manually. To do this, you will first need to navigate to the mail folder, which is located in the following directory:
/home/username/mail/ (where "username" is the username for the mail account)
The two folders you'll need to delete are the cur and new folders, which contain the messages. You may be able to do this with an SSH or SFTP program, though I also had problems with this method timing out. If you know some basic Unix commands, you can use a terminal (command-line) program to manually delete the folders. First navigate to the mail folder, then recursively delete the cur and new directories. The commands to do this are:
$ cd /home/username/mail
$ rm -rf cur
$ rm -rf new
Again, even this method may take awhile if the directories are loaded with thousands of mail messages. But it should eventually remove the folders. This command-line method should only be used a last resort, as the maildirsize file will no longer contain the correct information for the mail account. Fortunately, as soon as a new message is received into the empty Inbox, the cur and new folders are automatically recreated and the maildirsize is updated with the correct data.
If you have been wondering how to delete a large number of messages stored on your mail sever, I hope this information is helpful and saves you a lot of time.


bonus: 

The directories responsible for a default cPanel email account is “new” and “cur“. You can find it in /home/username/mail/cur and /home/username/mail/new. If you want to remove all the emails for the default email account follow the steps given below:
# cd /home/username/mail/cur
# rm -rf *
# /home/username/mail/new
# rm -rf *
Even though you have removed all the emails from cur and new folder, the email disk quota in cPanel won’t be changed. This is because the email disk quota is cached in the file “maildirsize“. Either remove the file “maildirsize” or just rename it.
That’s it!!
Done!!!

July 14, 2015

Copy Outgoing Email on a cPanel Server with Exim

Note: It is mentioned in the comments that recent versions of cPanel include an Email Archiving feature that stores copies of email on the server for you.  If this is what you’re trying to do, this feature is probably the better option. However, if you’re trying to forward copies of outgoing email to other addresses, the instructions in this post are appropriate.
A common request we get is how a user can automatically BCC outgoing email to another address. This is most often requested by law firms that need to be able to retain copies of communication. Forwarding incoming email is easy – there’s already a section in cPanel for it. However, if you want to forward outgoing email there’s a small twist.
First, go to WHM > Exim Configuration Editor, then click “Advanced Editor” at the bottom. Don’t make any changes, just click “save”. This should create a file on the server called /etc/exim.conf.local. From command line as root, you’ll need to edit /etc/exim.conf.local and add some options to the @CONFIG@ and @TRANSPORTSTART@ sections. The end result would look like this (plus any edits you may have already made):

@AUTH@
@BEGINACL@
@CONFIG@
system_filter_directory_transport = local_copy_outgoing

@DIRECTOREND@
@DIRECTORMIDDLE@
@DIRECTORSTART@
@ENDACL@
@RETRYEND@
@RETRYSTART@
@REWRITE@
@ROUTEREND@
@ROUTERSTART@
@TRANSPORTEND@
@TRANSPORTMIDDLE@
@TRANSPORTSTART@
local_copy_outgoing:
driver = appendfile
delivery_date_add
envelope_to_add
return_path_add
group = cpaneleximfilter
user = cpaneleximfilter
mode = 0660
maildir_format = true
create_directory = true
Now, make a copy of the existing system filter file to a custom location, so cPanel updates don’t mess with it:
cp /etc/cpanel_exim_system_filter /etc/cpanel_exim_system_filter_custom
Edit /etc/exim.conf.localopts and change the “systemfilter” option to match our new file:
systemfilter=/etc/cpanel_exim_system_filter_custom
Now, open /etc/cpanel_exim_system_filter_custom and add the following block of code to the end of the file:
if $header_from: contains "@senderdomain.com"
then
unseen deliver "other@emailaddress"
endif
Replace the blue text with the sender domain (or email address) and the email address that outgoing mail should be copied to.
Apply these new settings to the exim.conf and restart:
/scripts/buildeximconf
service exim restart
Now when you send email from the email address or domain specified in the filter file, it will be copied to the other email address.

July 13, 2015

Làm gì khi VPS spam Mail ?

Rất nhiều người thuê VPS hoặc máy chủ vật lý không có (hoặc rất ít) kiển thức quản lý máy chủ. Họ chỉ quản trị dựa vào các Control Panel như Cpanel hay DirectAdmin, hoặc sự hỗ trợ của kỹ thuật của các nhà cung cấp dịch vụ. Bài viết này cung cấp một số cách tìm kiếm một user trong vps spam mail chạy DirectAdmin hoặc Exim.
Nhận biết:
  • VPS của bạn tự nhiên chậm, RAM và CPU tăng cao
  • Email không đến được người nhận
  • DirectAdmin thông báo lượng mail gửi ra nhiều
  • Địa chỉ IP bị Blacklist
Nguyên nhân:
  • Nếu là máy chủ website có thể source code bạn có vấn đề. Hãy chắc chắn rằng các module, plugin, theme là đáng tin cậy.
  • SMTP spam: Có thể tài khoản nào đó bị đánh cắp
  • User chủ động spam mail ra ngoài (Email Marketing)
Cách xác định:
# exim -bpc    // số lượng mail queue. Nếu lượng mail queue > 100 mail thì chắc chắn vps của bạn đang spam ra ngoài.
# exim -bp    / danh sách mail trong mail queue. Tại đây bạn sẽ thấy user nào đang spam ra ngoài.
Bạn cũng có thể kiểm tra log bằng lệnh:
# cd/var/log/exim
# tail -n 100 mainlog |more
hoặc
tail -n 10000 mainlog |grep user@example.com |more    // lệnh này sẽ liệt kê log theo user được truy vấn
Để xem nội dung mail dùng lệnh sau:
# exim -Mvh [id]    // id dạng 1VwdCb-0001ze-5c
Xoá một mail đang nằm trong Queue:
# exim -Mrm {message-id}
Xoá toàn bộ email đang có trong Queue:
# exim -bp | awk ‘{ print $3 }’ | xargs exim -Mrm
Gửi toàn bộ email đang có trong Queue:
# /usr/sbin/exim -bp |awk ‘{print $3}’ | xargs -n 1 -P 40 /usr/sbin/exim -v -M
Tìm nguồn spam
Giả sử rằng sau một loạt câu lệnh trên bạn đã tìm được user nào đang spam. Việc làm đầu tiên là bạn nên suspend tài khoản đó để tạm dừng việc spam. Sau đó bạn vào source code của user đó:
# cd /home/username/domains/example.com/public_html
Cách tốt nhất là bạn download source code về máy dùng các trình antivirus để quét. Đừng quên backup lại một bản vì có thể khi quét sẽ mất file làm hỏng source. Thường thì các file mã độc này sẽ được mã hóa. Tuy nhiên bạn có thể tìm nhanh bằng câu lệnh sau:
# find . -name ‘*.php’ | while read FILE; do if grep ‘eval(base64_decode’ “$FILE”; then echo “$FILE” >> filemadoc; fi ; done
hoặc
# find . -name ‘*.php’ | while read FILE; do if grep ‘eval(base64_decode’ “$FILE”; then echo “$FILE”; fi ; done

June 30, 2015

Email with attachment file sent to mailing list cannot be received by receipent

As title, I encounter the problem when I send email to my mailing list with attachment file.

There are 2 cases,

1. attachment < 60KB
>> every one can receive the email.

2. attachment > 60KB
>> no one receive the email.


Does any one have the same experience? Any solution?

If you're not a systems administrator then you should be asking your hosting company this question.

if you are a systems administrator with shell access:

What's the output of the following command while logged in through the shell:

grep message_size_limit /etc/exim.conf

??

Also, this time as root:

grep maxlength /etc/vitual/example.com/majordomo/lists/listname.conf

where listname is the name of your list and example.com is the name of the domain.

In both these cases the number must be large enough for the attachment after it's encoded.

June 26, 2015

June 22, 2015

Cài đặt Postfix SMTP client trên Shinken Server để gửi mail

Tại Shinken host:

Cài đặt các gói cần thiết:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules -y
Sau đó postfix sẽ yêu cầu khai báo một số thông số, ta chọn Internet Site và điền một domain như mail.shinken.vdc
Sửa file cấu hình postfix:
vi /etc/postfix/main.cf
Thêm/sửa các dòng như sau:
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
inet_interfaces = all
inet_protocols = ipv4
Sau đó thêm vào dòng sau:
[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD
Với USERNAME là username của tài khoản gmail dùng để gửi mail, PASSWORD là mật khẩu của tài khoản đó.
chmod 400 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd
cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
/etc/init.d/postfix reload
Để kiểm tra xem các bước cài đặt đã đúng chưa ta có thể sử dụng lệnh sau để gửi thử một email, đăng nhập vào trình duyện kiểm tra mục thư đã gửi để xem thư đã được gửi đi chưa.
/etc/init.d/postfix reload
echo "Test email" | mail -s "Shinken" ducnc92@hotmail.com`
Để kiểm tra và sửa lỗi ta có thể sử dụng lệnh mailq để xem trạng thái của các mail trong hàng đợi, hoặc là xem log của postfix để sửa lỗi.
tail –f /var/log/mail.log
Một lỗi có thể gặp phải đó là tài khoản gmail được khai báo bên trên có thể sử dụng cơ chế đăng nhập hai nhân tố hoặc mức cao nên có thể postfix không gửi mail được mặc dù cấu hình đúng. Để khắc phục điều này ta có thể tắt các chức năng này đi hoặc tạo một tài khoản mới mà không sử dụng các chức năng này.

June 19, 2015

Khắc phục lỗi không gửi mail thông báo cho khách hàng của directadmin

Nếu bạn đang quản lý các hosting bằng Direct Admin, bạn sẽ rất khó chịu khi mà tạo xong các hosting nhưng DA không chịu gửi mail thông báo cho khách hàng. Khi đó bạn sẽ phải gửi các thông báo bằng tay rất mất thời gian. Hãy xử lý tình huống này theo hướng dẫn sau

90 %  Nguyên nhân do exim trên server chưa được update

Bạn hãy update theo hướng dẫn dưới đây là xong:

Bước 1: chạy các lệnh sau lần lượt để update exim
  • wget -O /etc/exim.conf http://files.directadmin.com/services/exim.conf
  • wget -O /etc/exim.pl http://files.directadmin.com/services/exim.pl
  • chmod 755 /etc/exim.pl
  • cd /etc/virtual
  • touch blacklist_domains whitelist_from use_rbl_domains bad_sender_hosts blacklist_senders whitelist_domains whitelist_hosts whitelist_senders
  • if [ ! -e limit ]; then echo 0 > limit; fi
  • mkdir -p usage
  • chown mail:mail limit blacklist_domains whitelist_from use_rbl_domains bad_sender_hosts blacklist_senders whitelist_domains whitelist_hosts whitelist_senders limit usage
Bước 2: kiểm tra xem file exim.conf nằm ở đâu, sau đó chạy một trong 2 lệnh sau đây. Lưu ý lệnh patch này chỉ chạy một lần duy nhất và đúng đường dẫn là được
  • patch -p0 < /usr/local/directadmin/customapache/exim.conf.dovecot.patch
  • patch -p0 < /usr/local/directadmin/custombuild/exim.conf.dovecot.patch
Bước 3: Khởi động lại exim  bằng 1 trong 3 lệnh sau, và nếu cẩn thận thì reboot luôn server
  • /sbin/service exim restart (đối với Red Hat - Centos)
  • /etc/init.d/exim restart (Đối với Debian)
  • /usr/local/etc/rc.d/exim restart (Đối với FreeBSD)

10% do email khách hàng bị lỗi hoặc tiến trình gửi mail bị lỗi

Hãy kiểm tra các lỗi trong file này để có hướng xử lý tiếp /var/log/exim/mainlog. Nếu thấy lỗi là "Unrouteable address" thì chỉ cần download bản exim.pl mới nhất theo link sau là ok
  • wget -O /etc/exim.pl http://files.directadmin.com/services/exim.pl
Còn nếu không phải lỗi này thì tìm hiểu tiếp xem quá trình gửi email bị tắc ở đâu. Đọc thêm bài viết về xử lý hàng đợi email exim ở đây để có thêm thông tin.
Chúc các bạn gặp may

Xây dựng mail server postfix và quản lý email bằng php mysql

Trước đây tôi đã có một bài viết hướng dẫn triển khai mail server đơn giản trên hệ điều hành linux, sử dụng các user của hệ điều hành làm email account. Nay tôi tiếp tục phát triển hướng này và chia sẻ cách để có thể quản lý được nhiều tên miền nhiều email trên một server.

Để tìm hiểu từ gốc đến ngọn các bạn có thể tham khảo bài viết này để biết cách hình thành một mail server nguyên thủy là như thế nào, tuy nhiên nếu muốn học theo kiểu step-by-step thì vẫn có thể bắt đầu ngay với bài viết này và chịu khó theo đúng từng bước của bài viết là xong.
Chuẩn bị: 

  • Bạn cần có một máy chủ hoặc một VPS cài đặt hệ điều hành Centos 5.x trở lên
  • Giả định là bạn đã biết về webserver và đã cài đặt sẵn các ứng dụng apache, php, mysql vàphpmyadmin lên server này nếu như bạn chưa có các ứng dụng này, hãy tham khảo bài viết này or bài này để cài đặt chúng.
1. Thiết lập hostname

Đầu tiên chúng ta cần kiểm tra hostname của máy chủ vì hostname này sẽ dùng cho một số thiết lập hệ thống sau này. Để kiểm tra hostname của linux, chúng ta dùng lệnh sau

# hostname -f

Để thay đổi hostname, chúng ta chỉnh sửa dòng HOSTNAME trong file /etc/sysconfig/network. 

Ngoài ra ta cũng cần định nghĩa hostname gắn với ip của máy chủ này trong file /etc/hosts. Việc định nghĩa hostname trong file /etc/hosts sẽ giúp server có thể diễn dịch các hostname dùng để test mà không cần phải định nghĩa các bản ghi trên DNS server. 


2: Kiểm tra và update yum and repo
Trước hết chúng ta update công cụ cài đặt yum bằng lệnh sau

# yum -y update

Ngoài ra chúng ta cũng cần kiểm tra trong repo của hệ điều hành đã có dòng lệnh exclude=postfix* như hình dưới đây hay chưa bằng lệnh sau:

# vi /etc/yum.repos.d/CentOS-Base.repo

Nếu chưa có thì thêm dòng này vào để quá trình download và cài đặt sẽ lấy bản postfix hỗ trợ mysql. 




3. Kiểm tra và cài đặt các gói mysql-devel, cyrus-sasl, dovecot,postfix 

Khi bạn cài đặt mysql có thể thiếu một số gói phần mềm cần thiết để kết nối postfix với mysql đó là mysql-develvà cyrus-sasl vì vậy chúng ta cần kiểm tra bằng các lệnh sau

# rpm -qa | grep mysql (kiểm tra xem có các gói dịch vụ nào đi kèm với mysql đã được cài đặt)
# rpm -qa | grep sasl (kiểm tra xem gói cyrus-sasl đã được cài đặt chưa)
# rpm -qa | grep dovecot
# rpm -qa | grep postfix

Nếu các gói trên đã có thì OK, còn nếu chưa có chúng ta dùng lệnh sau để cài đặt đồng loạt các gói này

# yum -y install mysql mysql-devel dovecot cyrus-sasl

Chúng ta cũng cần dùng lệnh sau đây để kiểm tra xem postfix đã được cài đặt cùng module mysql hay chưa

# postfix -m

Nếu nhìn thấy module mysql thì OK, còn nếu không thấy, chúng ta dùng lệnh yum remove postfix để gỡ bỏ bản postfix cũ và cài lại bản mới bằng lệnh sau

# yum --enablerepo=centosplus install postfix


4. gỡ bỏ các dịch vụ có xung đột và đặt chế độ tự khởi động cho postfix , dovecot

Hệ điều hành Centos có sẵn một số dịch vụ như sendmail, exim... chúng ta cần kiểm tra và tắt các dịch vụ này đi để tránh xung đột với postfix bằng cách sau

# service sendmail status
# service sendmail stop
# chkconfig sendmail off

Sau đó bật các dịch vụ mới cài đặt ở bước 3 lên

# service mysql start
# service dovecot start
# service postfix start
# chkconfig mysql on
# chkconfig dovecot on
# chkconfig postfix on

5. Đặt mật khẩu cho các user root của mysql 

Bạn cần lưu ý rằng, ngay sau khi cài đặt mysql, cơ sở dữ liệu này đã có sẵn một user có quyền tối cao. Đó là user root, trùng tên với user root của hệ thống nhưng khác là có password bằng trống. Vì vậy phải ngay lập tức đặt mật khẩu cho user này bằng lệnh sau:

# mysql_secure_installation

Khi thực hiện lệnh trên bạn sẽ nhận được 4 câu hỏi như sau:

- Set root password? [Y/n] (Đặt mật khẩu cho root )
- Remove anonymous users? [Y/n] (Xóa bỏ user vô danh)
- Disallow root login remotely? [Y/n] (Không cho phép root đăng nhập từ xa)
- Remove test database and access to it? [Y/n] (Xóa bỏ database test )

Bạn nên trả lời Y cho cả 4 câu hỏi trên để bịt các lỗ hổng bảo mật của mysql.


6. Tạo database mailserver để lưu các thông số domain, email account

Chúng ta dùng luôn user root vừa đặt mật khẩu trên để thực hiện các lệnh tạo database và table từ dòng lệnh như sau. Hoặc nếu bạn đã cài phpmyadmin thì có thể thao tác tạo database và table trên giao diện web.

# mysql -u root -p

sau khi gõ mật khẩu bạn sẽ được vào khu vực dòng lệnh của mysql với dấu nhắc như sau:

mysql >

Bạn gõ lệnh 

mysql> CREATE DATABASE mail

để tạo một database tên là mail. Sau đó tạo một user có quyền thao tác trên database này bằng lệnh sau:

mysql> USE mail;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON mail.* TO "mail_admin"@"localhost" IDENTIFIED BY "123#abc";
mysql> FLUSH PRIVILEGES;

Trong đó 123#abc là mật khẩu của user mail_admin. Bạn nên ghi lại mật khẩu này vì sau đó nó sẽ bị mã hóa trong cơ sở dữ liệu, không còn nhìn được ở dạng plain text nữa.

Lưu ý: 
- các lệnh của mysql đều phải kết thúc bằng dấu chấm phảy (;)
- các tham số in đậm và mầu đỏ ở trên là tùy biến, bạn có thể đặt tên database, và user, mật khẩu theo ý thích của mình, tuy nhiên nếu để test chúng ta nên dùng thống nhất các tên này để tránh phải sửa đổi nhiều ở các bước sau.


7. Tạo các table để quản lý domain và user ảo

Chúng ta cần tạo 4 table là domains, forwarding, transports, và users. table forwarding và transport để dùng cho các chức năng mở rộng sau này của email như forward, relay... Trong bài viết này chưa đề cập đến các chức năng đó nên có thể không cần tạo các table này. Chỉ cần có table domains và users là đủ.

mysql> CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain) );
mysql> CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source) );
mysql> CREATE TABLE transport ( domain varchar(128) NOT NULL default "", transport varchar(128) NOT NULL default "", UNIQUE KEY domain (domain) );
mysql> CREATE TABLE users (email varchar(80) NOT NULL, password varchar(20) NOT NULL, PRIMARY KEY (email) );
mysql> exit

Lưu ý các lệnh trên có thể thực hiện trong phpmyadmin nếu các bạn đã cài đặt gói dịch vụ này.

8. Thiết lập địa chỉ ip cho mysql server

# vi /etc/my.cnf

thêm vào một dòng như sau:

bind-address = 127.0.0.1

Ghi lại và khởi động lại dịch vụ mysql

# service mysql restart


9. tạo các file cấu hình cho postfix 

Chúng ta cần tạo 4 file cấu hình để postfix biết cách query vào cơ sở dữ liệu, lấy ra các giá trị như domain, forwarding, email account... Các bạn download cả 4 file ở đây sau đó upload lên thư mục /etc/postfix/, mở ra và sửa các tham số user, password và dbname như vừa tạo ở trên .

Sau đó thiết lập permissions và ownership cho các file này bằng các lệnh sau

# chmod o= /etc/postfix/mysql-virtual_*.cf
# chgrp postfix /etc/postfix/mysql-virtual_*.cf


Để có thể nhận email, chúng ta cần tạo một user và group có tên là vmail. Các virtual mailboxes của các email account được tạo ra sau này, sẽ được lưu trữ trong thư mục home của user này (/home/vmail)

# groupadd -g 5000 vmail
# useradd -g vmail -u 5000 vmail -d /home/vmail –m



10. cấu hình postfix

Các bạn tải file config.sh này về, sau đó upload vào thư mục /etc/postfix/ và sửa dòng đầu tiên của file cho đúng với hostname của các bạn, chmod sang 755 và chạy file để nó tự add các dòng trong config.sh vào file main.cf của postfix. 

# vi /etc/postfix/config.sh
# chmod 755 /etc/postfix/config.sh
# /etc/postfix/config.sh 


Tiếp theo chúng ta chỉnh sửa file master.cf, thêm dòng sau vào đoạn giữa fie, mục Interfaces to non-Postfix software

dovecot   unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}


Khởi động lại dịch vụ postfix

# service postfix restart


11. cấu hình dovecot

Các bạn tải file cấu hình dovecot.conf mới ở đây về, sau đó upload lên /etc. Lưu ý, nên backup lại một bản của file dovecot.conf cũ trước khi đè file mới lên. Mở file dovecot.conf ra và sửa dòng postmaster_address = postmaster@thuonghieuweb.com với tên miền của bạn.

Sau đó tạo 1 file dovecot-sql.conf  để móc vào database như sau:

# vi /etc/dovecot-sql.conf

     driver = mysql
     connect = host=127.0.0.1 dbname=mail user=mail_admin password=123#abc
     default_pass_scheme = CRYPT
     password_query = SELECT email as user, password FROM users WHERE email="%u";

Gán quyền truy cập cho file này như sau

 # chgrp dovecot /etc/dovecot-sql.conf
 # chmod o= /etc/dovecot-sql.conf


Khởi động lại dovecot

# service dovecot restart


12. kiểm tra kết quả các bước trên

Kiểm tra log để đảm bảo dịch vụ dovecot đã chạy và ko có lỗi gì

# tail -f /var/log/maillog

Nếu có 2 dòng sau là ok (xem thêm hình chụp)
: Dovecot v1.0.7 starting up
: mysql : Connected to 127.0.0.1 (mail)




Kiểm tra dịch vụ POP 3 gõ lệnh telnet localhost pop3 và nếu thấy thông báo +OK Dovecot ready là được

# telnet localhost pop3

Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is "^]".
+OK Dovecot ready.

Gõ quit để thoát.


13. tạo alias và kiểm tra sự hoạt động của postfix

Mở file aliases và thêm một dòng gán bí danh cho root như sau


# vi /etc/aliases

thêm dòng:  

root:     mail_admin

Sau đó chạy lại alias và khởi động lại postfix

# newaliases
# service postfix restart

Kiểm tra dịch vụ postfix bằng lệnh sau

# telnet localhost 25

Sau đó kiểm tra theo hình sau, nếu thấy giống như hình này là ok








14. Bắt đầu tạo các domain và email acc

Sau các bước trên có thể coi như phần ảo hóa và kết nối postfix với database đã xong chúng ta có thể tạo các domain và email gắn với domain được rồi. Bước tạo email account này có thể dùng phpmyadmin hoặc viết một script php để tạo, quản lý email cho nó chuyên nghiệp. Ở đây, với mục đích test chúng ta sẽ dùng dòng lệnh để tạo một domain tên là mailtest.com và một email tên là hai@mailtest.com

Trước tiên, chúng ta khai báo tên miền này vào file hosts để giả lập một bản ghi DNS

# vi /etc/hosts

Thêm dòng sau

127.0.0.1     mailtest.com

Sau đó bắt đầu khai báo domain này với user test trong cơ sở dữ liệu

# mysql -u root -p
mysql> USE mail;
mysql> INSERT INTO domains (domain) VALUES ("mailtest.com");
mysql> INSERT INTO users (email, password) VALUES ("hai@mailtest.com", ENCRYPT("123456"));
mysql> quit

Để account mail này có thể truy cập Imap hay Pop3 thì ta phải gởi 1 mail (welcome message) cho user đó bằng một tiện ích gửi mail sẵn có của linux, hãy gõ lệnh như sau

# mail -s "xin chao" hai@mailtest.com 

Gõ nội dung email ngay sau lệnh trên, sau đó bấm Ctrl-D 2 lần (lần thứ nhất nó hỏi cc để thêm địa chỉ cc, lần thứ 2 để kết thúc và gửi mail)

Sau đó vào thư mục /home/vmail để kiểm tra, nếu mọi thứ OK bạn sẽ nhìn thấy thư mục mailtest.com và trong mailtest.com lại có thư mục hai chứa các email của hai@mailtest.com. Tóm lại nếu bạn vào được đường dẫn này/home/vmail/mailtest.com/hai/Maildir/new và thấy một file thì dó chính là email vừa được gửi bằng tiện ích nói trên và như vậy postfix đã hoạt động tốt.



15. check mail với outlook:

Để có thể check mail từ một máy tính bất kỳ dùng outlook, bạn cần phải có một domain và có quyền trỏ bản ghi cho domain này. Giả sử bạn có domain là thuonghieuweb.com, hãy trỏ các bản ghi cần thiết cho email tới máy địa chỉ của máy chủ nói trên (bản ghi A và bản ghi MX) việc trỏ bản ghi không đề cập đến trong này. Bạn hãy làm sao để sau khi trỏ bản ghi bạn sẽ phải ping thành công đến địa chỉ mail.thuonghieuweb.com và thấy là nó đã được trỏ đến ip của máy chủ này.


Sau đó bạn lặp lại bước 14 (nhưng bỏ qua đoạn sửa file hosts vì đã trỏ tên miền từ DNS server rồi) để tạo 2 bản ghi domain = thuonghieuweb.com và user = tenban@thuonghieuweb.com.

Mở outlook trên máy của bạn và khai báo một account mới như hình sau:







Sau khi khai báo các tham số như hình trên, bấm vào test account setting nếu cả 2 bước gửi, nhận mail đều xanh là OK. còn nếu không hãy xem thông báo lỗi là gì để có thể kiểm tra lại. 

Outlook làm việc rất tốt với hệ thống mail postfix ảo hóa này tuy nhiên trong một số trường hợp, chẳng hạn đi công tác xa, chúng ta cần check mail bằng webmail thì cần cài đặt thêm một dịch vụ nữa. Hướng dẫn cài đặt webmail có trong bài viết Hướng dẫn cài đặt dịch vụ squirrel mail gắn với postfix, các bạn có thể tiếp tục tìm hiểu thêm.

Chúc các bạn thành công