refactor: project structure
This commit is contained in:
42
internal/attachments/attachments.go
Normal file
42
internal/attachments/attachments.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package attachments
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
const delimeter = "**=myohmy689407924327"
|
||||
|
||||
type EmailAttachment struct {
|
||||
File io.Reader
|
||||
Title string
|
||||
}
|
||||
|
||||
func (e EmailAttachment) ReadContent() ([]byte, error) {
|
||||
bts, err := io.ReadAll(e.File)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading attachment: %s", err)
|
||||
}
|
||||
return bts, nil
|
||||
}
|
||||
|
||||
func AttachmentsToBytes(body string, attachments []EmailAttachment) ([]byte, error) {
|
||||
var message bytes.Buffer
|
||||
message.WriteString("Content-Type: text/html; charset=\"UTF-8\"\r\n\r\n")
|
||||
message.WriteString(body + "\r\n\r\n")
|
||||
for _, attachment := range attachments {
|
||||
attachmentRawFile, err := attachment.ReadContent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
message.WriteString("--" + delimeter + "\r\n")
|
||||
message.WriteString("Content-Disposition: attachment; filename=\"" + attachment.Title + "\"\r\n")
|
||||
message.WriteString("Content-Type: application/octet-stream\r\n")
|
||||
message.WriteString("Content-Transfer-Encoding: base64\r\n\r\n")
|
||||
message.WriteString(base64.StdEncoding.EncodeToString(attachmentRawFile) + "\r\n")
|
||||
}
|
||||
message.WriteString("--" + delimeter + "--") // End the message
|
||||
return message.Bytes(), nil
|
||||
}
|
||||
39
internal/smtpclient/smtpclient.go
Normal file
39
internal/smtpclient/smtpclient.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package smtpclient
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type SMTPClient struct {
|
||||
client smtp.Client
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Auth(a smtp.Auth) error {
|
||||
return c.client.Auth(a)
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Close() error {
|
||||
return c.client.Close()
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Data() (io.WriteCloser, error) {
|
||||
return c.client.Data()
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Mail(from string) error {
|
||||
return c.client.Mail(from)
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Quit() error {
|
||||
return c.client.Quit()
|
||||
}
|
||||
|
||||
func (c *SMTPClient) Rcpt(to string) error {
|
||||
return c.client.Rcpt(to)
|
||||
}
|
||||
|
||||
func (c *SMTPClient) StartTLS(config *tls.Config) error {
|
||||
return c.client.StartTLS(config)
|
||||
}
|
||||
Reference in New Issue
Block a user