Skip to content

Commit 34e9637

Browse files
authored
Merge pull request #6 from ahmadfaizk/feat/remove-mailer
Feat/remove mailer
2 parents 25ad5eb + 1f9cf78 commit 34e9637

33 files changed

+1787
-1706
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ go.work.sum
2929
.env
3030

3131
# Editor/IDE
32-
# .idea/
33-
# .vscode/
32+
.idea/
33+
.vscode/

README.md

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,85 @@
1-
# Go-Mailer
2-
[![Go](https://github.com/ahmadfaizk/go-mailer/actions/workflows/ci.yml/badge.svg)](https://github.com/ahmadfaizk/go-mailer/actions/workflows/ci.yml)
3-
[![Go Report Card](https://goreportcard.com/badge/github.com/ahmadfaizk/go-mailer)](https://goreportcard.com/report/github.com/ahmadfaizk/go-mailer)
4-
[![codecov](https://codecov.io/gh/ahmadfaizk/go-mailer/graph/badge.svg?token=7tbSVRaD4b)](https://codecov.io/gh/ahmadfaizk/go-mailer)
5-
[![GoDoc](https://pkg.go.dev/badge/github.com/ahmadfaizk/go-mailer)](https://pkg.go.dev/github.com/ahmadfaizk/go-mailer)
6-
[![Go Version](https://img.shields.io/github/go-mod/go-version/ahmadfaizk/go-mailer)](https://golang.org/doc/devel/release.html)
1+
# Go-Mailgen
2+
3+
[![Go](https://github.com/ahmadfaizk/go-mailgen/actions/workflows/ci.yml/badge.svg)](https://github.com/ahmadfaizk/go-mailgen/actions/workflows/ci.yml)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/ahmadfaizk/go-mailgen)](https://goreportcard.com/report/github.com/ahmadfaizk/go-mailgen)
5+
[![codecov](https://codecov.io/gh/ahmadfaizk/go-mailgen/graph/badge.svg?token=7tbSVRaD4b)](https://codecov.io/gh/ahmadfaizk/go-mailgen)
6+
[![GoDoc](https://pkg.go.dev/badge/github.com/ahmadfaizk/go-mailgen)](https://pkg.go.dev/github.com/ahmadfaizk/go-mailgen)
7+
[![Go Version](https://img.shields.io/github/go-mod/go-version/ahmadfaizk/go-mailgen)](https://golang.org/doc/devel/release.html)
78
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
89

9-
Go-Mailer is a Go library that wraps the [wneessen/go-mail](https://github.com/wneessen/go-mail) library to provide a simplified interface for sending emails using an existing `mail.Client` instance. This library enhances `go-mail` with a fluent HTML message builder, making it easy to create and send rich, dynamic email content.
10+
**Go-Mailgen** is a Go library for generating professional HTML emails using a fluent, intuitive API. Simplify email creation with customizable templates and seamless integration into your Go applications. This project is inspired by the [mailgen](https://github.com/eladnava/mailgen) Node.js package, bringing its elegant email generation approach to the Go ecosystem.
1011

1112
## Features
12-
- **Uses Existing `mail.Client`**: Integrates with an existing `wneessen/go-mail` client for flexible configuration and reuse.
13-
- **Fluent HTML Message Builder**: Provides a chainable API for constructing HTML emails with methods like `Subject`, `To`, `Line`, `Action`, etc.
14-
- Attachment Support: Easily attach files from local disk, embedded filesystems, IOFS filesystems, or `io.Reader`/`io.ReadSeeker`.
15-
- Responsive HTML Template: Automatically formats emails with a clean, responsive design compatible with most email clients.
16-
- CSS-inlined HTML: Automatically inlines CSS styles for better compatibility with email clients that strip out `<style>` tags.
13+
14+
- **Fluent API**: Build emails with a clean, chainable interface.
15+
- **Inline CSS**: Ensures compatibility across major email clients.
16+
- **Template-Based**: Use pre-built or custom templates for rapid development.
17+
- **Easy Integration**: Works effortlessly with popular Go mail libraries like go-mail.
18+
- **Dynamic Components Ordering**: Add components like buttons, tables, and lines in any order you want.
1719

1820
## Installation
19-
To install Go-Mailer, run the following command:
21+
22+
To install Go-Mailgen, run the following command:
2023

2124
```bash
22-
go get github.com/ahmadfaizk/go-mailer
25+
go get github.com/ahmadfaizk/go-mailgen
2326
```
2427

25-
This will also install the `wneessen/go-mail` dependency.
26-
2728
## Usage
28-
Go-Mailer requires an existing `mail.Client` instance from `wneessen/go-mail` to send emails. Below is an example demonstrating how to initialize the mailer and send an HTML email using the fluent message builder:
29+
30+
Here's a simple example of how to use Go-Mailgen to create an email:
2931

3032
```go
3133
package main
3234

3335
import (
34-
"context"
35-
"github.com/ahmadfaizk/go-mailer"
36-
"github.com/wneessen/go-mail"
36+
"github.com/ahmadfaizk/go-mailgen"
37+
"github.com/wneessen/go-mail"
3738
)
3839

3940
func main() {
40-
// Create a new go-mail client
41-
client, err := mail.NewClient("smtp.example.com",
42-
mail.WithPort(587),
43-
mail.WithSMTPAuth(mail.SMTPAuthPlain),
44-
mail.WithUsername("your-username"),
45-
mail.WithPassword("your-password"),
46-
)
47-
if err != nil {
48-
panic(err)
49-
}
50-
51-
// Initialize the Go-Mailer instance with the existing client
52-
m := mailer.New(client,
53-
mailer.WithFrom("noreply@example.com"),
54-
mailer.WithProduct(mailer.Product{
55-
Name: "My Application",
56-
URL: "https://example.com",
57-
})
58-
)
59-
60-
// Build an HTML email using the fluent message builder
61-
message := mailer.NewMessage().
62-
Subject("Reset Password").
63-
To("recipient@example.com").
64-
Line("Click the button below to reset your password").
65-
Action("Reset Password", "https://example.com/reset-password").
66-
Line("If you did not request this, please ignore this email")
67-
68-
// Send the email
69-
err = m.Send(message)
70-
if err != nil {
71-
panic(err)
72-
}
41+
// Initialize SMTP client
42+
mailer, err := mail.NewClient("smtp.example.com",
43+
mail.WithPort(587),
44+
mail.WithUsername("user"),
45+
mail.WithPassword("pass"),
46+
)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
// Set global configuration (optional)
52+
mailgen.SetDefault(mailgen.New().
53+
From("no-reply@example.com", "Go-Mailgen").
54+
Product(mailgen.Product{
55+
Name: "Go-Mailgen",
56+
URL: "https://github.com/ahmadfaizk/go-mailgen",
57+
}).
58+
Theme("default"),
59+
)
60+
61+
// Build the email
62+
email := mailgen.New().
63+
Subject("Reset Your Password").
64+
To("johndoe@mail.com").
65+
Line("Click the button below to reset your password").
66+
Action("Reset your password", "https://example.com/reset-password").
67+
Line("If you did not request this, please ignore this email")
68+
message, err := email.Build()
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
// Send the email
74+
msg := mail.NewMsg()
75+
msg.Subject(message.Subject())
76+
msg.From(message.From().String())
77+
msg.To(message.To()...)
78+
msg.SetBodyString(mail.TypeTextPlain, message.PlainText())
79+
msg.SetBodyString(mail.TypeTextHTML, message.HTML())
80+
if err := mailer.DialAndSend(msg); err != nil {
81+
panic(err)
82+
}
7383
}
7484
```
7585

@@ -81,13 +91,14 @@ You can find more examples in the [examples](examples) directory.
8191

8292
The following open-source themes are bundled with this package:
8393

84-
* `default` by [Postmark Transactional Email Templates](https://github.com/ActiveCampaign/postmark-templates)
94+
- `default` by [Postmark Transactional Email Templates](https://github.com/ActiveCampaign/postmark-templates)
8595

8696
<img src="examples/default/welcome.png" height="200" /> <img src="examples/default/reset.png" height="200" /> <img src="examples/default/receipt.png" height="200" />
8797

88-
* `plain` by [Postmark Transactional Email Templates](https://github.com/ActiveCampaign/postmark-templates)
98+
- `plain` by [Postmark Transactional Email Templates](https://github.com/ActiveCampaign/postmark-templates)
8999

90100
<img src="examples/plain/welcome.png" height="200" /> <img src="examples/plain/reset.png" height="200" /> <img src="examples/plain/receipt.png" height="200" />
91101

92102
## License
93-
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
103+
104+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)