packages for handling email and protocol buffers
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
f87b4eb5be
commit
fcdb733839
|
@ -0,0 +1,29 @@
|
|||
package email
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Mail struct {
|
||||
Sender string
|
||||
To []string
|
||||
Subject string
|
||||
Body string
|
||||
}
|
||||
|
||||
func (mail Mail) Compose() string {
|
||||
msg := fmt.Sprintf("From: %s\r\n", mail.Sender)
|
||||
msg += fmt.Sprintf("To: %s\r\n", strings.Join(mail.To, ";"))
|
||||
msg += fmt.Sprintf("Subject: %s\r\n", mail.Subject)
|
||||
msg += fmt.Sprintf("\r\n%s\r\n", mail.Body)
|
||||
return msg
|
||||
}
|
||||
|
||||
func (mail Mail) SendMail(addr string, a smtp.Auth) error {
|
||||
if err := smtp.SendMail(addr, a, mail.Sender, mail.To, []byte(mail.Compose())); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package pb
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
// Read protocol buffer from file
|
||||
func ReadFile(path string, message protoreflect.ProtoMessage) error {
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := proto.Unmarshal(file, message); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write protocol buffer to file
|
||||
func WriteFile(path string, message protoreflect.ProtoMessage, perm fs.FileMode) error {
|
||||
msg, err := proto.Marshal(message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(path, msg, perm); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue