Preface: Hello, I am tree sauce. The origin of the article is because in a conversation, a friend mentioned a need. Need a batch of mailboxes to do some “things”, exactly what to do, keep the suspense. If you manually unregister your mailbox, you only need to solve the problem of receiving mail. Not only is it laborious, but now mobile authentication is required for email addresses like @163. Manual won’t work, so we’ll build our own mailboxes. It seemed complicated at first, because as a front end engineer, this “requirement” was beyond the scope. The problem is not big mo panic, after reading this you can create your own domain mailbox

1. Prepare

Setting up a mail server requires some “infrastructure”, including the following

  • Centos is recommended for one server
  • A domain name

1.1 Configuration Details

The mail server communicates with each other through SMTP. To enable the server to successfully receive mails, you need to enable port 25 and access port 25. Also, if you need to use a client like FoxMail to receive and receive email, you need to support POP3 protocol, you need to enable port 110. In other words, ports 25 and 110 need to be enabled to ensure the normal operation of the mail service

About the Post Office Protocol 3 (POP3) : The POP3 Protocol is used to remotely manage emails on servers using clients and store emails to local hosts

The following figure shows the rules for aliyun server to configure the security policy group, in which an access rule is added

Next comes the domain name. You need to configure domain name resolution and host records

The following figure shows the domain name resolution configuration, including several recorded values

  • MX class: Add an MX record. Set the type to MX record. The value can be the host name, your public IP address or mail.example.com. If the domain name is configured, A host record of type A needs to be added. The host record is defined as Mail. For details, see the following figure

  • Type A: This configuration is used to support clients to receive mails (for example, FoxMail). Add SMTP, IMAP, and POP configurations, and record the value as IP

After configuration, as shown in the figure below, you can see the configuration in the list.

2 Server Installation

2.1 Postfix

Postfix: Postfix is a software that implements SMTP and is also called the mail sending server. Postfix forwards mails. The specific forwarding rules need to be modified

I use aliyun server, first we install the mail service ‘Postfix’

  • The installation
Yum install postfix //Copy the code
  • configuration

After the installation, run the vi /etc/postfix/main.cf command to modify the following configuration

Myhostname = email.example.com// Set the hostname of the system mydomain = example.com// Set the domain name (we will let this setting be the part after the E-mail address "@") myOrigin = Inet_interfaces = all // Accept requests from all networks mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain // Specifies the domain name for sending local mail home_mailbox = Maildir/ / specifies the mailbox directory of the user # Specifies the maximum size of the mail to be 10M message_size_limit = 10485760 # Mailbox_size_limit = 1073741824 # SMTP authentication 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,rejectCopy the code

The main parameters in Postfix are shown below

  • Start the

If postfix is configured, start the service

Postfix check // Checking whether the configuration file is correct systemctl start postfix // Enabling the postfix service systemctl enable postfix // Enabling the Postfix service upon startupCopy the code

After configuring Postfix, we need to install Dovecot

2.2 Dovecot

Dovecot is a kind of open source service program which can provide IMAP and POP3 email service for Linux system, with high security, simple configuration, fast execution, and occupies less server hardware resources. POP3/IMAP is the protocol used to read messages from the mail server mentioned above

  • The installation
Yum install dovecot //Copy the code
  • configuration

After the success of the installation, configuration changes, through vi/etc/dovecot/dovecot conf command line to modify the following configuration

Protocols = imap POP3 LMTP listen = *, include conf.d/10-auth.conf ssl = no disable_plaintext_auth = no mail_location = maildir:~/MaildirCopy the code
  • Start the
Systemctl start dovecot systemctl start dovecot systemctl start dovecot systemctl start dovecot systemctl start dovecot systemctl start dovecotCopy the code

By configuring these two services, you are one step closer to success!

What’s the difference between Postfix and Dovecot?

Answer: Postfix is mainly used for sending emails, while Dovecot is mainly used for receiving emails

3 Creating a User

After setting up the mail server, we need to create users to receive and send mail

  • How to Create a User
Useradd tree/ New user passwd tree/ / Set the user passwordCopy the code

Ahle: If I create 100 email users like this, isn’t it a waste of time?

Don’t panic, we write a shell script, batch creation can solve your problem

Create a file createUser.sh with the following contents

TXT indicates the file of the user to be created. USER_FILE=user. TXT pass_FILE=passwd. TXT for user in 'cat user. TXT' do ID $user &> /dev/null # check whether The user exists if [$? -eq 0] then echo "The $user already exist" else useradd $user # create user if [$? -eq 0] ] then echo "$user create sucessful" PASSWD = $(echo $RANDOM | md5sum | the cut - 1-8 c) # randomly generated digital echo $PASSWD | PASSWD -- stdin $user &>/dev/null # change user password echo -e "$user\'$PASSWD'\'$(date +%Y%m%d)'" >> $pass_FILE #Copy the code

The premise is to create a user.txt to maintain the user we want to create, for example

tree
shujiang
Copy the code

The script will batch generate users based on the user names listed

4. Test the mailbox

After setting up the service and completing the creation of users, the next step is to test whether the mail is properly received

I use Foxmail for verification

This user name is the one we created in the previous section, and once we’ve created it, we test for success by sending an email

Another way, which I won’t cover in detail here, is to use Telnet to test. The original way

Arlo: Wouldn’t it be hard if I had to configure a client to receive mail for every new email user I created? Is there any other way?

Yes, think of it another way. You can configure email forwarding to forward all emails received to a certain user’s mailbox, so that you can only check emails in that mailbox (I’m beginning to doubt your motivation, is it doing some batch registration!).

You need to configure the Postfix configuration file mentioned in section 2 and add it at the end of the file

virtual_alias_domains = ensbook.com  mail.ensbook.com
virtual_alias_maps = hash:/etc/postfix/virtual
Copy the code

After completing the configuration, I looked up some information on the Internet and needed 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

This rule means: all emails sent to @ensbook.com are forwarded to QQ mailbox

It was found that it did not take effect. Finally, a virtual user was created to realize forwarding and receiving. If you see anything wrong, let me know in the comments section

Arlo: I can’t receive my email, and I don’t know what the problem is. How can I troubleshoot it?

You can view mail logs by running tail -n /var/log/maillog

The last

From 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 old or not, it’s good to be able to meet our needs. If you have another way to do it, feel free to leave a comment in the comments section.

Previous popular articles πŸ“– :

  • These Node open Source tools are worth owning
  • Developing visual Data Screens from 0 to 1 (PART 1)
  • Developing visual data Screens from 0 to 1 (part 2)
  • Construction of front-end Knowledge System of Tree Jam (PART 1)
  • Construction of front-end Knowledge System of Tree Jam (Part 2)
  • Talk about daily collaboration tools for front-end development
  • Babel configuration is stupid
  • How to better manage the Api
  • The interviewer asks you about Node
  • Front-end engineering stuff
  • Did you learn BFF and Serverless
  • Front-end operations and deployment

Hello, I am 🌲 tree sauce, please drink a cup 🍡 remember three lian oh ~

1. Remember to click a thumbs-up after reading oh, there is πŸ‘ motivation

2. Pay attention to the interesting things at the front of the public account, and chat with you about the interesting things at the front

3. Github frontendThings thanks to Star✨