ผลต่างระหว่างรุ่นของ "คู่มือการพัฒนา EAI Mail Server บน Ubuntu 20"

จาก Wiki_EAI
ไปยังการนำทาง ไปยังการค้นหา
แถว 436: แถว 436:
 
  manpage_directory = null
 
  manpage_directory = null
 
  mydestination = localhost, localhost.localdomain
 
  mydestination = localhost, localhost.localdomain
  mydomain = mailthaiXX.in.th
+
  mydomain = mailthai99.in.th
  myhostname = mail.mailthaiXX.in.th
+
  myhostname = mail.mailthai99.in.th
 
  mynetworks = 127.0.0.1
 
  mynetworks = 127.0.0.1
 
  myorigin = $mydomain
 
  myorigin = $mydomain
แถว 454: แถว 454:
 
  smtpd_sasl_type = dovecot
 
  smtpd_sasl_type = dovecot
 
  smtpd_tls_auth_only = yes
 
  smtpd_tls_auth_only = yes
  smtpd_tls_cert_file = /etc/ssl/private/mailthaiXX.crt
+
  smtpd_tls_cert_file = /etc/ssl/private/mailthai99.crt
  smtpd_tls_key_file = /etc/ssl/private/mailthaiXX.key
+
  smtpd_tls_key_file = /etc/ssl/private/mailthai99.key
 
  smtpd_tls_loglevel = 1
 
  smtpd_tls_loglevel = 1
 
  smtpd_use_tls = yes
 
  smtpd_use_tls = yes
 
  smtputf8_enable = yes
 
  smtputf8_enable = yes
 
  unknown_local_recipient_reject_code = 550
 
  unknown_local_recipient_reject_code = 550
  virtual_alias_domains = จดหมายXX.ไทย
+
  virtual_alias_domains = จดหมาย99.ไทย
 
  virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
 
  virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
 
  virtual_gid_maps = static:5000
 
  virtual_gid_maps = static:5000

รุ่นแก้ไขเมื่อ 14:13, 14 มกราคม 2565


EAI Email Server

คู่มือนี้ได้จัดทำขึ้นมาเพื่อทดสอบและเป็นแนวทางการติดตั้ง Email Server ให้สามารถรับส่ง email address ภาษาไทย หรือ Email Address Internationalization (EAI) โดยใช้ Opensource Software เพื่อหาวิธีการที่สามารถใช้งานได้ และสามารถนำไปพัฒนาต่อยอดได้

Software ที่นำมาทดสอบ

  1. Ubuntu 20.04.3 LTS
  2. Postfix 3.4.13
  3. Dovecot 2.3.7.2
  4. MySQL 8.0.27
  5. Roundcube 1.4.4 [ดาวน์โหลด] [tar]

EAI Workshop Authentication

  1. [EAI Workshop]
    • user: eai
    • pass: eai+ws@thnic
  2. โดเมนที่ใช้เป็นตัวอย่างใน workshop
    • English : mailthai99.in.th
    • ภาษาไทย : จดหมาย99.ไทย
    • (Punycode) : xn--15-6qi0c9cg5dxc.xn--o3cw4h
  3. Ubuntu Server:
    • User: user
    • Pass: userpass
  4. Passphase mailthai.key
    • Key: mailthai.key, mailthai.crt, mailthai.csr
    • Pass: mailpass
  5. MySQL
    • User: root
    • Pass: rootpass
  6. User: dbuser
    • Pass: dbuserpass
    • DB: eaimail
  7. User: rcmail
    • Pass: rcmailpass
    • DB: roundcubemail
  8. Roundcube
    • User: thai@mailthaiXX.in.th
    • Pass: thaipass
    • User: ไทย@จดหมายXX.ไทย
    • Pass: thaipass

การติดตั้ง EAI Email Server

ตั้งค่า hostname

1. เข้าใช้งานเครื่องด้วย สิทธิ์ root

sudo su

รหัสผ่าน : userpass

2. อัพเดตแพคเกจของ mail serverให้เป็นเวอร์ชั่นล่าสุด ด้วยคำสั่ง

apt update && apt -y upgrade

3. กำหนดชื่อ hostname และตรวจสอบด้วยคำสั่ง

  • กำหนดชื่อ hostname
hostnamectl set-hostname mail.mailthai99.in.th 

ตัวอย่าง เช่น mail.mailthai99.in.th

  • ตรวจสอบ hostname
hostnamectl

ตัวอย่าง

Static hostname: mail.mailthai99.in.th 

4. เพิ่มค่า hostname ชื่อเดียวกับชื่อที่กำหนดข้างต้นในไฟล์ hosts

# nano /etc/hosts

ตัวอย่าง เช่น mail.mailthai99.in.th

ตัวอย่างไฟล์ hosts

127.0.0.1   	localhost mail.mailthai99.in.th

5. ตั้งค่าเวลาของ Mail Server และตรวจสอบ

  • ตั้งค่าเวลาของ Mail Server
 timedatectl set-timezone "Asia/Bangkok" 
  • ตรวจสอบเวลาของ Mail Server
 timedatectl  status

ตัวอย่าง

Local time: Fri 2022-01-14 12:06:03 +07
Universal time: Fri 2022-01-14 05:06:03 UTC
RTC time: Fri 2022-01-14 05:06:03    
Time zone: Asia/Bangkok (+07, +0700)
System clock synchronized: yes                        
NTP service: active                     
RTC in local TZ: no

ตั้งค่า firewall

1. เปิด firewall service และตรวจสอบสถานะ ด้วยคำสั่ง

  • เปิด firewall service
ufw enable

ตัวอย่าง

Command may disrupt existing ssh connections. Proceed with operation (y|n)? y # ให้ตอบ y
Firewall is active and enabled on system startup

2. เปิด firewall services ของ email server

ufw allow in ssh
ufw allow http
ufw allow https
ufw allow smtp
ufw allow smtps
ufw allow imap
ufw allow imaps
ufw allow mysql

ตัวอย่าง

Rule added
Rule added (v6)

3. ตรวจสอบ firewall services ที่เปิดอยู่

ufw status

ตัวอย่าง

Status: active
To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
25/tcp                     ALLOW       Anywhere                  
465/tcp                    ALLOW       Anywhere                  
143/tcp                    ALLOW       Anywhere                  
993/tcp                    ALLOW       Anywhere                  
3306/tcp                   ALLOW       Anywhere               
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
25/tcp (v6)                ALLOW       Anywhere (v6)             
465/tcp (v6)               ALLOW       Anywhere (v6)             
143/tcp (v6)               ALLOW       Anywhere (v6)             
993/tcp (v6)               ALLOW       Anywhere (v6)
3306/tcp (v6)              ALLOW       Anywhere (v6)  

*ref: configure firewall services for mail server (https://linuxconfig.org/ubuntu-20-04-open-http-port-80-and-https-port-443-with-ufw)

สร้าง SSL Cert

  • เป็นขั้นตอนการสร้าง SSL cert/key เตรียมไว้ใช้งานกับ postfix และ dovecot
  • ในเครื่อง workshop ได้เตรียม cert mailthai.crt, mailthai.key, mailthai.csr ไว้แล้ว

1. สร้าง private key

cd /etc/ssl/private
openssl genrsa -aes128 2048 > mailthaiXX.key

ตัวอย่าง เช่น mailthai99.key

Generating RSA private key, 2048 bit long modulus (2 primes)
........+++++
..+++++
e is 65537 (0x010001)
Enter pass phrase:                      # mailpass
Verifying - Enter pass phrase:          # mailpass

2. ถอด Passphase ออกจาก private key ที่สร้างขึ้น

openssl rsa -in mailthaiXX.key -out mailthaiXX.key

ตัวอย่าง เช่น mailthai99.key

Enter pass phrase for mailthai99.key:  # mailpass
writing RSA key

3. สร้าง csr

openssl req -utf8 -new -key mailthaiXX.key -out mailthaiXX.csr

ตัวอย่างเช่น mailthai99.key, mailthai99.csr

Country Name (2 letter code) [AU]:TH
State or Province Name (full name) [Some-State]:Bangkok
Locality Name (eg, city) []:Dusit
Organization Name (eg, company) [Internet Widgits Pty Ltd]:EAI
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:mail.mailthai99.in.th
Email Address []:postmaster@mailthai99.in.th

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:			# Enter
An optional company name []:			# Enter


4. ทำการ generate key

openssl x509 -in mailthaiXX.csr -out mailthaiXX.crt -req -signkey mailthaiXX.key -days 365

ตัวอย่างเช่น mailthai99.key, mailthai99.csr, mailthai99.crt

 Signature ok
subject=C = TH, ST = Bangkok, L = Dusit, O = EAI, OU = IT, CN = mail.mailthaiXX.in.th, emailAddress = postmaster@mailthai99.in.th 
Getting Private key

5. ตรวจสอบ key ที่สร้างขึ้น

ls

ตัวอย่าง

mailthai99.crt  mailthai99.csr  mailthai99.key

ติดตั้ง MySQL, Postfix, Dovecot

1. ติดตั้ง MySQL, Postfix , Dovecot และ packet ที่จำเป็นสำหรับ email sever ด้วยคำสั่ง

apt -y install postfix postfix-mysql dovecot-core dovecot-mysql dovecot-imapd dovecot-pop3d dovecot-lmtpd  mysql-server

2. สั่ง start service ของ MySQL, Postfix, Dovecot ด้วยคำสั่ง

systemctl start mysql && systemctl start postfix && systemctl start dovecot

3. ดูสถานะการทำงานของ MySQL, Postfix, Dovecot

systemctl status mysql
systemctl status postfix
systemctl status dovecot

ตัวอย่าง

Active: active (running) since Wed 2022-01-05 14:42:29 +07; 33min ago

ตั้งค่า MySQL

1. ตั้งค่า UTF8 ให้กับ mysql เพื่อให้รองรับภาษาไทย โดยเพิ่มคำสั่งด้านล่างลงในไฟล์ mysqld.cnf ใต้ [mysqld]

nano /etc/mysql/mysql.conf.d/mysqld.cnf

ตัวอย่าง ไฟล์ mysqld.cnf

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake
bind-address=127.0.0.1
#  
# * Basic Settings
#
user            = mysql

2. สั่ง restart MySQL

systemctl restart mysql

3. ตั้งค่าความปลอดภัยการเข้าใช้งาน MySQL โดยจะมีการตั้งค่า password ของ user root เพื่อ login เข้าใช้งาน (* กำหนดเพียง password ใหม่ ในส่วนอื่นๆ ให้ enter ผ่านได้เลย)

mysql_secure_installation

ตัวอย่าง

Please set the password for root here.

New password:                    # rootpass

Re-enter new password:           # rootpass

4. Login เข้าใช้งาน MySQL โดยใช้ password ของ user root ที่ได้ตั้งค่าไว้

mysql -u root -p

รหัสผ่าน rootpass

5. สร้าง Database เพื่อใช้เก็บข้อมูลของการใช้งาน Email Server

  • สร้าง Database
CREATE DATABASE eaimail;

ตัวอย่าง

Query OK, 1 row affected (0.02 sec)
  • เลือกใช้งาน Database eaimail
USE eaimail;

ตัวอย่าง

Database changed

6. สร้าง user พร้อมกำหนดสิทธิ์การใช้งานให้กับ eaimail database ที่สร้างขึ้นมา

CREATE USER dbuser@localhost IDENTIFIED BY 'dbuserpass';
GRANT SELECT, INSERT, UPDATE, DELETE ON eaimail.* TO dbuser@localhost;
FLUSH PRIVILEGES;

7. ตรวจสอบ user ที่สร้างขึ้นใหม่โดย

SHOW GRANTS FOR dbuser@localhost;

ตัวอย่าง

+----------------------------------------------------------------------------------------------------------------+
| Grants for dbuser@localhost                                                 			|
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `dbuser`@`localhost`                                  		|
| GRANT SELECT, INSERT, UPDATE, DELETE ON `eaimail`.* TO `dbuser`@`localhost` |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

8. สร้างตารางใช้เก็บค่า domains, forwardings สำหรับ forward อีเมลภาษาไทยไปภาษาอังกฤษ, users เพื่อใช้สร้าง mailbox

CREATE TABLE domains (domain varchar(50) NOT NULL, PRIMARY KEY (domain));
CREATE TABLE forwardings (source varchar(80) NOT NULL, destination TEXT NOT NULL, PRIMARY KEY (source));
CREATE TABLE users (email varchar(80) NOT NULL, password varchar(256) NOT NULL, PRIMARY KEY (email));

9. เพิ่มข้อมูล Domain และ Email Account ทั้งภาษาอังกฤษ และภาษาไทย เพื่อเริ่มใช้งาน

INSERT INTO domains (domain) VALUES ('mailthaiXX.in.th');
INSERT INTO domains (domain) VALUES ('จดหมายXX.ไทย');
INSERT INTO users (email, password) VALUES ('thai@mailthaiXX.in.th', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');
INSERT INTO users (email, password) VALUES ('ไทย@จดหมายXX.ไทย', '$6$7GQtdsDwSjzB7EAm$0.W6ZBcGEylrQMHKv1j7PZd/6EPoXAYClNhxxs3V2mG/NrHKDsycOTWwETniwI9O/OUeQkqBT5KPfNkDUb8hk/');

ตัวอย่าง เช่น mailthai99.in.th, จดหมาย99.ไทย, thai@mailthai99.in.th, ไทย@จดหมาย99.ไทย

หมายเหตุ สร้างรหัสผ่านด้วยคำสั่ง doveadm pw -s SHA512-CRYPT

10. กำหนด Email ที่เข้ามายัง Email Account ภาษาไทย ให้ forward เข้า Email-Account ภาษาอังกฤษ เนื่องจากระบบไม่สามารถสร้าง mailbox เป็น ภาษาไทยได้

INSERT INTO forwardings (source,destination) VALUES ('ไทย@จดหมายXX.ไทย','thai@mailthaiXX.in.th');
quit

ตั้งค่า postfix

สร้างไฟล์เชื่อมต่อระหว่าง Postfix และ MySQL

  • สร้างไฟล์ script เพื่อใช้สำหรับเรียกค่า domain จาก Database
nano /etc/postfix/mysql-virtual_domains.cf

ตัวอย่างไฟล์

user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT domain FROM domains WHERE domain='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ forward email
nano /etc/postfix/mysql-virtual_forwardings.cf

ตัวอย่างไฟล์

 user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT destination FROM forwardings WHERE source='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ สร้าง mailbox และใช้ login
nano /etc/postfix/mysql-virtual_mailboxes.cf

ตัวอย่างไฟล์

user = dbuser 
password = dbuserpass
dbname = eaimail
query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM users WHERE email='%s'
hosts = 127.0.0.1
  • สร้างไฟล์ script เพื่อใช้สำหรับ ส่งต่อ email ทั้งหมด ไปยัง smtp server อื่น ซึ่งใช้ในกรณีที่ host เป็น IP Address
nano /etc/postfix/mysql-virtual_email2email.cf

ตัวอย่างไฟล์

user = dbuser
password = dbuserpass
dbname = eaimail
query = SELECT email FROM users WHERE email='%s'
hosts = 127.0.0.1
  • กำหนดสิทธิ์ให้กับ script ที่สร้างขึ้น
chmod 640 /etc/postfix/mysql-virtual_*.cf && chgrp postfix /etc/postfix/mysql-virtual_*.cf

ตัวอย่าง

-rw-r-----   1 root postfix   124 Jan  7 23:36 mysql-virtual_domains.cf
-rw-r-----   1 root postfix   120 Jan  7 23:39 mysql-virtual_email2email.cf
-rw-r-----   1 root postfix   133 Jan  7 23:37 mysql-virtual_forwardings.cf
-rw-r-----   1 root postfix   190 Jan  7 23:38 mysql-virtual_mailboxes.cf


สร้าง user vmail สำหรับอ่าน/เขียน mailbox

  • สร้าง group เพื่อใช้กับ mailbox
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /home/vmail -m

config การทำงานของ postfix ด้วยคำสั่ง postconf (คำสั่งนี้จะไปแก้ไขไฟล์ "/etc/postfix/main.cf")

postconf "myhostname=mail.mailthaiXX.in.th" && \
postconf "mydomain=mailthaiXX.in.th" && \ 
postconf "virtual_alias_domains=จดหมายXX.ไทย" && \
postconf "smtpd_tls_cert_file=/etc/ssl/private/mailthaiXX.crt" && \
postconf "smtpd_tls_key_file=/etc/ssl/private/mailthaiXX.key" && \
postconf "myorigin=\$mydomain" && \
postconf "inet_interfaces=all" && \
postconf "inet_protocols=ipv4" && \
postconf "mydestination=localhost, localhost.localdomain" && \
postconf "mynetworks=127.0.0.1" && \
postconf "readme_directory=no" && \
postconf "shlib_directory=no" && \
postconf "smtpd_sasl_auth_enable=yes" && \
postconf "smtpd_sasl_type=dovecot" && \
postconf "smtpd_sasl_path=private/auth" && \
postconf "smtpd_sasl_authenticated_header=yes" && \
postconf "broken_sasl_auth_clients=yes" && \
postconf "smtpd_use_tls=yes" && \
postconf "smtpd_recipient_restrictions=permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination" && \
postconf "smtpd_tls_auth_only=yes" && \
postconf "smtpd_tls_loglevel=1" && \
postconf "virtual_mailbox_base=/home/vmail" && \
postconf "virtual_mailbox_maps=proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf" && \
postconf "virtual_mailbox_domains=proxy:mysql:/etc/postfix/mysql-virtual_domains.cf" && \
postconf "virtual_alias_maps=proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf" && \
postconf "virtual_uid_maps=static:5000" && \
postconf "virtual_gid_maps=static:5000" && \
postconf "virtual_transport=dovecot" && \
postconf "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" && \
postconf "smtputf8_enable=yes" && \
postconf "newaliases_path=null" && \
postconf "mailq_path=null" && \
postconf "setgid_group=postdrop" && \
postconf "html_directory=null" && \
postconf "manpage_directory = null" &&  \
postconf "sample_directory = null" && \
postconf "sendmail_path=null"
  • ตัวอย่างไฟล์
broken_sasl_auth_clients = yes
command_directory = /usr/sbin
compatibility_level = 2
daemon_directory = /usr/lib/postfix/sbin
data_directory = /var/lib/postfix
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
html_directory = null
inet_interfaces = all
inet_protocols = ipv4 
mailq_path = null 
manpage_directory = null
mydestination = localhost, localhost.localdomain
mydomain = mailthai99.in.th
myhostname = mail.mailthai99.in.th
mynetworks = 127.0.0.1
myorigin = $mydomain
newaliases_path = null
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
readme_directory = no
sample_directory = null
sendmail_path = null
setgid_group = postdrop
shlib_directory = no
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
smtpd_sasl_path = private/auth
smtpd_sasl_type = dovecot
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/ssl/private/mailthai99.crt
smtpd_tls_key_file = /etc/ssl/private/mailthai99.key
smtpd_tls_loglevel = 1
smtpd_use_tls = yes
smtputf8_enable = yes
unknown_local_recipient_reject_code = 550
virtual_alias_domains = จดหมาย99.ไทย
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, mysql:/etc/postfix/mysql-virtual_email2email.cf
virtual_gid_maps = static:5000
virtual_mailbox_base = /home/vmail
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf
virtual_transport = dovecot
virtual_uid_maps = static:5000

แก้ไขไฟล์ "/etc/postfix/master.cf"

# postconf -M smtps/inet="smtps inet n - - - - smtpd"
# postconf -P "smtps/inet/syslog_name=postfix/smtps"
# postconf -P "smtps/inet/smtpd_tls_wrappermode=yes"
# postconf -P "smtps/inet/smtpd_sasl_auth_enable=yes"
# postconf -P "smtps/inet/smtpd_client_restrictions=permit_sasl_authenticated,reject"
# postconf -P "smtps/inet/smtpd_relay_restrictions=permit_sasl_authenticated,reject"
# postconf -P "smtps/inet/milter_macro_daemon_name=ORIGINATING"
# postconf -vM dovecot/unix="dovecot   unix  -       n       n       -       -       pipe flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}"
  • ตัวอย่างไฟล์
smtps inet n - - - - smtpd
   -o syslog_name=postfix/smtps
   -o smtpd_tls_wrappermode=yes 
   -o smtpd_sasl_auth_enable=yes 
   -o smtpd_client_restrictions=permit_sasl_authenticated,reject 
   -o smtpd_relay_restrictions=permit_sasl_authenticated,reject 
   -o milter_macro_daemon_name=ORIGINATING
dovecot   unix  -       n       n       -       -       pipe
    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}
  • restart postfix
# systemctl restart postfix

7. ตั้งค่า dovecot

สร้างไฟล์เชื่อมต่อระหว่าง dovecot และ mariadb

  • สร้าง script เพื่อให้ dovecot ใช้งานกับ SQL ที่เราได้กำหนดก่อนหน้านี้
# vi /etc/dovecot/dovecot-sql.conf.ext
driver = mysql
connect = host=127.0.0.1 dbname=eaimail user=yourdbuser password=yourdbpassword
default_pass_scheme = CRYPT
password_query = SELECT email as user, password FROM users WHERE email='%u';
  • กำหนดสิทธิ์อนุญาตให้ user ที่อยู่ใน dovecot group ใช้งาน
# chmod 640 /etc/dovecot/dovecot-sql.conf.ext && chgrp dovecot /etc/dovecot/dovecot-sql.conf.ext

แก้ไขไฟล์ "/etc/dovecot/dovecot.conf"

  • Backup ค่า config เดิมไว่ก่อน
# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf-backup
  • ตั้งค่า config dovecot โดยสร้างไฟล์ให้ และใส่คำสั่งตามด้านล่าง
# vi /etc/dovecot/dovecot.conf

ตัวอย่าง

protocols = imap pop3 lmtp
log_timestamp = "%Y-%m-%d %H:%M:%S "
mail_location = maildir:/home/vmail/%d/%n/Maildir

ssl_cert = </etc/pki/tls/certs/eai.crt
ssl_key = </etc/pki/tls/certs/eai.key
ssl_dh_parameters_length = 2048 
ssl_cipher_list = ALL:!LOW:!SSLv2:!EXP:!aNULL
namespace {
    type = private
    separator = .
    prefix = INBOX.
    inbox = yes
}
service auth {
    unix_listener auth-master {
        mode = 0600
        user = vmail
    }
    unix_listener /var/spool/postfix/private/auth {
        mode = 0666
        user = postfix
        group = postfix
    }
user = root
}
auth_mechanisms = plain login
service auth-worker {
    user = root
}
protocol lda {
    log_path = /home/vmail/dovecot-deliver.log
    auth_socket_path = /var/run/dovecot/auth-master
    postmaster_address = postmaster@eai.in.th
}
protocol pop3 {
    pop3_uidl_format = %08Xu%08Xv
        pop3_client_workarounds = outlook-no-nuls oe-ns-eoh
}
passdb {
    driver = sql
    args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
    driver = static
    args = uid=5000 gid=5000 home=/home/vmail/%d/%n allow_all_users=yes
}
  • สั่ง restart dovecot
# systemctl restart dovecot

8. ทดสอบการทำงานของ EAI Email Server

1. ทดสอบ imap

# telnet localhost imap

2. ได้ผลตามด้านล่าง และ ใช้คำสั่ง quit เพื่ออแกจาก telnet

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

3. แก้ใขไฟล์ /etc/aliases โดยเพิ่มบรรทัดตามด้านล่าง

# vi /etc/aliases
mailer-daemon:	postmaster
postmaster:	root
root: postmaster@eai.in.th

4 สั่ง update aliases และ restart postfix

# newaliases
# systemctl restart postfix

5. ทดสอบ smtp server

# telnet localhost 25
# ehlo localhost

6. จะได้ผลลัพธ์ตามด้านล่าง

250-mail.eai.in.th
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8

7. ติดตั้ง mailx และทดสอบส่ง email

# yum install mailx
# mailx thai@eai.in.th (ใส่ subject แล้ว enter ตามด้วยเนื้อหา และใช้ Ctrl+D เพื่อส่งออก)

8. ตรวจสอบ log ของการส่ง Email : /var/log/maillog ซึ้งจะได้ผลตามด้านล่าง

Nov 13 15:19:07 mail postfix/pickup[31954]: E9AB020199E5: uid=0 from=<root>
Nov 13 15:19:07 mail postfix/cleanup[1938]: E9AB020199E5: message-id=<20171113081907.E9AB020199E5@mail.eai.in.th>
Nov 13 15:19:07 mail postfix/qmgr[21141]: E9AB020199E5: from=<root@eai.in.th>, size=434, nrcpt=1 (queue active)
Nov 13 15:19:08 mail postfix/pipe[1946]: E9AB020199E5: to=<tanagan@eai.in.th>, relay=dovecot, delay=0.09, delays=0.04/0.01/0/0.03, dsn=2.0.0, status=sent (delivered via dovecot service)
Nov 13 15:19:08 mail postfix/qmgr[21141]: E9AB020199E5: removed

9. ตรวจสอบ log ผลการส่งของ dovecot : /home/vmail/dovecot-deliver.log ซึ่งจะได้ผลตามด้านล่าง

2017-11-13 15:19:08 lda(tanagan@eai.in.th): Info: msgid=<20171113081907.E9AB020199E5@mail.eai.in.th>: saved mail to INBOX

10. ตรวจสอบ mailbox เมื่อมีการรับส่ง-ส่ง Email จะสร้างขึ้นโดย dovecot ซึ่งจะได้ผลตามภาพ

# cd /home/vmail/eai.in.th/thai/Maildir
# find
.
./dovecot-uidlist
./cur
./new
./new/1285609582.P6115Q0M368794.li172-137
./dovecot.index
./dovecot.index.log
./tmp

11. ตรวจสอบ mailbox โดยใช้ mail client คือ mutt เพื่อดู email ภายใน mailbox

# yum install mutt
# mutt -f .

12. ทดสอบการส่งเมลในรูปแบบ eai

# telnet localhost 25
# EHLO localhost
# MAIL FROM: ธนากานต์@อีเอไอ.ไทย SMTPUTF8		
# RCPT TO: ธนากานต์@คน.ไทย
# data
# Subject: eai test
# เทสอีเมลส์ภาษาไทย
# .
  • ใช้เครื่องหมาย “.” เพื่อจบข้อความและส่งข้อความ
  • กด Ctrl+] เพื่อออก

9. ติดตั้ง roundcube webmail

ติดตั้ง apache php

  • ติดตั้ง repo ของ php 7.4 เพิ่มเติม และเปิดให้สามารถติดตั้ง php 7.4 ได้
# yum -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm 
# yum module reset php && yum module enable php:remi-7.4 -y
  • ใช้คำสั่งติดตั้ง php, php library และ apache web server
# yum -y install httpd php php-common php-devel php-imap php-imagick php-json php-xml php-mbstring php-pear php-pdo php-intl php-ldap php-gd php-zip php-pdo_mysql php-curl php-bz2 php-gmp
  • ใช้คำสั่ง start service apache และ php และให้เริ่มทำงานโดยอัติโนมัติทุกครั้งที่เปิดเครื่อง
# systemctl start httpd && systemctl start php-fpm
# ​systemctl enable httpd && systemctl enable php-fpm
  • ตรวจสอบสถานะการทำงานของ apache และ
# systemctl status httpd 
# systemctl status php-fpm

ตั้งค่า php

  • ตั้งค่า timezone (เราจะตั้งค่าเฉพาะส่วนที่จำเป็นต่อการใช้งาน roundcube)
# vi /etc/php.ini 
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone 
date.timezone = Asia/Bangkok

ตั้งค่า mariadb database

  • สร้าง database roundcubemail โดยกำหนด username=rcmail, password=passrc
# mysql -u root -p
# CREATE DATABASE roundcubemail CHARACTER SET utf8 COLLATE utf8_general_ci;
# GRANT ALL PRIVILEGES ON roundcubemail.* TO rcmail@localhost IDENTIFIED BY 'passrc';
# FLUSH PRIVILEGES;

ตั้งค่า roundcube

# cd /var/www/html/
# wget https://github.com/roundcube/roundcubemail/releases/download/1.4.4/roundcubemail-1.4.4-complete.tar.gz
# tar zvfx /var/www/html/roundcubemail-1.4.4-complete.tar.gz 
# mv /var/www/html/roundcubemail-1.4.4 /var/www/html/roundcubemail
# cp /var/www/html/roundcubemail/config/config.inc.php.sample /var/www/html/roundcubemail/config/config.inc.php
# chown -R apache:apache /var/www/html/roundcubemail

สร้าง virtualhost

# vi /etc/httpd/conf.d/rcmail.conf
<VirtualHost *:80>
    ServerName mail.eai.in.th
    ServerAdmin webmaster@eai.in.th
    DocumentRoot /var/www/html/roundcubemail
    <Directory /var/www/html/roundcubemail>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
    ErrorLog /var/log/httpd/mail.eai.in.th-error.log
    CustomLog /var/log/httpd/mail.eai.in.th-access.log combined
</VirtualHost>
# systemctl restart httpd
  • ทดสอบเข้าเว็บโซต์
http://mail.eai.in.th/installer/
  • ตั้งค่าเชื่อมต่อ database
host = localhost
dbname =roundcubemail
user = rcmail
password = passrc
db_prefix = rc_
  • ตั้งค่า IMAP
ssl://mail.eai.in.th 
993 
  • ตั้งค่า SMTP
ssl://mail.eai.in.th 
465
  • user, pass ของ eai email
thai@eai.in.th และ ไทย@อีเอไอ.ไทย
thaiuserpassword
  • การใช้ ssl แบบ self-sign อาจจะทำให้ roundcube ไม่สามารถเชื่อมต่อ mail server ได้ แก้ปัญหาโดยการ config เพิ่มเติม ดังนี้
# vi /var/www/html/roundcubemail/config/defaults.inc.php
$config['imap_conn_options'] = array(
  'ssl'         => array(
     'verify_peer'      => false,
     'verify_peer_name' => false,
  ),
);
 
$config['smtp_conn_options'] = array(
  'ssl'         => array(
     'verify_peer'      => false,
     'verify_peer_name' => false,
  ),
);