Foreword: Hello, I'm Shuchan. The source of the article is because in a conversation, a friend mentioned a need. Need a batch of mailboxes to do some "things", specifically what they are doing, leave a little suspense. If you manually register the mailbox, you only need to solve the problem of receiving mail. Not only is it laborious, but now including email addresses like @163 also require mobile phone verification. If it doesn't work manually, then we will "make" the mailbox ourselves. At first, I felt quite complicated. After all, as a front-end engineer, this "requirement" is already beyond the scope. Don’t panic if the problem is not big, you can create your own domain email after reading this article.
1. Preliminary preparation
Setting up a mail server requires some "infrastructure", including the following
- One server recommend centos
- A domain name
1.1 Configuration details
The mail server communicates through the SMTP protocol. In order for the server to successfully receive mail, we need to open port 25 and allow access to port 25. At the same time, if you need to use a client like foxmail to receive and send mail, you also need to support the POP3 protocol, and you need to open port 110. In other words, in order to ensure the normal use of the mail service, the two ports 25 and 110 need to be opened.
Regarding the POP3 protocol ( Post Office Protocol 3 ): the protocol is mainly used to support the use of the client to remotely manage the email on the server and store the email to the local host
The following figure shows the rules of the security policy group configured by Alibaba Cloud server, and an access rule is added to it
Next is the domain name, you need to configure domain name resolution, configure host records
The following figure shows the resolution configuration of the domain name, which mainly includes several record values
- MX category: add MX records, select MX records for the type, and the value can fill in the host name, or fill in your public network ip address or mail.example.com. If the configuration is a domain name, you also need to add a record of type A, the host record is defined as: mail, see the figure below for details
- Type A: This configuration is mainly used to support the client to receive mail (such as: foxmail) to add smtp, imap, pop and other configurations, and the record value is ip
After configuring as shown in the figure below, you can see the configured in the list,
2 Server installation
2.1 Postfix
About postfix: Postfix is the software that implements the SMTP protocol, also known as the mail sending server, which is responsible for forwarding mail. The specific forwarding rules require us to modify the configuration of postfix.
I am using Alibaba Cloud server, first we install the mail service `postfix'
- Install
yum install postfix // 服务器安装
- Configuration
After the installation is successful, modify the configuration, modify the following configuration vi /etc/postfix/main.cf
myhostname = email.example.com // 设置系统的主机名
mydomain = example.com // 设置域名(我们将让此处设置将成为E-mail地址“@”后面的部分)
myorigin = $mydomain // 将发信地址“@”后面的部分设置为域名(非系统主机名)
inet_interfaces = all // 接受来自所有网络的请求
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain // 指定发给本地邮件的域名
home_mailbox = Maildir/ // 指定用户邮箱目录
# 规定邮件最大尺寸为10M
message_size_limit = 10485760
# 规定收件箱最大容量为1G
mailbox_size_limit = 1073741824
# SMTP认证
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject
The figure below is the main parameters in postfix
- start up
After configuring postfix, start the service
postfix check // 检查配置文件是否正确
systemctl start postfix //开启postfix服务
systemctl enable postfix //设置postfix服务开机启动
After completing the postfix configuration, we also need to install dovecot
2.2 Dovecot
About Dovecot: It is an open source service program that can provide IMAP and POP3 email services for Linux systems. It has high security, simple configuration, fast execution speed, and it takes up less server hardware resources. As mentioned above, POP3/IMAP is the protocol used when reading mail from the mail server
- Install
yum install dovecot // 服务器安装
- Configuration
After the installation is successful, modify the configuration, modify the following configurationvi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp listen = *,
#新添加以下配置 #
!include conf.d/10-auth.conf
ssl = no
disable_plaintext_auth = no
mail_location = maildir:~/Maildir
- start up
systemctl start dovecot //开启dovecot服务
systemctl enable dovecot //置dovecot服务开机启动
After completing the configuration of the above two services, you are one step closer to success!
Ah Le: What is the difference between postfix and dovecot?
Answer: Postfix is mainly used for sending mails, while dovecot is mainly used for receiving. Only by combining the two can a complete mail service be completed.
3 New user
After setting up the mail server, we need to create users to complete the receiving and sending of emails
- How to create users
useradd tree/ 新增用户
passwd tree // 设置用户密码
Ah Le: Wouldn’t it be a waste of time if I create 100 mailbox users in this way?
Don’t panic, let’s write a shell script and create it in batches to solve your problem
Create a file, createUser.sh
content is as follows
/bash
#user.txt 为需要创建的用户的文件passwd.txt为随机生成密码
USER_FILE=user.txt
pass_FILE=passwd.txt
for user in `cat user.txt`
do
id $user &> /dev/null #查看用户是否存在
if [ $? -eq 0 ]
then
echo "The $user already exist"
else
useradd $user #创建用户
if [ $? -eq 0 ]
then
echo "$user create sucessful"
PASSWD=$(echo $RANDOM |md5sum |cut -c 1-8) #随机生成数字
echo $PASSWD |passwd --stdin $user &>/dev/null #修改用户密码
echo -e "$user\'$PASSWD'\'$(date +%Y%m%d)'" >> $pass_FILE #将用户,密码,日期输入到文件中
fi
fi
done
The premise needs to create a user.txt to maintain the user we want to create, such as
tree
shujiang
The script will generate users in batches based on the usernames we listed
4. Test the mailbox
After setting up the service and completing the user creation, the next step is to test whether the email is received normally.
I use foxmail for verification
This user name is the user name we created in the previous section. After the creation is completed, we send an email to test whether it can be successfully received
Another way is to use telnet to do the test, and I will not introduce it here. The most primitive way
Student Ale: If I create a new mailbox user, I have to configure a client to receive mail. Isn’t it very laborious? Is there any other way?
Yes, from another perspective, you can configure mail forwarding to forward all mail received to a certain user’s mailbox, and you can check mail only in that mailbox (I am starting to doubt your motives, whether you are doing something Register in bulk!)
The details are as follows, you need to configure the postfix configuration file mentioned in the second section, add at the end of the file
virtual_alias_domains = ensbook.com mail.ensbook.com
virtual_alias_maps = hash:/etc/postfix/virtual
After completing the configuration, I check some information on the Internet, and need to configure the /etc/postfix/virtual
file, which is mainly used to manage email forwarding rules.
So I tried to modify the /etc/postfix/virtual file and add some information
The meaning of this rule is: all emails sent to @ensbook.com are forwarded to qq mailbox
It was discovered that it did not take effect. Finally, a virtual
was created to forward and receive. If you can see the problem, remember to tell me in the comment section
Student Ale: I can’t receive the mailbox and I don’t know what the problem is. How can I troubleshoot it?
You can check the mail log tail -n /var/log/maillog
finally
Through the above understanding, it is not difficult to see that the creation of a domain name mail server is actually very simple and the technology is very old. But whether you are old or not, it is good to be able to solve our needs. If you have other ways to achieve it, please leave a message in the comment area.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。