Skip to content

Commit

Permalink
fix: update content type and encoding for xlsx attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
tommzn committed Dec 29, 2022
1 parent 2c31e95 commit 3cdd86b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
19 changes: 16 additions & 3 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package timetracker

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"mime/multipart"
"net/textproto"
Expand Down Expand Up @@ -51,6 +53,10 @@ func (publisher *EMailPublisher) Send(content []byte, fileName string) error {
// RawEMail generates a raw email with given sender/receiver, subject, message and attachment.
func rawEMail(source, destination, subject, message string, csvFile []byte, attachmentFilename string) (*string, error) {

if !strings.HasSuffix(attachmentFilename, "xlsx") {
return nil, errors.New("Excel (xlsx) supported only atm!")
}

buf := new(bytes.Buffer)
writer := multipart.NewWriter(buf)

Expand Down Expand Up @@ -82,16 +88,23 @@ func rawEMail(source, destination, subject, message string, csvFile []byte, atta
fn := attachmentFilename
h = make(textproto.MIMEHeader)
h.Set("Content-Disposition", "attachment; filename="+fn)
h.Set("Content-Type", "text/csv; x-unix-mode=0644; name=\""+fn+"\"")
h.Set("Content-Transfer-Encoding", "7bit")
h.Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; x-unix-mode=0644; name=\""+fn+"\"")
h.Set("Content-Transfer-Encoding", "base64")
//h.Set("Content-Transfer-Encoding", "7bit")
part, err = writer.CreatePart(h)
if err != nil {
return nil, err
}
_, err = part.Write(csvFile)

b := make([]byte, base64.StdEncoding.EncodedLen(len(csvFile)))
base64.StdEncoding.Encode(b, csvFile)

_, err = part.Write(b)
//_, err = part.Write(csvFile)
if err != nil {
return nil, err
}

err = writer.Close()
if err != nil {
return nil, err
Expand Down
15 changes: 9 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ module github.com/tommzn/hob-timetracker

go 1.19

require (
github.com/aws/aws-sdk-go v1.44.168
github.com/calendarific/go-calendarific v0.0.0-20221115171631-30c5173a0a3f
github.com/tommzn/go-log v1.2.2
github.com/tommzn/go-utils v1.0.2
github.com/xuri/excelize/v2 v2.6.1
golang.org/x/exp v0.0.0-20221114191408-850992195362
)

require (
github.com/aws/aws-lambda-go v1.34.1 // indirect
github.com/aws/aws-sdk-go v1.44.168 // indirect
github.com/calendarific/go-calendarific v0.0.0-20221115171631-30c5173a0a3f // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand All @@ -27,14 +34,10 @@ require (
github.com/stretchr/testify v1.8.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tommzn/go-config v1.0.1 // indirect
github.com/tommzn/go-log v1.2.2 // indirect
github.com/tommzn/go-secrets v1.0.0 // indirect
github.com/tommzn/go-utils v1.0.2 // indirect
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
github.com/xuri/excelize/v2 v2.6.1 // indirect
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
Expand Down

0 comments on commit 3cdd86b

Please sign in to comment.