github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/notification/sendemail.go (about)

     1  package email
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/go-gomail/gomail"
     9  	"github.com/mdaxf/iac/logger"
    10  )
    11  
    12  type EmailConfiguration struct {
    13  	Host     string
    14  	Port     int
    15  	Username string
    16  	Password string
    17  	From     string
    18  }
    19  
    20  // SendEmail sends an email using the provided email configuration, recipient(s), subject, and body.
    21  // It returns an error if the email sending fails.
    22  // The email body is expected to be in HTML format.
    23  func SendEmail(emailConfig EmailConfiguration, to []string, subject string, body string) error {
    24  	log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "Notification"}
    25  	startTime := time.Now()
    26  	defer func() {
    27  		elapsed := time.Since(startTime)
    28  		log.PerformanceWithDuration("framework.email.SendEmail", elapsed)
    29  	}()
    30  	defer func() {
    31  		if r := recover(); r != nil {
    32  			log.Error(fmt.Sprintf("Error in framework.email.SendEmail: %s", r))
    33  			return
    34  		}
    35  	}()
    36  
    37  	log.Debug(fmt.Sprintf("Send the notification by email to: %s  ", strings.Join(to, ",")))
    38  
    39  	m := gomail.NewMessage()
    40  	m.SetHeader("From", emailConfig.From)
    41  	m.SetHeader("To", to...)
    42  	m.SetHeader("Subject", subject)
    43  	m.SetBody("text/html", body)
    44  
    45  	d := gomail.NewDialer(emailConfig.Host, emailConfig.Port, emailConfig.Username, emailConfig.Password)
    46  
    47  	if err := d.DialAndSend(m); err != nil {
    48  		log.Error(fmt.Sprintf("Failed to send email:%s", err.Error()))
    49  		return err
    50  	}
    51  	return nil
    52  }
    53  
    54  // SendEmailWithAttachment sends an email with an attachment.
    55  // It takes the following parameters:
    56  // - emailConfig: EmailConfiguration struct containing email server configuration details.
    57  // - to: a slice of strings representing the email recipients.
    58  // - subject: a string representing the email subject.
    59  // - body: a string representing the email body in HTML format.
    60  // - attachment: a string representing the file path of the attachment.
    61  // It returns an error if the email fails to send.
    62  
    63  func SendEmailWithAttachment(emailConfig EmailConfiguration, to []string, subject string, body string, attachment string) error {
    64  	log := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "Notification"}
    65  	startTime := time.Now()
    66  	defer func() {
    67  		elapsed := time.Since(startTime)
    68  		log.PerformanceWithDuration("framework.email.SendEmailWithAttachment", elapsed)
    69  	}()
    70  	defer func() {
    71  		if r := recover(); r != nil {
    72  			log.Error(fmt.Sprintf("Error in framework.email.SendEmailWithAttachment: %s", r))
    73  			return
    74  		}
    75  	}()
    76  	log.Debug(fmt.Sprintf("Send the notification by email to: %s  ", strings.Join(to, ",")))
    77  
    78  	m := gomail.NewMessage()
    79  	m.SetHeader("From", emailConfig.From)
    80  	m.SetHeader("To", to...)
    81  	m.SetHeader("Subject", subject)
    82  	m.SetBody("text/html", body)
    83  	m.Attach(attachment)
    84  
    85  	d := gomail.NewDialer(emailConfig.Host, emailConfig.Port, emailConfig.Username, emailConfig.Password)
    86  
    87  	if err := d.DialAndSend(m); err != nil {
    88  		log.Error(fmt.Sprintf("Failed to send email:%s", err.Error()))
    89  		return err
    90  	}
    91  	return nil
    92  }