-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eab6210
commit 3438acb
Showing
8 changed files
with
120 additions
and
2,255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,6 @@ _testmain.go | |
*.test | ||
*.prof | ||
|
||
*.db | ||
|
||
config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<h2>Envío {{.PackageNumber}}</h2> | ||
Hay un update del envío de referencia. | ||
<h4>Origen del envío</h4> | ||
<p> | ||
{{.From}} | ||
</p> | ||
<h4>Ultimo(s) movimiento(s)</h4> | ||
<p> | ||
<ul> | ||
{{ range $movement := .Movements }} | ||
<li>{{ $movement.Date }}: <strong>{{ $movement.Description }}</strong></li> | ||
{{ end }} | ||
</ul> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package notifications | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/eljuanchosf/gocafier/caching" | ||
log "github.com/eljuanchosf/gocafier/logging" | ||
"github.com/eljuanchosf/gocafier/settings" | ||
"gopkg.in/gomail.v2" | ||
) | ||
|
||
func loadBodyTemplate(packageNumber string, packageDetail caching.OcaPackageDetail, diff []caching.DetailLog) string { | ||
var fullBody bytes.Buffer | ||
|
||
type emailData struct { | ||
PackageNumber string | ||
From string | ||
Movements []caching.DetailLog | ||
} | ||
|
||
originDetails := packageDetail.Data[0].Detail[0] | ||
|
||
var packageData emailData | ||
packageData.PackageNumber = packageNumber | ||
packageData.From = fmt.Sprintf("%s %s, %s, %s", | ||
strings.TrimSpace(originDetails.DomicilioRetiro), | ||
strings.TrimSpace(originDetails.NumeroRetiro), | ||
strings.TrimSpace(originDetails.LocalidadRetiro), | ||
strings.TrimSpace(originDetails.PciaRetiro)) | ||
|
||
packageData.Movements = diff | ||
t, _ := template.ParseFiles("email-template.html") | ||
t.Execute(&fullBody, packageData) | ||
return fullBody.String() | ||
} | ||
|
||
//Send sends the email notification | ||
func Send(currentData caching.OcaPackageDetail, diff []caching.DetailLog, smtpUser string, smtpPassword string) error { | ||
packageNumber := currentData.Data[0].Code | ||
|
||
if diff == nil { | ||
diff = currentData.Data[0].Log | ||
} | ||
|
||
log.LogPackage(packageNumber, "Sending notification...") | ||
m := gomail.NewMessage() | ||
m.SetHeader("From", settings.Values.Email.From) | ||
m.SetHeader("To", settings.Values.Email.To) | ||
m.SetHeader("Subject", fmt.Sprintf(settings.Values.Email.Subject, packageNumber)) | ||
m.SetBody("text/html", loadBodyTemplate(packageNumber, currentData, diff)) | ||
|
||
d := gomail.NewPlainDialer(settings.Values.SMTP.Server, settings.Values.SMTP.Port, smtpUser, smtpPassword) | ||
if err := d.DialAndSend(m); err != nil { | ||
return err | ||
} | ||
|
||
log.LogPackage(packageNumber, "Notification sent") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.