123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package smtpn
- /*
- SMTP Notifaiction Agent
- This is the mail sending agent for sending notification to user
- */
- import (
- "io/ioutil"
- "log"
- "net/smtp"
- "strconv"
- "time"
- "github.com/valyala/fasttemplate"
- notification "imuslab.com/arozos/mod/notification"
- )
- type Agent struct {
- Hostname string
- SMTPSenderDisplayName string
- SMTPSender string
- SMTPPassword string
- SMTPDomain string
- SMTPPort int
- UsernameToEmailMap map[string]string
- }
- func (a Agent) Name() string {
- return "smtpn"
- }
- func (a Agent) Desc() string {
- return "Notify user throught email"
- }
- func (a Agent) IsConsumer() bool {
- return true
- }
- func (a Agent) IsProducer() bool {
- return false
- }
- func (a Agent) ConsumerNotification(incomingNotification *notification.NotificationPayload) error {
- //Get a notification and send it out
- //Analysis the notification, get the target user's email
- userEmails := [][]string{}
- for _, username := range incomingNotification.Receiver {
- userEmail, ok := a.UsernameToEmailMap[username]
- if ok {
- userEmails = append(userEmails, []string{username, userEmail})
- } else {
- log.Println("[SMTP Notification] Unable to notify " + username + ": Email not set")
- }
- }
- //For each user, send out the email
- for _, thisEntry := range userEmails {
- thisUser := thisEntry[0]
- thisEmail := thisEntry[1]
- //Load email template
- template, _ := ioutil.ReadFile("system/www/smtpn.html")
- t := fasttemplate.New(string(template), "{{", "}}")
- s := t.ExecuteString(map[string]interface{}{
- "receiver": "Hello " + thisUser + ",",
- "message": incomingNotification.Message,
- "sender": incomingNotification.Sender,
- "hostname": a.Hostname,
- "timestamp": time.Now().Format("2006-01-02 3:4:5 PM"),
- })
- msg := []byte("To: " + thisEmail + "\n" +
- "From: " + a.SMTPSenderDisplayName + " <" + a.SMTPSender + ">\n" +
- "Subject: " + incomingNotification.Title + "\n" +
- "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
- s + "\n\n")
- //Login to the SMTP server
- auth := smtp.PlainAuth("", a.SMTPSender, a.SMTPPassword, a.SMTPDomain)
- err := smtp.SendMail(a.SMTPDomain+":"+strconv.Itoa(a.SMTPPort), auth, a.SMTPSender, []string{thisEmail}, msg)
- if err != nil {
- log.Println("[SMTPN] Email sent failed: ", err.Error())
- return err
- }
- }
- return nil
- }
- func (a Agent) ProduceNotification(producerListeningEndpoint *notification.AgentProducerFunction) {
- return
- }
|