Skip to content

Commit

Permalink
added emailTaskHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-devs committed Jun 8, 2024
1 parent 3b4775c commit e8ccab5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
9 changes: 7 additions & 2 deletions worker2/internal/grpcapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package internal

import (
"context"
"github.com/ansh-devs/task-zephyr/worker/taskhandler"

"github.com/ansh-devs/task-zephyr/worker/protov3/protos"
"github.com/sirupsen/logrus"
)

func (w *Worker) AssignTaskToWorker(ctx context.Context, req *protos.AssignTaskToWorkerRequest) (*protos.AssignTaskToWorkerResponse, error) {
logrus.Info(req.JobType)
logrus.Infof("processing task with id : %s", req.GetJobId())
logrus.WithFields(logrus.Fields{"job_id": req.GetJobId(), "job_type": req.GetJobType()}).Info("processing_task")
if req.JobType == "SEND_MAIL" {
if err := taskhandler.SendMailTask(req.GetCommand()); err != nil {
return &protos.AssignTaskToWorkerResponse{IsAccepted: false}, nil
}
}
return &protos.AssignTaskToWorkerResponse{IsAccepted: true}, nil
}
45 changes: 45 additions & 0 deletions worker2/taskhandler/emailTaskHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package taskhandler

import (
"errors"
"net/smtp"
"os"
)

func SendMailTask(receiver string) error {
// sender data
from, ok := os.LookupEnv("SENDER_MAIL")
if !ok {
return errors.New("env SENDER_MAIL not set")
}
password, ok := os.LookupEnv("SENDER_PASSWORD")
if !ok {
return errors.New("env SENDER_PASSWORD not set")
}
senderUserName, ok := os.LookupEnv("SENDER_USERNAME")
if !ok {
return errors.New("env SENDER_USERNAME not set")
}
// receiver address
toEmail := receiver // ex: "[email protected]"
to := []string{toEmail}
// smtp - Simple Mail Transfer Protocol
host, ok := os.LookupEnv("SENDER_HOST")
if !ok {
return errors.New("env SENDER_HOST not set")
}
port, ok := os.LookupEnv("SMTP_PORT")
if !ok {
return errors.New("env SMTP_PORT not set")
}
address := host + ":" + port
// Set up authentication information.
auth := smtp.PlainAuth("", senderUserName, password, host)
msg := []byte(
"From: " + from + " <" + from + ">\r\n" +
"To: " + toEmail + "\r\n" +
"Subject: Greetings Message !\r\n" +
"MIME: MIME-version: 1.0\r\n" +
"Content-Type: text/html; charset=\"UTF-8\";\r\n" + "\r\n" + `Hi, How Are You !`)
return smtp.SendMail(address, auth, from, to, msg)
}

0 comments on commit e8ccab5

Please sign in to comment.