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

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	sendemail "github.com/mdaxf/iac/framework/notification"
    10  )
    11  
    12  type EmailFuncs struct{}
    13  
    14  type EmailStru struct {
    15  	ToEmailList  []string
    16  	FromEmail    string
    17  	Subject      string
    18  	Body         string
    19  	Attachment   string
    20  	SmtpServer   string
    21  	SmtpPort     int
    22  	SmtpUser     string
    23  	SmtpPassword string
    24  }
    25  
    26  // Execute executes the SendEmail function.
    27  // It validates the email configuration, sets up the email parameters,
    28  // and sends the email using the provided SMTP server.
    29  // If an attachment is specified, it sends the email with the attachment,
    30  // otherwise it sends the email without any attachment.
    31  func (cf *EmailFuncs) Execute(f *Funcs) {
    32  	// function execution start time
    33  	startTime := time.Now()
    34  	defer func() {
    35  		// calculate elapsed time
    36  		elapsed := time.Since(startTime)
    37  		// log performance duration
    38  		f.iLog.PerformanceWithDuration("engine.funcs.SendEmail.Execute", elapsed)
    39  	}()
    40  
    41  	defer func() {
    42  		// recover from any panics and handle the error
    43  		if err := recover(); err != nil {
    44  			// log the error
    45  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.SendEmail.Execute with error: %s", err))
    46  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.SendEmail.Execute with error: %s", err))
    47  			// set the error message
    48  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.SendEmail.Execute with error: %s", err)
    49  			return
    50  		}
    51  	}()
    52  
    53  	f.iLog.Debug(fmt.Sprintf("EmailFuncs Execute"))
    54  
    55  	// validate the email configuration
    56  	_, email := cf.Validate(f)
    57  
    58  	// set up the email configuration
    59  	ecfg := sendemail.EmailConfiguration{
    60  		Host:     email.SmtpServer,
    61  		Port:     email.SmtpPort,
    62  		Username: email.SmtpUser,
    63  		Password: email.SmtpPassword,
    64  		From:     email.FromEmail,
    65  	}
    66  
    67  	// send the email with or without attachment
    68  	if email.Attachment != "" {
    69  		sendemail.SendEmailWithAttachment(ecfg, email.ToEmailList, email.Subject, email.Body, email.Attachment)
    70  		return
    71  	} else {
    72  		sendemail.SendEmail(ecfg, email.ToEmailList, email.Subject, email.Body)
    73  	}
    74  }
    75  
    76  // Validate validates the inputs for sending an email.
    77  // It checks if the required fields (ToEmails, Subject, Body) are not empty.
    78  // If any of the required fields are empty, it returns false along with the empty email structure.
    79  // Otherwise, it returns true along with the populated email structure.
    80  func (cf *EmailFuncs) Validate(f *Funcs) (bool, EmailStru) {
    81  	// function execution time measurement
    82  	startTime := time.Now()
    83  	defer func() {
    84  		elapsed := time.Since(startTime)
    85  		f.iLog.PerformanceWithDuration("engine.funcs.SendEmail.Validate", elapsed)
    86  	}()
    87  
    88  	/*	// recover from any panics and log the error
    89  		defer func() {
    90  			if err := recover(); err != nil {
    91  				f.iLog.Error(fmt.Sprintf("There is an error in engine.funcs.SendEmail.Validate: %s", err))
    92  				f.ErrorMessage = fmt.Sprintf("There is an error in engine.funcs.SendEmail.Validate: %s", err)
    93  				return
    94  			}
    95  		}()
    96  	*/
    97  	// get the input values
    98  	namelist, valuelist, _ := f.SetInputs()
    99  
   100  	// create an empty email structure
   101  	email := EmailStru{}
   102  
   103  	// iterate over the input names and values
   104  	for i, name := range namelist {
   105  		value := valuelist[i]
   106  
   107  		// check if the name is "ToEmails"
   108  		if name == "ToEmails" {
   109  			if value == "" {
   110  				f.iLog.Debug(fmt.Sprintf("Error: %v", "ToEmail is empty"))
   111  			} else {
   112  				email.ToEmailList = strings.Split(value, ";")
   113  				if len(email.ToEmailList) == 0 {
   114  					f.iLog.Debug(fmt.Sprintf("Error: %v", "ToEmail is empty"))
   115  				}
   116  			}
   117  		}
   118  
   119  		// check if the name is "FromEmail"
   120  		if name == "FromEmail" {
   121  			if value == "" {
   122  				f.iLog.Debug(fmt.Sprintf("Error: %v", "FromEmail is empty"))
   123  			} else {
   124  				email.FromEmail = value
   125  			}
   126  		}
   127  
   128  		// check if the name is "Subject"
   129  		if name == "Subject" {
   130  			if value == "" {
   131  				f.iLog.Debug(fmt.Sprintf("Error: %v", "Subject is empty"))
   132  			} else {
   133  				email.Subject = value
   134  			}
   135  		}
   136  
   137  		// check if the name is "Body"
   138  		if name == "Body" {
   139  			if value == "" {
   140  				f.iLog.Debug(fmt.Sprintf("Error: %v", "Body is empty"))
   141  			} else {
   142  				email.Body = value
   143  			}
   144  		}
   145  
   146  		// check if the name is "SmtpServer"
   147  		if name == "SmtpServer" {
   148  			if value == "" {
   149  				f.iLog.Debug(fmt.Sprintf("Error: %v", "SmtpServer is empty"))
   150  			} else {
   151  				email.SmtpServer = value
   152  			}
   153  		}
   154  
   155  		// check if the name is "SmtpPort"
   156  		if name == "SmtpPort" {
   157  			if value == "" {
   158  				f.iLog.Debug(fmt.Sprintf("Error: %v", "SmtpPort is empty"))
   159  			} else {
   160  				var err error
   161  				email.SmtpPort, err = strconv.Atoi(value)
   162  				if err != nil {
   163  					f.iLog.Debug(fmt.Sprintf("Error: %v", "SmtpPort is not a number"))
   164  					email.SmtpPort = 0
   165  				}
   166  			}
   167  		}
   168  
   169  		// check if the name is "SmtpUser"
   170  		if name == "SmtpUser" {
   171  			if value == "" {
   172  				f.iLog.Debug(fmt.Sprintf("Error: %v", "SmtpUser is empty"))
   173  			} else {
   174  				email.SmtpUser = value
   175  			}
   176  		}
   177  
   178  		// check if the name is "SmtpPassword"
   179  		if name == "SmtpPassword" {
   180  			if value == "" {
   181  				f.iLog.Debug(fmt.Sprintf("Error: %v", "SmtpPassword is empty"))
   182  			} else {
   183  				email.SmtpPassword = value
   184  			}
   185  		}
   186  
   187  		// check if the name is "Attachment"
   188  		if name == "Attachment" {
   189  			if value == "" {
   190  				f.iLog.Debug(fmt.Sprintf("Error: %v", "Attachment is empty"))
   191  			} else {
   192  				email.Attachment = value
   193  			}
   194  		}
   195  	}
   196  
   197  	// check if any of the required fields are empty
   198  	if len(email.ToEmailList) == 0 || email.Subject == "" || email.Body == "" {
   199  		f.iLog.Debug(fmt.Sprintf("Error: %v", "ToEmails or Subject or Body is empty"))
   200  		return false, email
   201  	} else {
   202  		return true, email
   203  	}
   204  }