Skip to content

Commit

Permalink
add mail header and body in integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Oct 14, 2023
1 parent 401d60e commit 6a1fa4d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,38 @@ type SMTPClient struct {
}

func (c *SMTPClient) SendEmail() error {
from := "[email protected]"
to := "[email protected]"

s, err := smtp.Dial(fmt.Sprintf("%s:%d", c.IP, c.Port))
if err != nil {
return fmt.Errorf("smtp.Dial(%s:%d): %#v", c.IP, c.Port, err)
}
if ok, _ := s.Extension("STARTTLS"); ok {
return fmt.Errorf("STARTTLS is available: %#v", s)
}
if err := s.Mail("[email protected]"); err != nil {
if err := s.Mail(from); err != nil {
return fmt.Errorf("smtp mail error: %#v", err)
}
if err := s.Rcpt("[email protected]"); err != nil {
if err := s.Rcpt(to); err != nil {
return fmt.Errorf("smtp rcpt error: %#v", err)
}
wc, err := s.Data()
if err != nil {
return fmt.Errorf("smtp data error: %#v", err)
}
_, err = fmt.Fprintf(wc, "This is the email body")

prefix := []byte(fmt.Sprintf("To: %s\nFrom: %s\nSubject: Test\n\n", to, from))
data := make([]byte, 1024)
for i := 0; i < len(data); i += 4 {
copy(data[i:i+4], []byte("Test"))
}
data = append(prefix, data...)
_, err = wc.Write(data)
if err != nil {
return fmt.Errorf("smtp data print error: %#v", err)
}

if err = wc.Close(); err != nil {
return fmt.Errorf("smtp close print error: %#v", err)
}
Expand Down

0 comments on commit 6a1fa4d

Please sign in to comment.