Skip to content

Commit

Permalink
fixes #1, add headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
crossphoton committed Jan 4, 2022
1 parent ed94625 commit c09b680
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Send Standard emails with following parameters:
- `Body` *string* - Email body
- `ContentType` *string*: If `text/html` then HTML otherwise Plain text.
- `Attachments` *[Attachment](#attachment)* - Attachments to be sent with email.
- `Headers` *map(string -> string)* - Add headers.

[Example](./examples/Std.go)
### **SendRawEmail**
Expand All @@ -28,6 +29,7 @@ Send Templated emails (templates should exist beforehand):
- `TemplateName` *string* - Name of the template
- `Attachments` *[Attachment](#attachment)* - Attachments to be sent with email.
- `TemplateParams` *map(string -> string)* - Template data to be used in the template.
- `Headers` *map(string -> string)* - Add headers.

[Example](./examples/Template.go)

Expand Down
4 changes: 3 additions & 1 deletion email.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ message SendEmailRequest {
string body = 3;
string contentType = 4;
repeated Attachment attachments = 5;
map<string, string> headers = 6;
}

message RawSendEmailRequest {
repeated string recipients = 1;
bytes body = 3;
bytes body = 2;
}

message SendEmailWithTemplateRequest {
Expand All @@ -37,6 +38,7 @@ message SendEmailWithTemplateRequest {
string template_name = 3;
repeated Attachment attachments = 4;
map<string, string> template_params = 5;
map<string, string> headers = 6;
}

service EmailService {
Expand Down
12 changes: 12 additions & 0 deletions src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func sendEmailFromMessage(message *mail.Email) error {
return nil
}

func addHeaders(email *mail.Email, headers map[string]string) {
for k, v := range headers {
email.AddHeader(k, v)
}
}

// SendEmail sends an email with given Recipients, Subject, Body, .....
func (s *EmailServer) SendEmail(ctx context.Context, req *SendEmailRequest) (*ResponseMessage, error) {
if err := req.Recipients.validate(); err != nil {
Expand All @@ -44,6 +50,9 @@ func (s *EmailServer) SendEmail(ctx context.Context, req *SendEmailRequest) (*Re

email := mail.NewMSG()

// Add headers
addHeaders(email, req.GetHeaders())

// Add receivers
email.
AddTo(req.Recipients.To...).
Expand Down Expand Up @@ -113,6 +122,9 @@ func (s *EmailServer) SendEmailWithTemplate(ctx context.Context, req *SendEmailW
// forming email
email := mail.NewMSG()

// Add headers
addHeaders(email, req.GetHeaders())

// Add receivers
email.
AddTo(req.Recipients.To...).
Expand Down
5 changes: 5 additions & 0 deletions src/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"bytes"
"fmt"
"html/template"
"strings"
)

func getTemplate(templateName string) (*template.Template, error) {
// Santize
templateName = strings.ReplaceAll(templateName, "/", "")
templateName = strings.ReplaceAll(templateName, ".", "")

t, err := template.New(fmt.Sprintf("%s.html", templateName)).ParseFiles(fmt.Sprintf("./templates/%s.html", templateName))
if err != nil {
return nil, fmt.Errorf("invalid template: %v", err)
Expand Down

0 comments on commit c09b680

Please sign in to comment.