| 1 |
# go-pop3 |
| 2 |
|
| 3 |
A simple Go POP3 client library for connecting and reading mails from POP3 servers. This is a full rewrite of [TheCreeper/go-pop3](https://github.com/TheCreeper/go-pop3) with bug fixes and new features. |
| 4 |
|
| 5 |
|
| 6 |
## Install |
| 7 |
`go get -u github.com/knadh/go-pop3` |
| 8 |
|
| 9 |
|
| 10 |
## Example |
| 11 |
```go |
| 12 |
import ( |
| 13 |
"fmt" |
| 14 |
"github.com/knadh/go-pop3" |
| 15 |
) |
| 16 |
|
| 17 |
func main() { |
| 18 |
// Initialize the client. |
| 19 |
p := pop3.New(pop3.Opt{ |
| 20 |
Host: "pop.gmail.com", |
| 21 |
Port: 995, |
| 22 |
TLSEnabled: true, |
| 23 |
}) |
| 24 |
|
| 25 |
// Create a new connection. POP3 connections are stateful and should end |
| 26 |
// with a Quit() once the opreations are done. |
| 27 |
c, err := p.NewConn() |
| 28 |
if err != nil { |
| 29 |
log.Fatal(err) |
| 30 |
} |
| 31 |
defer c.Quit() |
| 32 |
|
| 33 |
// Authenticate. |
| 34 |
if err := c.Auth("myuser", "mypassword"); err != nil { |
| 35 |
log.Fatal(err) |
| 36 |
} |
| 37 |
|
| 38 |
// Print the total number of messages and their size. |
| 39 |
count, size, _ := c.Stat() |
| 40 |
fmt.Println("total messages=", count, "size=", size) |
| 41 |
|
| 42 |
// Pull the list of all message IDs and their sizes. |
| 43 |
msgs, _ := c.List(0) |
| 44 |
for _, m := range msgs { |
| 45 |
fmt.Println("id=", m.ID, "size=", m.Size) |
| 46 |
} |
| 47 |
|
| 48 |
// Pull all messages on the server. Message IDs go from 1 to N. |
| 49 |
for id := 1; id <= count; id++ { |
| 50 |
m, _ := c.Retr(id) |
| 51 |
|
| 52 |
fmt.Println(id, "=", m.Header.Get("subject")) |
| 53 |
|
| 54 |
// To read the multi-part e-mail bodies, see: |
| 55 |
// https://github.com/emersion/go-message/blob/master/example_test.go#L12 |
| 56 |
} |
| 57 |
|
| 58 |
// Delete all the messages. Server only executes deletions after a successful Quit() |
| 59 |
for id := 1; id <= count; id++ { |
| 60 |
c.Dele(id) |
| 61 |
} |
| 62 |
} |
| 63 |
``` |
| 64 |
|
| 65 |
[](https://pkg.go.dev/github.com/knadh/go-pop3) |
| 66 |
|
| 67 |
|
| 68 |
### To-do: tests |
| 69 |
Setup a Docker test environment that runs [InBucket](https://github.com/inbucket/inbucket) POP3 + SMTP server to run a dummy POP3 server and test all the commands in the lib. |
| 70 |
|
| 71 |
Licensed under the MIT License. |