头图

This article will introduce how to use Docker to quickly build a mail gateway suitable for HomeLab and development stage to quickly aggregate notification messages from various software. Of course, you can also use it to quickly verify that the mail configuration in various software is correct.

If you are familiar with Docker, in about ten minutes, you will have a set of mail notification aggregation services that are completely your own, and this service only needs about 20MB of memory consumption, which is very lightweight.

write in front

Recently, I am sorting out the software and services deployed at home. Most of these services have the ability to "email notification", and when necessary, they will use "send emails" to notify users of some necessary information, such as: task execution, Sensitive operations, statistical summaries of data run according to scheduled tasks, etc.

When deploying these software in the past, we had three choices in the configuration of the email notification function: registering a real mailbox, using the email account we are already using, and turning off the email notification function.

When the software is relatively small, no matter which plan you choose, it is all right, because our one-time operation and maintenance costs are relatively low .

But when we have deployed more and more software and services, closing the email notification is "ostrich behavior" and is not recommended; under the premise that the reliability of the software cannot be 100% sure, all software shares an email account, which is obviously a Not safe; the most reliable solution for is to configure a different email account for each software.

If you configure an independent email account for each software, the time cost of maintaining the email account will become non-negligible, because you never know when, which email account will have problems, and when you will leak Drop important app messages.

Therefore, I began to look for a privately deployed email gateway solution suitable for individuals or small teams to reduce account maintenance costs and economic costs, as well as minimize unnecessary public network data exchanges.

Software selection

In order to solve the above problems, two types of software solutions can generally be selected: post office software and mail test gateway.

Let's talk about post office applications first.

Post office software applications

Post office software, as the name implies, is the same as GMail, Outlook, QQ mailbox, 163 mailbox and so on that we use every day. On GitHub, we can also find many excellent post office software applications, such as the following:

The above open source solutions can all be used as alternatives to the cloud services and well-known email vendors that we use every day.

But usually, such software will contain a lot of components and capabilities, such as: Web interface, multi-account support, multiple post office aggregation, various mail protocol support, mail push, spam inspection, mail firewall, various complex Mail-related DNS support and more.

With the enrichment and improvement of software functions, the resource consumption in the running process of the software and the functional complexity in use will naturally increase. In addition to these head projects, the technology selection is mostly Ruby and Python, and the use of resources is natural. Even more "worse".

Considering that I don't need multi-user support, and I want my app to always be light and reliable . So, I turned my attention: testing gateway applications.

Mail test gateway application

Frankly speaking, there are not many projects that meet most of the requirements I mentioned above and have a relatively low resource footprint. If you limit the projects that can quickly perform functional verification (running and seeing the effect), it will be even more limited:

After a simple use, I chose to use the second project as a code base for secondary development. After all, based on my experience in previous projects, I have more confidence in Node than Ruby's performance and efficiency.

If you can't wait to verify the effect, you can skip the subsection below and go directly to the "Quick experience with Docker" section of the article.

Secondary development based on MailDev

From the current problems of the project and the feedback from the community, we can see several obvious problems:

  1. The software documentation and official images seem to be "mismatched", and there are also problems with the dependent configuration items in some codes, which will cause the software to fail to work properly. issue #376 , issue list
  2. The author officially announced the abandonment of the pit, and the latecomers made a fork version, but only solved some basic problems. issue #335
  3. Software dependencies and runtimes are too outdated, and the versions of the dependent libs lack effective management. Many dependencies in NPM sub-dependencies have been abandoned or have security risks.
  4. Safe tag filtering of content using a more reliable Markdown and HTML interconversion scheme.

So, I spent some time and made some tweaks to the original code:

  • Upgraded Node Runtime to v16 TLS.
  • Upgrade various basic dependencies to reliable versions to solve various security issues.
  • Rebuild the available Docker container version.

针对 MailDev 进行细节调整

If you are curious about what has been changed, you can see the commit record here: https://github.com/maildev/maildev/compare/master...soulteary:master

Next, let's take a look at how to quickly use this "mail tool" through the container.

Use Docker to quickly experience the mail gateway

If we want to start a "mail gateway", we can directly use the "one sentence" container command to solve the battle:

docker run -p 1080:1080 -p 1025:1025 soulteary/maildev

When the command finishes executing, we will be able to see log output similar to the following:

MailDev using directory /tmp/maildev-1
MailDev webapp running at http://0.0.0.0:1080
MailDev SMTP Server running at 0.0.0.0:1025

Then open http://0.0.0.0:1080 in the browser, and you can see the inbox interface as shown below.

MailDev 的欢迎界面

If we need to test whether the mail aggregation function can work normally, only needs to use the mail client, configure any user name and password , and send mail to port 0.0.0.0:1025 , and we can see the effect.

Remember the question above about needing to configure different accounts? Is it easy to solve it? You can even configure email forwarding to real mailboxes, and restrict receiving email messages from certain accounts only.

Use Node.js to quickly verify service functions

Rather than using the client, I prefer to use code for quick validation.

For the convenience of description here, I wrote a very simple script for sending letters using Node.js:

'use strict'

const nodemailer = require('nodemailer')

async function main () {
  const { user, pass } = await nodemailer.createTestAccount()
  let transporter = nodemailer.createTransport({
    host: '0.0.0.0',
    port: 1025,
    auth: { type: 'login', user, pass }
  })

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '\'Fred Foo 👻\' <foo@example.com>', // sender address
    to: 'bar@example.com, baz@example.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world?', // plain text body
    html: '<b>Hello world?</b>' // html body
  })

  console.log('Message sent: %s', info.messageId)
  // Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>

  // Preview only available when sending through an Ethereal account
  console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info))
  // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}

main().catch(console.error)

Save the above code as example-sendmail.js , then execute node example-sendmail.js , if it goes well, we will see a log output similar to the following:

Message sent: <c9867b60-8c5d-df8d-2476-39f5dd77f856@example.com>
Preview URL: false `

Next, open http://0.0.0.0:1080 in the browser, we will see an expected "email" in the MailDev interface, and the body of the email is exactly what we wrote in the code above.

收到来信的 MailDev

Before making additional code adjustments, we can repeat the above letter sending operation several times to simulate the email notification sending scenarios of various applications in daily study and work.

At this point, the newly arrived "mail" will be displayed in the list of MailDev in real time.

MailDev 的邮件列表

Start the service with Docker-Compose

In order to facilitate my old readers, so that everyone can be lazy together, by convention, I provide a simple container orchestration configuration file:

version: '3'

services:

  maildev:
    image: soulteary/maildev
    restart: always
    environment:
      - TZ=Asia/Shanghai
      - MAILDEV_WEB_PORT=1080
      - MAILDEV_SMTP_PORT=1025
    ports:
      - "1080:1080"
      - "1025:1025"

Save the above content as docker-compose.yml , and then use docker-compose up -d start the application. As mentioned above, we can access http://localhost:1080 in the browser to browse and manage the "mail content", and perform mail aggregation operations through the 1025 port. .

finally

Like the other projects mentioned before, I will continue to improve this project. In a short time, I hope it can better support WebHook, and get through with some message push software to better support my HomeLab scene.

If you are interested in this project and are "impatient", you can visit the project source code: https://github.com/soulteary/maildev for DIY. Of course, you are also welcome to leave your suggestions and ideas for this project in the project issue.

--EOF


We have a small tossing group, which gathers hundreds of friends who like tossing.

Without advertising, we will chat about software and hardware, HomeLab, and programming issues together, and we will also share some technical salon information in the group from time to time.

Friends who like tossing are welcome to scan the code to add friends. (Add friends, please note the real name, indicate the source and purpose, otherwise it will not pass the review)

Those things about tossing the group into the group


This article uses the "Signature 4.0 International (CC BY 4.0)" license agreement, welcome to reprint, or re-modify for use, but you need to indicate the source. Attribution 4.0 International (CC BY 4.0)

Author of this article: Su Yang

Created: March 15, 2022
Statistical words: 5362 words
Reading time: 11 minutes to read
Link to this article: https://soulteary.com/2022/03/15/build-a-lightweight-mail-gateway-for-homelab-using-docker.html


soulteary
191 声望7 粉丝

折腾硬核技术,分享实用内容。