Added basic email renderer

This commit is contained in:
2019-08-16 14:27:45 +09:00
parent 373b34177d
commit 2a1d4ea479
9 changed files with 90 additions and 25 deletions

View File

@ -72,11 +72,10 @@ type APIKeysData struct {
PrivateKey string `json:"privateKey"`
} `json:"vapid"`
SMTP struct {
Server string `json:"server"`
Address string `json:"address"`
Password string `json:"password"`
} `json:"smtp"`
Mailgun struct {
Domain string `json:"domain"`
PrivateKey string `json:"privateKey"`
} `json:"mailgun"`
S3 struct {
ID string `json:"id"`

33
arn/EmailRenderer.go Normal file
View File

@ -0,0 +1,33 @@
package arn
import (
"context"
"fmt"
"time"
"github.com/mailgun/mailgun-go/v3"
)
// HTMLEmailRenderer is the instance used for rendering emails.
var HTMLEmailRenderer EmailRenderer
// EmailRenderer is an interface for rendering HTML emails.
type EmailRenderer interface {
Notification(notification *Notification) string
}
// SendEmail sends an e-mail.
func SendEmail(email string, subject string, html string) error {
mg := mailgun.NewMailgun(APIKeys.Mailgun.Domain, APIKeys.Mailgun.PrivateKey)
sender := fmt.Sprintf("Anime Notifier <notifications@%s>", APIKeys.Mailgun.Domain)
message := mg.NewMessage(sender, subject, "", email)
message.SetHtml(html)
// Allow a 10-second timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Send the message
_, _, err := mg.Send(ctx, message)
return err
}

View File

@ -208,6 +208,17 @@ func (user *User) SendNotification(pushNotification *PushNotification) {
// Save changes
subs.Save()
// Send email notification
if IsDevelopment() && user.ID == "4J6qpK1ve" {
subject := notification.Title
html := HTMLEmailRenderer.Notification(notification)
err := SendEmail(user.Email, subject, html)
if err != nil {
fmt.Println(err)
}
}
// Send an event to the user's open tabs
user.BroadcastEvent(&aero.Event{
Name: "notificationCount",

View File

@ -1,20 +0,0 @@
package mailer
import (
"github.com/animenotifier/notify.moe/arn"
gomail "gopkg.in/gomail.v2"
)
// SendEmailNotification sends an e-mail notification.
func SendEmailNotification(email string, notification *arn.PushNotification) error {
m := gomail.NewMessage()
m.SetHeader("From", arn.APIKeys.SMTP.Address)
m.SetHeader("To", email)
m.SetHeader("Subject", notification.Title)
m.SetBody("text/html", "<h2>"+notification.Message+"</h2><p><a href='"+notification.Link+"' target='_blank'><img src='"+notification.Icon+"' alt='Anime cover image' style='width:125px;height:181px;'></a></p>")
d := gomail.NewDialer(arn.APIKeys.SMTP.Server, 587, arn.APIKeys.SMTP.Address, arn.APIKeys.SMTP.Password)
// Send the email
return d.DialAndSend(m)
}