https://github.com/iamfaizankhalid/goemail
Send simple/html email easily with attachments in golang.
https://github.com/iamfaizankhalid/goemail
attachment email golang smtp template
Last synced: about 1 month ago
JSON representation
Send simple/html email easily with attachments in golang.
- Host: GitHub
- URL: https://github.com/iamfaizankhalid/goemail
- Owner: IamFaizanKhalid
- License: mit
- Created: 2021-04-15T09:01:05.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-17T04:57:27.000Z (about 5 years ago)
- Last Synced: 2025-01-18T01:09:57.613Z (over 1 year ago)
- Topics: attachment, email, golang, smtp, template
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goemail [](https://travis-ci.com/github/IamFaizanKhalid/goemail) [](https://goreportcard.com/report/github.com/IamFaizanKhalid/goemail) 

A wrapper over `net/smtp` to make sending email easier in go.
## Features
- HTML email
- HTML template email
- Attach file (`[]byte`, `os.File` or by file path)
## Usage
### 1. Get Client:
Use your smtp server credentials to get client object.
```go
client := goemail.NewClient(&goemail.Config{
Host: "smtp.gmail.com",
Port: 587,
Email: "user@example.com",
Password: "password",
})
```
### 2. Get Mailer:
Get a new mailer object from the client for each new email to send.
```go
mailer := client.NewMailer("Test Email", "This is an email for testing.")
})
```
### 3. Build Email and Send:
Add recipients of your email.
Attach a file if you need to.
And send the email.
```go
mailer.AddRecipients([]mail.Address{
{
Name: "Random Guy",
Address: "randomguy123@example.com",
},
})
mailer.AddBlindCopyRecipients([]mail.Address{
{
Address: "secret01@example.com",
},
})
mailer.SetSender(mail.Address{
Name: "Faizan Khalid",
})
mailer.SetReplyToEmail("no-reply@example.com")
_ = mailer.AttachFile("../Downloads/my_file.pdf")
_ = mailer.Send()
```
## Html Template Example
```go
package main
import (
"github.com/IamFaizanKhalid/goemail"
"log"
"net/mail"
)
func main() {
client := goemail.NewClient(&goemail.Config{
Host: "smtp.gmail.com",
Port: 587,
Email: "user@example.com",
Password: "password",
})
templateValues := struct {
Name string
Url string
Title string
}{
Name: "John Doe",
Url: "http://johndoe.com",
Title: "Welcome to my Homepage",
}
mailer, err := client.NewHtmlMailerFromTemplate("JohnDoe.com", "welcome.html", templateValues)
if err != nil {
log.Println(err)
return
}
mailer.AddRecipients([]mail.Address{{Address: "IamFaizanKhalid@gmail.com"}})
err = mailer.Send()
if err != nil {
log.Println(err)
return
}
}
```
## Known Issues
- `AddInlineFile()` adds gibberish in the email.