Compare commits

...

21 Commits
0.0.6 ... 0.0.9

Author SHA1 Message Date
Ralph Slooten
b57e340389 Merge branch 'release/0.0.9' 2022-08-06 00:12:10 +12:00
Ralph Slooten
b9043b6c39 Release 0.0.9 2022-08-06 00:12:09 +12:00
Ralph Slooten
5860171002 Feature: HTTPS option for web UI 2022-08-06 00:09:20 +12:00
Ralph Slooten
ad49bf2898 Bugfix: Include read status in search results 2022-08-05 23:04:14 +12:00
Ralph Slooten
2d221a6b67 Testing: Memory & physical database tests 2022-08-05 21:35:57 +12:00
Ralph Slooten
4f266cd3f3 Merge tag '0.0.8' into develop
Release 0.0.8
2022-08-05 16:17:17 +12:00
Ralph Slooten
9fc7202552 Merge branch 'release/0.0.8' 2022-08-05 16:17:15 +12:00
Ralph Slooten
22a476ded5 Release 0.0.8 2022-08-05 16:17:15 +12:00
Ralph Slooten
54d3f6e3ad UI: Add project links to help in CLI 2022-08-05 15:53:22 +12:00
Ralph Slooten
cbe61e3f2e Add screenshot 2022-08-05 15:40:32 +12:00
Ralph Slooten
3b65a8852e Bugfix: Fix total/unread count after failed message inserts 2022-08-05 15:15:27 +12:00
Ralph Slooten
970a534d77 Update link to wiki 2022-08-04 23:18:06 +12:00
Ralph Slooten
f7502b1c14 Refer to wiki for build instructions 2022-08-04 23:17:01 +12:00
Ralph Slooten
e0f7d88d61 Merge tag '0.0.7' into develop
Release 0.0.7
2022-08-04 23:00:17 +12:00
Ralph Slooten
fc8148bfb3 Merge branch 'release/0.0.7' 2022-08-04 22:59:57 +12:00
Ralph Slooten
74fe6d55b4 Release 0.0.7 2022-08-04 22:59:57 +12:00
Ralph Slooten
47376d4db9 Update README 2022-08-04 22:59:07 +12:00
Ralph Slooten
4b9b60f247 Merge branch 'feature/docker' into develop 2022-08-04 22:51:28 +12:00
Ralph Slooten
123b0f19db Feature:: Add multi-arch docker image
Resolves #2
2022-08-04 22:51:20 +12:00
Ralph Slooten
9fed08245a Bugfix: Command flag should be --auth-file 2022-08-04 22:44:54 +12:00
Ralph Slooten
f807c166f7 Merge tag '0.0.6' into develop
Release 0.0.6
2022-08-04 20:48:07 +12:00
13 changed files with 237 additions and 100 deletions

2
.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
/node_modules
/mailpit

37
.github/workflows/build-docker.yml vendored Normal file
View File

@@ -0,0 +1,37 @@
on:
release:
types: [created]
name: Build docker images
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Get tag
id: tag
uses: dawidd6/action-get-tag@v1
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm
build-args: |
"VERSION=${{ steps.tag.outputs.tag }}"
push: true
tags: axllent/mailpit:latest,axllent/mailpit:${{ steps.tag.outputs.tag }}

View File

@@ -3,6 +3,33 @@
Notable changes to Mailpit will be documented in this file.
## 0.0.9
### Bugfix
- Include read status in search results
### Feature
- HTTPS option for web UI
### Testing
- Memory & physical database tests
## 0.0.8
### Bugfix
- Fix total/unread count after failed message inserts
### UI
- Add project links to help in CLI
## 0.0.7
### Bugfix
- Command flag should be `--auth-file`
## 0.0.6
### Bugfix

20
Dockerfile Normal file
View File

@@ -0,0 +1,20 @@
FROM golang:alpine as builder
ARG VERSION=dev
COPY . /app
WORKDIR /app
RUN apk add --no-cache git npm && \
npm install && npm run package && \
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/axllent/mailpit/cmd.Version=${VERSION}" -o /mailpit
FROM alpine:latest
COPY --from=builder /mailpit /mailpit
RUN apk add --no-cache tzdata
ENTRYPOINT ["/mailpit"]

View File

@@ -1,45 +0,0 @@
# Building Mailpit from source
Go (>= version 1.8) and npm are required to compile mailpit from source.
```
git clone git@github.com:axllent/mailpit.git
cd mailpit
```
## Building the UI
The Mailpit web user interface is built with node. In the project's root (top) directory run the following to install the required node modules:
### Installing the node modules
```
npm install
```
### Building the web UI
```
npm run build
```
You can also run `npm run watch` which will watch for changes and rebuild the HTML/CSS/JS automatically when changes are detected.
Please note that you must restart Mailpit (`go run .`) to run with the changes.
## Build the mailpit binary
One you have the assets compiled, you can build mailpit as follows:
```
go build -ldflags "-s -w"
```
## Building a stand-alone sendmail binary
This step is unnecessary, however if you do not intend to either symlink `sendmail` to mailpit or configure your existing sendmail to route mail to mailpit, you can optionally build a stand-alone sendmail binary.
```
cd sendmail
go build -ldflags "-s -w"
```

View File

@@ -6,32 +6,34 @@ It acts as both an SMTP server, and provides a web interface to view all capture
Mailpit is inspired by [MailHog](#why-rewrite-mailhog), but much, much faster.
![Mailpit](https://raw.githubusercontent.com/axllent/mailpit/develop/screenshot.png)
## Features
- Runs completely on a single binary
- SMTP server (default `0.0.0.0:1025`)
- Web UI to view emails (HTML format, text, source and MIME attachments, default `0.0.0.0:8025`)
- Optional HTTPS for web UI ([see wiki](https://github.com/axllent/mailpit/wiki/HTTPS))
- Real-time web UI updates using web sockets for new mail
- Optional basic authentication for web UI (see [wiki](https://github.com/axllent/mailpit/wiki/Basic-authentication))
- Optional basic authentication for web UI ([see wiki](https://github.com/axllent/mailpit/wiki/Basic-authentication))
- Email storage in either memory or disk (using [CloverDB](https://github.com/ostafen/clover)) - note that in-memory has a physical limit of 1MB per email
- Configurable automatic email pruning (default keeps the most recent 500 emails)
- Fast SMTP processing & storing - approximately 300-600 emails per second depending on CPU, network speed & email size
- Can handle tens of thousands of emails
- Multi-arch [Docker images](https://github.com/axllent/mailpit/wiki/Docker-images)
## Planned features
- Optional HTTPS for web UI
- Browser notifications for new mail (HTTPS only)
- Docker container
## Installation
Download a pre-built binary in the [releases](https://github.com/axllent/mailpit/releases/latest). The `mailpit` can be placed in your `$PATH`, or simply run as `./mailpit`. See `mailpit -h` for options.
To build mailpit from source see [building from source](README-BUILDING.md).
To build Mailpit from source see [building from source](https://github.com/axllent/mailpit/wiki/Building-from-source).
### Configuring sendmail
@@ -43,11 +45,11 @@ You can use `mailpit sendmail` as your sendmail configuration in `php.ini`:
sendmail_path = /usr/local/bin/mailpit sendmail
```
If mailpit is found on the same host as sendmail, you can symlink the mailpit binary to sendmail, eg: `ln -s /usr/local/bin/mailpit /usr/sbin/sendmail` (only if mailpit is running on default 1025 port).
If Mailpit is found on the same host as sendmail, you can symlink the Mailpit binary to sendmail, eg: `ln -s /usr/local/bin/mailpit /usr/sbin/sendmail` (only if Mailpit is running on default 1025 port).
You can use your default system `sendmail` binary to route directly to port `1025` (configurable) by calling `/usr/sbin/sendmail -S localhost:1025`.
You can build a mailpit-specific sendmail binary from source ( see [building from source](README-BUILDING.md)).
You can build a Mailpit-specific sendmail binary from source ( see [building from source](https://github.com/axllent/mailpit/wiki/Building-from-source)).
## Why rewrite MailHog?

View File

@@ -20,7 +20,11 @@ var rootCmd = &cobra.Command{
Short: "Mailpit is an email testing tool for developers",
Long: `Mailpit is an email testing tool for developers.
It acts as an SMTP server, and provides a web interface to view all captured emails.`,
It acts as an SMTP server, and provides a web interface to view all captured emails.
Documentation:
https://github.com/axllent/mailpit
https://github.com/axllent/mailpit/wiki`,
Run: func(_ *cobra.Command, _ []string) {
if err := config.VerifyConfig(); err != nil {
logger.Log().Error(err.Error())
@@ -60,9 +64,12 @@ func SendmailExecute() {
func init() {
// hide autocompletion
rootCmd.CompletionOptions.HiddenDefaultCmd = true
// rootCmd.Flags().SortFlags = false
// hide help
rootCmd.Flags().SortFlags = false
// hide help command
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
// hide help flag
rootCmd.PersistentFlags().BoolP("help", "h", false, "This help")
rootCmd.PersistentFlags().Lookup("help").Hidden = true
// defaults from envars if provided
if len(os.Getenv("MP_DATA_DIR")) > 0 {
@@ -80,11 +87,19 @@ func init() {
if len(os.Getenv("MP_AUTH_FILE")) > 0 {
config.AuthFile = os.Getenv("MP_AUTH_FILE")
}
if len(os.Getenv("MP_SSL_CERT")) > 0 {
config.SSLCert = os.Getenv("MP_SSL_CERT")
}
if len(os.Getenv("MP_SSL_KEY")) > 0 {
config.SSLKey = os.Getenv("MP_SSL_KEY")
}
rootCmd.Flags().StringVarP(&config.DataDir, "data", "d", config.DataDir, "Optional path to store peristent data")
rootCmd.Flags().StringVarP(&config.SMTPListen, "smtp", "s", config.SMTPListen, "SMTP bind interface and port")
rootCmd.Flags().StringVarP(&config.HTTPListen, "listen", "l", config.HTTPListen, "HTTP bind interface and port for UI")
rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages per mailbox")
rootCmd.Flags().StringVarP(&config.AuthFile, "-auth-file", "a", config.AuthFile, "A username:bcryptpw mapping file")
rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages to store")
rootCmd.Flags().StringVarP(&config.AuthFile, "auth-file", "a", config.AuthFile, "A password file for authentication (see wiki)")
rootCmd.Flags().StringVar(&config.SSLCert, "ssl-cert", config.SSLCert, "SSL certificate - requires ssl-key (see wiki)")
rootCmd.Flags().StringVar(&config.SSLKey, "ssl-key", config.SSLKey, "SSL key - requires ssl-cert (see wiki)")
rootCmd.Flags().BoolVarP(&config.VerboseLogging, "verbose", "v", false, "Verbose logging")
}

View File

@@ -2,6 +2,8 @@ package config
import (
"errors"
"fmt"
"os"
"regexp"
"github.com/tg123/go-htpasswd"
@@ -26,9 +28,10 @@ var (
// NoLogging for tests
NoLogging = false
// SSLCert @TODO
// SSLCert file
SSLCert string
// SSLKey @TODO
// SSLKey file
SSLKey string
// AuthFile for basic authentication
@@ -49,6 +52,10 @@ func VerifyConfig() error {
}
if AuthFile != "" {
if !isFile(AuthFile) {
return fmt.Errorf("password file not found: %s", AuthFile)
}
a, err := htpasswd.New(AuthFile, htpasswd.DefaultSystems, nil)
if err != nil {
return err
@@ -56,5 +63,29 @@ func VerifyConfig() error {
Auth = a
}
if SSLCert != "" && SSLKey == "" || SSLCert == "" && SSLKey != "" {
return errors.New("you must provide both an SSL certificate and a key")
}
if SSLCert != "" {
if !isFile(SSLCert) {
return fmt.Errorf("SSL certificate not found: %s", SSLCert)
}
if !isFile(SSLKey) {
return fmt.Errorf("SSL key not found: %s", SSLKey)
}
}
return nil
}
// IsFile returns if a path is a file
func isFile(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) || !info.Mode().IsRegular() {
return false
}
return true
}

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"net"
"net/mail"
"regexp"
"github.com/axllent/mailpit/config"
"github.com/axllent/mailpit/logger"
@@ -19,7 +20,15 @@ func mailHandler(origin net.Addr, from string, to []string, data []byte) error {
}
if _, err := storage.Store(storage.DefaultMailbox, data); err != nil {
logger.Log().Errorf("error storing message: %s", err.Error())
// Value with size 4800709 exceeded 1048576 limit
re := regexp.MustCompile(`(Value with size \d+ exceeded \d+ limit)`)
tooLarge := re.FindStringSubmatch(err.Error())
if len(tooLarge) > 0 {
logger.Log().Errorf("[db] error storing message: %s", tooLarge[0])
} else {
logger.Log().Errorf("[db] error storing message")
logger.Log().Errorf(err.Error())
}
return err
}

View File

@@ -209,6 +209,8 @@ func Store(mailbox string, b []byte) (string, error) {
return "", err
}
statsAddNewMessage(mailbox)
// save the raw email in a separate collection
raw := clover.NewDocument()
raw.Set("_id", id)
@@ -218,12 +220,11 @@ func Store(mailbox string, b []byte) (string, error) {
if err != nil {
// delete the summary because the data insert failed
logger.Log().Debugf("[db] error inserting raw message, rolling back")
_ = DeleteOneMessage(mailbox, id)
DeleteOneMessage(mailbox, id)
return "", err
}
statsAddNewMessage(mailbox)
count++
if count%100 == 0 {
logger.Log().Infof("100 messages added in %s", time.Since(per100start))
@@ -326,12 +327,12 @@ func Search(mailbox, search string, start, limit int) ([]data.Summary, error) {
results := []data.Summary{}
for _, d := range q {
cs := &CloverStore{}
cs := &data.Summary{}
if err := d.Unmarshal(cs); err != nil {
return nil, err
}
results = append(results, cs.Summary(d.ObjectId()))
cs.ID = d.ObjectId()
results = append(results, *cs)
}
return results, nil
@@ -350,25 +351,6 @@ func CountUnread(mailbox string) (int, error) {
)
}
// Summary generated a message summary. ID must be supplied
// as this is not stored within the CloverStore but rather the
// *clover.Document
func (c *CloverStore) Summary(id string) data.Summary {
s := data.Summary{
ID: id,
From: c.From,
To: c.To,
Cc: c.Cc,
Bcc: c.Bcc,
Subject: c.Subject,
Created: c.Created,
Size: c.Size,
Attachments: c.Attachments,
}
return s
}
// GetMessage returns a data.Message generated from the {mailbox}_data collection.
// ID must be supplied as this is not stored within the CloverStore but rather the
// *clover.Document
@@ -399,10 +381,7 @@ func GetMessage(mailbox, id string) (*data.Message, error) {
from = &mail.Address{Name: env.GetHeader("From")}
}
date, err := env.Date()
if err != nil {
// date =
}
date, _ := env.Date()
obj := data.Message{
ID: q.ObjectId(),
@@ -522,11 +501,18 @@ func UnreadMessage(mailbox, id string) error {
// DeleteOneMessage will delete a single message from a mailbox
func DeleteOneMessage(mailbox, id string) error {
q, err := db.FindById(mailbox, id)
if err != nil {
return err
}
unreadStatus := !q.Get("Read").(bool)
if err := db.DeleteById(mailbox, id); err != nil {
return err
}
statsDeleteOneMessage(mailbox)
statsDeleteOneMessage(mailbox, unreadStatus)
return db.DeleteById(mailbox+"_data", id)
}

View File

@@ -5,6 +5,8 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"
@@ -18,8 +20,10 @@ var (
)
func TestTextEmailInserts(t *testing.T) {
setup()
setup(false)
t.Log("Testing memory storage")
RepeatTest:
start := time.Now()
for i := 0; i < 1000; i++ {
if _, err := Store(DefaultMailbox, testTextEmail); err != nil {
@@ -55,11 +59,20 @@ func TestTextEmailInserts(t *testing.T) {
t.Logf("deleted 1,000 text emails in %s\n", time.Since(delStart))
db.Close()
if config.DataDir == "" {
setup(true)
t.Logf("Testing physical storage to %s", config.DataDir)
defer os.RemoveAll(config.DataDir)
goto RepeatTest
}
}
func TestMimeEmailInserts(t *testing.T) {
setup()
setup(false)
t.Log("Testing memory storage")
RepeatTest:
start := time.Now()
for i := 0; i < 1000; i++ {
if _, err := Store(DefaultMailbox, testMimeEmail); err != nil {
@@ -95,11 +108,19 @@ func TestMimeEmailInserts(t *testing.T) {
t.Logf("deleted 1,000 mime emails in %s\n", time.Since(delStart))
db.Close()
if config.DataDir == "" {
setup(true)
t.Logf("Testing physical storage to %s", config.DataDir)
defer os.RemoveAll(config.DataDir)
goto RepeatTest
}
}
func TestRetrieveMimeEmail(t *testing.T) {
setup()
setup(false)
t.Log("Testing memory storage")
RepeatTest:
id, err := Store(DefaultMailbox, testMimeEmail)
if err != nil {
t.Log("error ", err)
@@ -128,11 +149,20 @@ func TestRetrieveMimeEmail(t *testing.T) {
assertEqual(t, len(inlineData.Content), msg.Inline[0].Size, "inline attachment size does not match")
db.Close()
if config.DataDir == "" {
setup(true)
t.Logf("Testing physical storage to %s", config.DataDir)
defer os.RemoveAll(config.DataDir)
goto RepeatTest
}
}
func TestSearch(t *testing.T) {
setup()
setup(false)
t.Log("Testing memory storage")
RepeatTest:
for i := 0; i < 1000; i++ {
msg := enmime.Builder().
From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%d@example.com", i)).
@@ -198,10 +228,17 @@ func TestSearch(t *testing.T) {
assertEqual(t, len(summaries), 200, "200 search results expected")
db.Close()
if config.DataDir == "" {
setup(true)
t.Logf("Testing physical storage to %s", config.DataDir)
defer os.RemoveAll(config.DataDir)
goto RepeatTest
}
}
func BenchmarkImportText(b *testing.B) {
setup()
setup(false)
for i := 0; i < b.N; i++ {
if _, err := Store(DefaultMailbox, testTextEmail); err != nil {
@@ -214,7 +251,7 @@ func BenchmarkImportText(b *testing.B) {
}
func BenchmarkImportMime(b *testing.B) {
setup()
setup(false)
for i := 0; i < b.N; i++ {
if _, err := Store(DefaultMailbox, testMimeEmail); err != nil {
@@ -225,9 +262,16 @@ func BenchmarkImportMime(b *testing.B) {
db.Close()
}
func setup() {
func setup(dataDir bool) {
config.NoLogging = true
config.MaxMessages = 0
if dataDir {
config.DataDir = fmt.Sprintf("%s-%d", path.Join(os.TempDir(), "mailpit-tests"), time.Now().UnixNano())
} else {
config.DataDir = ""
}
if err := InitDB(); err != nil {
panic(err)
}
@@ -243,7 +287,6 @@ func setup() {
if err != nil {
panic(err)
}
}
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {

View File

@@ -63,13 +63,23 @@ func statsAddNewMessage(mailbox string) {
statsLock.Unlock()
}
// Deleting one will always mean it was read
func statsDeleteOneMessage(mailbox string) {
// Delete one message from the totals. If the message was unread,
// then it will also deduct one from the Unread status.
func statsDeleteOneMessage(mailbox string, unread bool) {
statsLock.Lock()
s, ok := mailboxStats[mailbox]
if ok {
// deduct from the totals
if s.Total > 0 {
s.Total = s.Total - 1
}
// only deduct if the original was unread
if unread && s.Unread > 0 {
s.Unread = s.Unread - 1
}
mailboxStats[mailbox] = data.MailboxStats{
Total: s.Total - 1,
Total: s.Total,
Unread: s.Unread,
}
}