Tutorial on setting up an email server in Centos 6 (Postfix, Dovecot, and MySQL)

Source: Internet
Author: User
Tags aliases auth bind chmod imap dovecot email account vmail

Postfix is an attempt by Wietse Venema to provide alternatives to the most widely used sendmail. Postfix tries to be faster, easier to manage, and safer, while maintaining sufficient compatibility with sendmail.


This tutorial aims to configure postfix, provide IMAP/POP3 services with Dovecot, and store data with mysql.
Log on to the server host (ssh root @ domain name | IP address)
Install the required package


1. Update the installation source first.


Yum update
2. The Postfix that comes with Centos6 does not support mysql, so to install it from the CentOS Plus library, here you need to modify/etc/yum. repos. d/CentOS-Base.repo to add exceptions. Add the following exceptions under the [base] and [updates] of this file.
File:/etc/yum. repos. d/CentOS-Base.rep
[Base]
Name = CentOS-$ releasever-Base
Exclude = postfix
# Released updates
[Updates]
Name = CentOS-$ releasever-Updates
Exclude = postfix
3. Install the required package
Yum -- enablerepo = centosplus install postfix
Yum install dovecot mysql-server dovecot-mysql
First install the postfix email server in the centosplus database, and then install the dovecot and mysql servers.
Configure mysql
1. Configure mysql to start automatically, and then start mysql
Chkconfig mysqld on
Service mysqld start
2. run mysql_secure_installation to set the mysql root password, delete anonymous accounts, cancel root remote logon, delete the test database and access permissions to the test database, and refresh the authorization table to make the modification take effect (enter y/ n ).
Mysql_secure_installation
3. Log on to mysql
Mysql-u root-p
4. Create a mail database.
Create database mail;
USE mail;
5. Create a new mail administrator account mail_admin and grant it the permissions in the mail database. The password is mail_admin_password. Remember to change the password to your own 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;
6. Create a virtual domain name table, email forwarding table, user table, and transmission table respectively, and enter quit to exit the SQL statement.
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 (20) not null, primary key (email ));
Create table transport (domain varchar (128) not null default '', transport varchar (128) not null default'', unique key domain (domain ));
Quit
 
7. Edit/etc/my. cnf and bind mysql to localhost (127.0.0.1 ).
File:/etc/my. cnf
Bind-address = 127.0.0.1
This address is used for the communication between postfix and the mysql database server.
8. Restart mysql
Service mysql restart
 
Configure postfix
The password of mail_admin for mysql users in the following configuration is mail_admin_password. If the password has been changed, replace the following configuration.
1. Create/etc/postfix/mysql-virtual_domains.cf as the virtual domain name Profile for postfix.
Files:/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
2. Create a new/etc/postfix/mysql-virtual_forwardings.cf as a virtual forwarding file for postfix
Files:/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
3. Create/etc/postfix/mysql-virtual_mailboxes.cf as the postfix virtual mailbox profile
Files:/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
4. Create a new/etc/postfix/mysql-virtual_email2email.cf as a virtual right-click ing file for postfix
Files:/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
5. Set the correct permissions and owner for these files.
Chmod o =/etc/postfix/mysql-virtual _ *. cf
Chgrp postfix/etc/postfix/mysql-virtual _ *. cf
6. Create a new user and group for mail processing. All virtual mailboxes are stored in the home directory of the user.
Groupadd-g 5000 vmail
Useradd-g vmail-u 5000 vmail-d/home/vmail-m
7. Enter the command to complete all postfix configurations. Note that you must replace server.example.com with your second-level domain name. domain name. The domain name is a self-purchased domain name. For example, if my server host name is jlyblog, the domain name purchased on HiChina is jlyblog.com, and the second-level domain name is set to mail, it is replaced with mail.jlyblog.com.
Postconf-e 'myhostname = server.example.com'
Postconf-e 'mydestination = $ myhostname, localhost, localhost. localdomain'
Postconf-e 'mynetworks = 127.0.0.0/8'
Postconf-e 'inet _ interfaces = all'
Postconf-e 'message _ size_limit = 100'
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: 100'
Postconf-e 'virtual _ gid_maps = static: 100'
Postconf-e 'smtpd _ sasl_type = dovecot'
Postconf-e 'smtpd _ sasl_path = private/auth'
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/pki/dovecot/certs/dovecot. Pem'
Postconf-e 'smtpd _ tls_key_file = </etc/pki/dovecot/private/dovecot. Pem'
Postconf-e 'virtual _ create_maildirsize = yes'
Postconf-e 'virtual _ maildir_extended = yes'
Postconf-e 'proxy _ read_maps = $ region $ mydestination $ virtual_alias_maps $ region $ virtual_mailbox_maps $ region $ canonical_maps $ region $ relocated_maps $ transport_maps $ mynetworks $ region'
Postconf-e 'virtual _ transport = doccot'
Postconf-e 'dovecot _ destination_recipient_limit = 1'
8. Edit the/etc/postfix/master. cf file and add the docecot service at the end.
File:/etc/postfix/master. cf
Dovecot unix-n-pipe
Flags = DRhu user = vmail: vmail argv =/usr/libexec/dovecot/deliver-f $ {sender}-d $ {recipient}
9. Configure postfix to auto start and start postfix.
Chkconfig postfix on
Service postfix start
 
Configure Dovecot
1. First back up the/etc/dovecot. conf file
Mv/etc/dovecot. conf/etc/dovecot. conf-backup
2. in this case, the file/etc/dovecot. conf does not exist. Create/etc/dovecot. conf, add the following configuration information. Here example.com needs to be replaced with its own domain name.
File:/etc/dovecot. conf
Protocols = imap pop3
Log_timestamp = "% Y-% m-% d % H: % M: % S"
Mail_location = maildir:/home/vmail/% d/% n/Maildir
Ssl_cert = </etc/pki/dovecot/certs/dovecot. pem
Ssl_key = </etc/pki/dovecot/private/dovecot. pem
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
}
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@example.com
}
Protocol pop3 {
Pop3_uidl_format = % 08Xu % 08Xv
}
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
}
3. New/etc/dovecot/dovecot-sql.conf.ext add the following configuration information, here you need to replace mail_admin_password for your own password.
File:/etc/dovecot/dovecot-sql.conf.ext
Driver = mysql
Connect = host = 127.0.0.1 dbname = mail user = mail_admin password = mail_admin_password
Default_pass_scheme = CRYPT
Password_query = SELECT email as user, password FROM users WHERE email = '% u ';
4. Modify the permissions and users of the file.
Chgrp dovecot/etc/dovecot/dovecot-sql.conf.ext
Chmod o =/etc/dovecot/dovecot-sql.conf.ext
5. Configure dovecot to automatically start and start it.
Chkconfig dovecot on
Service dovecot start
6. View the/var/log/maillog file and check the log information to check whether the startup is successful. For example, use cat to check whether the startup is successful. The file is roughly as follows:
Mar 18 15:21:59 sothoryos postfix/postfix-script [3069]: starting the Postfix mail system
Mar 18 15:22:00 sothoryos postfix/master [3070]: daemon started -- version 2.6.6, configuration/etc/postfix
Mar 18 15:32:03 sothoryos dovecot: master: Dovecot v2.0.9 starting up (core dumps disabled)
7. Test whether the pop3 server runs successfully. First install the telnet command.
Yum install telnet
Telnet localhost pop3
8. If it succeeds, the following content will be displayed, and then you can enter quit to exit.
Trying 127.0.0.1...
Connected to localhost. localdomain.
Escape character is '^]'.
+ OK Dovecot ready.
 
Configure email alias
1. Edit the/etc/aliases file, add the following statement, set the email alias, and replace example.com with your own domain name.
File:/etc/aliases
Postmaster: root
Root: postmaster@example.com
2. Update the alias and restart postfix.
Newaliases
Service postfix restart
 
Test Postfix
1. Connect to the server
Telnet localhost 25
2. After the connection is successful, enter the following command:
Ehlo localhost
3. The following information is displayed, indicating that the server runs successfully. Enter quit to disconnect.
250-hostname.example.com
250-PIPELINING
250-SIZE 30720000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN
250-AUTH = PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
 
Configure domain names and create users


First, you need to set the mx to add the domain name, that is, the resolution record, as shown in the figure I set on HiChina:

Here, My domain name is jlyblog.com. Subdomain name mail. Add an MX resolution record to mail.jlyblog.com. The smaller the value is, the higher the priority is ).
The following assumes that the domain name I set previously is already jlyblo.com. Perform the test.
1. Log on to mysql
Mysql-u root-p
2. Switch to the mail database, add the domain name to the virtual domain name table, and add an email account. Modify your domain name, account, and password.
USE mail;
Insert into domains (domain) VALUES ('jlyblog. Com ');
Insert into users (email, password) VALUES ('Hi @ jlyblog.com ', ENCRYPT ('mypassword '));
Quit
3. Use mailx to send emails. For example, now I am sending an email to another of my mailbox jianglinyan.jly@163.com (if mailx does not exist, install: yum install mailx first)


 

Here we use the new mailbox account hi@jlyblog.com to send information to another mailbox,-r specifies the sender,-s specifies the sending topic, and then specify the receiver, multiple receivers are used, separated, press enter, enter the email content, and press ctrl + D to submit the email.

 

4. We can view the/var/log/maillog log file to observe the status information, as shown in the figure: status = sent successfully:

 

 

5. Similarly I reply Hello with jianglinyan.jly@163.com, first meet "this mail, we view the log information as shown in the figure shows the status of successful delivery.

 

 

6. Next we can use the mutt tool to view the mailbox. According to the previous settings, the mailbox address is in the home directory of the vmail user. First install the mutt tool (yum install mutt ). Then go to the hi account's email address and run the mutt command to view the received email:

 

 

 

 

7. send an email with an attachment, first upload the attachment to the server (e.g. scp/home/jly/Desktop/resume.pdf root@www.jlyblog.com:/home/vmail ), then, use the-a option of mailx to specify the attachment, and redirect the standard input of mailx to the mess File (edited in advance) for convenience.

 

 

 

8. Receive an email containing an attachment and send an email containing the attachment to the jianglinyan.jly@163.com below. As shown in the figure, a Microsoft 100 question pdf is sent:

 

 

Run the mutt & ndash; f command to view the received email. The following shows how to receive a 6 m email:

 

 

View the email, press the space key to flip the page to the attachment, and press v to view the attachment.

 

 

Go to pdf, save by s, enter the Save name, and click OK.

 

The next step is to download the saved PDF file from the server to our computer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.