Skip to content

Commit

Permalink
Added config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
JCoupalK committed Dec 23, 2023
1 parent cd47548 commit 44cdfb5
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 284 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ go.work

mail2go
*.tar.gz

releases/
build/
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Mail2Go is a very lightweight command-line SMTP client written in Go, designed t
- [Installation](#installation)
- [Building from Source](#building-from-source)
- [Usage](#usage)
- [Examples](#examples)
- [Contributing](#contributing)
- [License](#license)

Expand All @@ -22,7 +23,7 @@ Mail2Go is a very lightweight command-line SMTP client written in Go, designed t
- **Attachments Support**: Attach multiple files of various types to your emails.
- **HTML and Plain Text**: Supports both HTML and plain text formats for email bodies.
- **Command Line Interface**: Easy-to-use CLI arguments for configuring and sending emails.
- **Flexible Configuration**: SMTP server, port, username, and password can be configured through CLI arguments.
- **Flexible Configuration**: SMTP server, port, username, and password can be configured through CLI arguments or a JSON configuration file.

## Requirements

Expand All @@ -34,13 +35,13 @@ Mail2Go is a very lightweight command-line SMTP client written in Go, designed t
1. Download the binary with wget:

```shell
wget https://github.com/KeepSec-Technologies/Mail2Go/releases/download/1.0/mail2go_linux_amd64_1.0.tar.gz
wget https://github.com/KeepSec-Technologies/Mail2Go/releases/download/1.1/mail2go_linux_amd64_1.1.tar.gz
```

2. Unpack it with tar

```shell
tar -xf mail2go_linux_amd64_1.0.tar.gz
tar -xf mail2go_linux_amd64_1.1.tar.gz
```

3. Move it to your /usr/local/bin/ (Optional):
Expand Down Expand Up @@ -72,27 +73,36 @@ Mail2Go is a very lightweight command-line SMTP client written in Go, designed t

## Usage

Run the Mail2Go tool with the required flags:

```shell
./mail2go --smtp-server [SMTP_SERVER] --smtp-port [SMTP_PORT] --smtp-username [USERNAME] --smtp-password [PASSWORD] --from-email [FROM_EMAIL] --to-email [TO_EMAIL_1],[TO_EMAIL_2] --subject "Your Subject" --body "Your email body." --attachments "path/to/attachment1,path/to/attachment2"
```

Flags:
Run the Mail2Go tool with the required arguments:

```text
-s, --smtp-server SMTP server for sending emails
-p, --smtp-port SMTP server port (Default: 587)
-u, --smtp-username Username for SMTP authentication
-w, --smtp-password Password for SMTP authentication
-f, --from-email Email address to send from
-c, --config Path to the SMTP json config file which replaces the above arguments
-t, --to-email Email addresses that will receive the email, comma-separated
-h, --subject Subject of the email
-b, --body Body of the email
-af, --attachments File paths for attachments, comma-separated
-bf, --body-file File path for email body
```

Those arguments would look like this in your CLI:

```shell
./mail2go --smtp-server [SMTP_SERVER] --smtp-port [SMTP_PORT] --smtp-username [USERNAME] --smtp-password [PASSWORD] --from-email [FROM_EMAIL] --to-email [TO_EMAIL_1],[TO_EMAIL_2] --subject "Your Subject" --body "Your email body." --attachments "path/to/attachment1,path/to/attachment2"
```

or

```shell
./mail2go --config "path/to/config.json" --to-email [TO_EMAIL_1],[TO_EMAIL_2] --subject "Your Subject" --body-file "path/to/email.html" --attachments "path/to/attachment1,path/to/attachment2"
```

## Examples

Basic example:

```shell
Expand All @@ -105,6 +115,24 @@ Example with two recipients, the body from an HTML file and two attached files (
./mail2go -s mail.example.com -u user@example.com -w password123 -f mail2go@example.com -t admin@example.com,other@example.com -h 'Test Mail2Go Subject' -bf demo/body.html -af README.md,demo/mail2go-smaller.png
```

Example of json config file to pass to Mail2Go:

```json
{
"smtp_server": "mail.example.com",
"smtp_port": 587,
"smtp_username": "user@example.com",
"smtp_password": "password123",
"from_email": "mail2go@example.com"
}
```

The command that goes with it:

```shell
./mail2go -c demo/config.json -t admin@example.com -h 'Test Mail2Go Subject' -b 'This is a body!'
```

## Contributing

Contributions to Mail2Go are welcome. Please fork the repository and submit a pull request with your changes or improvements.
Expand Down
33 changes: 33 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"encoding/json"
"os"
)

// For config file
type Config struct {
SMTPServer string `json:"smtp_server"`
SMTPPort int `json:"smtp_port"`
SMTPUsername string `json:"smtp_username"`
SMTPPassword string `json:"smtp_password"`
FromEmail string `json:"from_email"`
}

func loadConfig(filePath string) (Config, error) {
var config Config

file, err := os.Open(filePath)
if err != nil {
return config, err
}
defer file.Close()

decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return config, err
}

return config, nil
}
7 changes: 7 additions & 0 deletions demo/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"smtp_server": "mail.example.com",
"smtp_port": 587,
"smtp_username": "user@example.com",
"smtp_password": "password123",
"from_email": "mail2go@example.com"
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module mail2go
module github.com/KeepSec-Technologies/Mail2Go

go 1.20

require gopkg.in/mail.v2 v2.3.1

require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk=
gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
Loading

0 comments on commit 44cdfb5

Please sign in to comment.