51 lines
		
	
	
		
			913 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			913 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package mail
 | |
| 
 | |
| import (
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 	"io/ioutil"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| type Email struct {
 | |
| 	Sender    string   `json:"sender"`
 | |
| 	Receivers []string `json:"receivers"`
 | |
| 	Subject   string   `json:"subject"`
 | |
| 	Content   string   `json:"content"`
 | |
| }
 | |
| 
 | |
| func NewEmail(sender string, receivers []string, subject, content string) Email {
 | |
| 	return Email{
 | |
| 		Sender:    sender,
 | |
| 		Receivers: receivers,
 | |
| 		Subject:   subject,
 | |
| 		Content:   content,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func FromJSON(path string) (Email, error) {
 | |
| 	var mail Email
 | |
| 
 | |
| 	content, err := ioutil.ReadFile(path)
 | |
| 	if err != nil {
 | |
| 		return mail, err
 | |
| 	}
 | |
| 
 | |
| 	if err := json.Unmarshal(content, &mail); err != nil {
 | |
| 		return mail, err
 | |
| 	}
 | |
| 
 | |
| 	return mail, nil
 | |
| }
 | |
| 
 | |
| func (e Email) Generate() []byte {
 | |
| 	mail := fmt.Sprintf(
 | |
| 		"To: %s\nFrom: %s\nContent-Type: text/html;charset=utf-8\nSubject: %s\n\n%s",
 | |
| 		strings.Join(e.Receivers, ","),
 | |
| 		e.Sender,
 | |
| 		e.Subject,
 | |
| 		e.Content,
 | |
| 	)
 | |
| 	return []byte(mail)
 | |
| }
 |