Skip to content

Commit

Permalink
add sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcoder04 committed Nov 24, 2022
1 parent 5c6fbbf commit 4038252
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
18 changes: 14 additions & 4 deletions iserv/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package email

import (
"fmt"
"net/smtp"

"github.com/alexcoder04/iserv2go/iserv/types"
"github.com/emersion/go-imap/client"
Expand All @@ -10,19 +11,28 @@ import (
type IServEmailClient struct {
config *types.IServAccountConfig
imapClient *client.Client
smtpAuth smtp.Auth
}

func (c *IServEmailClient) Login(config *types.IServAccountConfig) error {
c.config = config

conn, err := client.DialTLS(fmt.Sprintf("%s:993", c.config.IServHost), nil)
// imap client
conn1, err := client.DialTLS(fmt.Sprintf("%s:993", c.config.IServHost), nil)
if err != nil {
fmt.Println("error dial tls")
return err
}
c.imapClient = conn
c.imapClient = conn1

return c.imapClient.Login(c.config.Username, c.config.Password)
err = c.imapClient.Login(c.config.Username, c.config.Password)
if err != nil {
return err
}

// smtp client
c.smtpAuth = smtp.PlainAuth("", c.config.Username, c.config.Password, c.config.IServHost)

return nil
}

func (c *IServEmailClient) Logout() error {
Expand Down
18 changes: 17 additions & 1 deletion iserv/email/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package email

import "github.com/emersion/go-imap"
import (
"fmt"
"net/smtp"

"github.com/alexcoder04/iserv2go/iserv/types"
"github.com/emersion/go-imap"
)

func (c *IServEmailClient) ListMailboxes() ([]imap.MailboxInfo, error) {
mailboxes := make(chan *imap.MailboxInfo, 10)
Expand Down Expand Up @@ -52,3 +58,13 @@ func (c *IServEmailClient) ReadMailbox(name string, limit uint32) ([]imap.Messag

return res, nil
}

func (c *IServEmailClient) SendMail(mail types.EMail) error {
return smtp.SendMail(
fmt.Sprintf("%s:587", c.config.IServHost),
c.smtpAuth,
mail.From,
append(mail.CCs, mail.To),
buildMailBody(mail),
)
}
38 changes: 38 additions & 0 deletions iserv/email/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package email

import (
"fmt"
"strings"

"github.com/alexcoder04/iserv2go/iserv/types"
)

func getToString(addr string, dispName string, ccs []string) string {
var res string

if dispName == "" {
res = fmt.Sprintf("To: %s", addr)
} else {
res = fmt.Sprintf("To: %s <%s>", dispName, addr)
}

if len(ccs) > 0 {
res += fmt.Sprintf("\r\nCc: %s", strings.Join(ccs, ", "))
}

return res
}

func buildMailBody(mail types.EMail) []byte {
return []byte(
strings.Join([]string{
"MIME-version: 1.0",
"Content-Type: text/plain; charset=utf-8",
"From: " + mail.From,
getToString(mail.To, mail.ToDispName, mail.CCs),
"Subject: " + mail.Subject,
"\r\n",
mail.Body,
}, "\r\n"),
)
}
10 changes: 10 additions & 0 deletions iserv/types/email-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

type EMail struct {
Subject string
From string
To string
ToDispName string
CCs []string
Body string
}
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func main() {
for _, m := range messages {
fmt.Printf(" = '%s' from %s\n", m.Envelope.Subject, m.Envelope.Sender[0].Address())
}

myMail := fmt.Sprintf("%s@%s", os.Getenv("ISERV_USERNAME"), os.Getenv("ISERV_HOST"))
m := types.EMail{
Subject: "Hello World",
From: myMail,
To: myMail,
ToDispName: "ME",
CCs: []string{},
Body: "Hello World, it's me via iserv2go!",
}
err = client.Email.SendMail(m)
if err != nil {
Warn("Cannot send mail: %s", err.Error())
}
fmt.Println("Sent test message to myself!")
}

// files
Expand Down

0 comments on commit 4038252

Please sign in to comment.