Skip to content

Commit a388836

Browse files
authored
smtp support added (#7)
[default] PROVIDER=smtp SMTP_HOST=smtp.gmail.com SMTP_PORT=587 SMTP_USE_STARTTLS=true SMTP_USER=<yourmailid@gmail.com> SMTP_PASSWORD=<app-password> SMTP_SUBJECT=<Your subject> SMTP_FROM=<from-mail@gmail.com> SMTP_TO=<user1@gmail.com,user1@gmail.com>
1 parent 77c3d53 commit a388836

9 files changed

Lines changed: 487 additions & 184 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ With chatz, you can streamline your notification processes across multiple platf
2727
- Telegram: [Read documentation](docs/telegram.md)
2828
- Discord: [Read documentation](docs/discord.md)
2929
- Redis: [Read documentation](docs/redis.md)
30+
- SMTP: [Read documentation](docs/smtp.md)
3031

3132
## Installation
3233
Download and install executable binary from GitHub releases page.
@@ -113,6 +114,47 @@ CONNECTION_URL=<redis-connection-url>
113114
CHANNEL_ID=<redis-publish-channel>
114115
```
115116

117+
- Config for smtp provider with TLS
118+
```ini
119+
[default]
120+
PROVIDER=smtp
121+
SMTP_HOST=smtp.gmail.com
122+
SMTP_PORT=465
123+
SMTP_USE_TLS=true
124+
SMTP_USER=<yourmailid@gmail.com>
125+
SMTP_PASSWORD=<app-password>
126+
SMTP_SUBJECT=<Your subject>
127+
SMTP_FROM=<from-mail@gmail.com>
128+
SMTP_TO=<user1@gmail.com,user1@gmail.com>
129+
```
130+
131+
- Config for smtp provider with STARTTLS
132+
```ini
133+
[default]
134+
PROVIDER=smtp
135+
SMTP_HOST=smtp.gmail.com
136+
SMTP_PORT=587
137+
SMTP_USE_STARTTLS=true
138+
SMTP_USER=<yourmailid@gmail.com>
139+
SMTP_PASSWORD=<app-password>
140+
SMTP_SUBJECT=<Your subject>
141+
SMTP_FROM=<from-mail@gmail.com>
142+
SMTP_TO=<user1@gmail.com,user1@gmail.com>
143+
```
144+
145+
- Config for smtp provider without encryption
146+
```ini
147+
[default]
148+
PROVIDER=smtp
149+
SMTP_HOST=smtp.yourmailserver.com
150+
SMTP_PORT=25
151+
SMTP_USER=<yourmailid@yourmailserver.com>
152+
SMTP_PASSWORD=<app-password>
153+
SMTP_SUBJECT=<Your subject>
154+
SMTP_FROM=<from-mail@yourmailserver.com>
155+
SMTP_TO=<user1@gmail.com,user1@gmail.com>
156+
```
157+
116158
### System Environment Support
117159
Chatz also support system environment variable. To use system environment variable then use `--from-env` or `-e` flag.
118160
Name of the environment variable is same as `chatz.ini` config key, for example `export PROVIDER=slack`.

config/config.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ package config
22

33
// Environment
44
type Config struct {
5-
Provider string
6-
WebHookURL string
7-
Token string
8-
ChannelId string
9-
ChatId string
10-
ConnectionURL string
5+
Provider string
6+
WebHookURL string
7+
Token string
8+
ChannelId string
9+
ChatId string
10+
ConnectionURL string
11+
SMTPHost string
12+
SMTPPort string
13+
UseTLS bool
14+
UseSTARTTLS bool
15+
SMTPUser string
16+
SMTPPassword string
17+
SMTPSubject string
18+
SMTPFrom string
19+
SMTPTo string
1120
}

constants/common.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package constants
22

33
const (
4-
PROVIDER_SLACK="slack"
5-
PROVIDER_DISCORD="discord"
6-
PROVIDER_TELEGRAM="telegram"
7-
PROVIDER_GOOGLE="google"
8-
PROVIDER_REDIS="redis"
4+
PROVIDER_SLACK = "slack"
5+
PROVIDER_DISCORD = "discord"
6+
PROVIDER_TELEGRAM = "telegram"
7+
PROVIDER_GOOGLE = "google"
8+
PROVIDER_REDIS = "redis"
9+
PROVIDER_SMTP = "smtp"
910
)

docs/smtp.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SMTP with Gmail
2+
3+
This document explains how to configure SMTP with Gmail, including creating an App Password, and understanding TLS/STARTTLS and encryption options.
4+
5+
6+
## 1. Creating an App Password
7+
8+
Gmail requires an "App Password" for less secure apps accessing your account. This is a more secure alternative to using your regular Gmail password directly.
9+
10+
1. Go to your [Google account security settings](https://myaccount.google.com/security).
11+
2. Scroll down to "Signing in to Google" and click "App Passwords".
12+
3. Select "Mail" as the app and "Other (Custom name)" as the device. Give it a descriptive name (e.g., "My SMTP App").
13+
4. Click "Generate". A new password will be displayed. **Copy this password immediately; you won't be able to see it again.**
14+
15+
## 2. SMTP Server Settings
16+
17+
* **Server:** `smtp.gmail.com`
18+
* **Port:**
19+
* **STARTTLS:** 587 (Recommended)
20+
* **TLS:** 465
21+
* **No Encryption (Insecure - Avoid):** 25
22+
* **Username:** Your full Gmail address (e.g., `yourname@gmail.com`)
23+
* **Password:** The App Password you generated.
24+
25+
26+
## 3. TLS/STARTTLS and Encryption
27+
28+
* **TLS (Transport Layer Security) / STARTTLS:** These are encryption protocols that secure the connection between your email client and the SMTP server. They encrypt your email messages and prevent eavesdropping. **Using TLS/STARTTLS is strongly recommended.** Many email clients support STARTTLS, initiating encryption during the connection process.
29+
30+
31+
* **No Encryption:** Sending emails without encryption is highly discouraged. Your email, including the subject, body, and any attachments, could be intercepted by malicious actors. Avoid this unless absolutely necessary, and only with trusted parties.
32+
33+
34+
35+
## 4. Example Configuration (Illustrative - adapt to your email client)
36+
37+
38+
This is a generic example; the exact settings will depend on your email client (e.g., Outlook, Thunderbird). Refer to your email client's documentation for specific instructions.
39+
40+
41+
```
42+
SMTP_HOST=smtp.gmail.com
43+
SMTP_PORT=587
44+
SMTP_USE_STARTTLS=true
45+
```
46+
47+
Remember to replace placeholders with your actual information.
48+
49+
## 5. Troubleshooting
50+
51+
52+
If you encounter issues, check:
53+
54+
* **Correct App Password:** Verify you copied the correct App Password.
55+
* **Firewall Settings:** Ensure your firewall isn't blocking outgoing connections on port 587 (or 465 if using no encryption).
56+
* **Email Client Configuration:** Double-check all server settings in your email client.
57+
58+
Using an App Password is crucial for securing your Gmail account when using SMTP. Always prioritize using TLS/STARTTLS encryption for the security of your emails.
59+
```

main.go

Lines changed: 88 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,106 +10,104 @@ import (
1010
)
1111

1212
var (
13-
AppVersion = "v0.0.1"
14-
CommitHash = "unknown"
15-
BuildDate = "unknown"
13+
AppVersion = "v0.0.1"
14+
CommitHash = "unknown"
15+
BuildDate = "unknown"
1616
)
1717

1818
func main() {
19-
20-
var version bool
21-
var profile string
22-
var threadId string
23-
var output bool
24-
var fromEnv bool
2519

26-
app := cli.NewApp()
27-
app.Name = "chatz"
28-
app.Description = "chatz is a versatile messaging app designed to send notifications to Google Chat, Slack, Discord, Telegram and Redis."
29-
app.Flags = []cli.Flag {
30-
&cli.BoolFlag{
31-
Name: "output",
32-
Aliases: []string{"o"},
33-
Usage: "Print output to stdout",
34-
Destination: &output,
35-
},
36-
&cli.StringFlag{
37-
Name: "profile",
38-
Aliases: []string{"p"},
39-
Value: "default",
40-
Usage: "Profile from .chatz.ini",
41-
Destination: &profile,
42-
},
43-
&cli.StringFlag{
44-
Name: "thread-id",
45-
Aliases: []string{"t"},
46-
Value: "",
47-
Usage: "Thread ID for reply to a message",
48-
Destination: &threadId,
49-
},
50-
&cli.BoolFlag{
51-
Name: "version",
52-
Aliases: []string{"v"},
53-
Usage: "Print the version number",
54-
Destination: &version,
55-
},
56-
&cli.BoolFlag{
57-
Name: "from-env",
58-
Aliases: []string{"e"},
59-
Usage: "To use config from environment variables",
60-
Destination: &fromEnv,
61-
},
62-
}
63-
app.Action = func(ctx *cli.Context) error {
64-
if version {
65-
fmt.Println("chatz version: ", AppVersion)
66-
fmt.Println("Commit Hash: ", CommitHash)
67-
fmt.Println("Build Date: ", BuildDate)
68-
return nil
69-
}
20+
var version bool
21+
var profile string
22+
var threadId string
23+
var output bool
24+
var fromEnv bool
7025

26+
app := cli.NewApp()
27+
app.Name = "chatz"
28+
app.Description = "chatz is a versatile messaging app designed to send notifications to Google Chat, Slack, Discord, Telegram and Redis."
29+
app.Flags = []cli.Flag{
30+
&cli.BoolFlag{
31+
Name: "output",
32+
Aliases: []string{"o"},
33+
Usage: "Print output to stdout",
34+
Destination: &output,
35+
},
36+
&cli.StringFlag{
37+
Name: "profile",
38+
Aliases: []string{"p"},
39+
Value: "default",
40+
Usage: "Profile from .chatz.ini",
41+
Destination: &profile,
42+
},
43+
&cli.StringFlag{
44+
Name: "thread-id",
45+
Aliases: []string{"t"},
46+
Value: "",
47+
Usage: "Thread ID for reply to a message",
48+
Destination: &threadId,
49+
},
50+
&cli.BoolFlag{
51+
Name: "version",
52+
Aliases: []string{"v"},
53+
Usage: "Print the version number",
54+
Destination: &version,
55+
},
56+
&cli.BoolFlag{
57+
Name: "from-env",
58+
Aliases: []string{"e"},
59+
Usage: "To use config from environment variables",
60+
Destination: &fromEnv,
61+
},
62+
}
63+
app.Action = func(ctx *cli.Context) error {
64+
if version {
65+
fmt.Println("chatz version: ", AppVersion)
66+
fmt.Println("Commit Hash: ", CommitHash)
67+
fmt.Println("Build Date: ", BuildDate)
68+
return nil
69+
}
70+
71+
var message string
72+
if ctx.Args().Len() == 0 {
73+
fmt.Println("Please provide a message.")
74+
return nil
75+
}
76+
for i, a := range ctx.Args().Slice() {
77+
if i == 0 {
78+
message = a
79+
continue
80+
}
81+
message = fmt.Sprintf("%s %s", message, a)
82+
}
7183

72-
var message string
73-
if ctx.Args().Len() == 0 {
74-
fmt.Println("Please provide a message.")
75-
return nil
76-
}
77-
for i, a := range ctx.Args().Slice() {
78-
if i == 0 {
79-
message = a
80-
continue
81-
}
82-
message = fmt.Sprintf("%s %s",message, a)
83-
}
84-
85-
env, err := utils.LoadEnv(profile, fromEnv)
86-
if err!=nil {
87-
return nil
88-
}
89-
provider, err := providers.NewProvider(env)
90-
if err!=nil {
91-
fmt.Println(err.Error())
92-
return nil
93-
}
84+
env, err := utils.LoadEnv(profile, fromEnv)
85+
if err != nil {
86+
return nil
87+
}
9488

95-
if len(threadId) > 0 {
96-
res, _ := provider.Reply(threadId, message)
97-
if output {
98-
fmt.Println(res)
99-
}
100-
return nil
101-
}
102-
res, _ := provider.Post(message)
103-
if output {
104-
fmt.Println(res)
105-
}
89+
provider, err := providers.NewProvider(env)
90+
if err != nil {
91+
fmt.Println(err.Error())
92+
return nil
93+
}
10694

107-
return nil
108-
}
95+
if len(threadId) > 0 {
96+
res, _ := provider.Reply(threadId, message)
97+
if output {
98+
fmt.Println(res)
99+
}
100+
return nil
101+
}
102+
res, err := provider.Post(message)
103+
if output {
104+
fmt.Println(res)
105+
}
106+
return nil
107+
}
109108

110-
if err := app.Run(os.Args); err != nil {
109+
if err := app.Run(os.Args); err != nil {
111110
panic(err)
112111
}
113112

114113
}
115-

man/chatz.1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ CONNECTION_URL=<redis-url>
115115
CHANNEL_ID=<redis-channel>
116116
.fi
117117

118+
.TP
119+
Configuration for SMTP.
120+
121+
.nf
122+
[default]
123+
PROVIDER=smtp
124+
SMTP_HOST=smtp.gmail.com
125+
SMTP_PORT=587
126+
SMTP_USE_STARTTLS=true
127+
SMTP_USER=<yourmailid@gmail.com>
128+
SMTP_PASSWORD=<app-password>
129+
SMTP_SUBJECT=<Your subject>
130+
SMTP_FROM=<from-mail@gmail.com>
131+
SMTP_TO=<user1@gmail.com,user1@gmail.com>
132+
.fi
133+
118134
.TP
119135
Ensure that each service is properly configured with valid tokens and webhook URLs.
120136

0 commit comments

Comments
 (0)