Home > Software engineering >  cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal (SOLVE
cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal (SOLVE

Time:10-07

How to fix this ? I want to fetch email data from database to send email with that data. this code from controller/notif-controller.go

func (c *notifController) SendNotifEmail(context *gin.Context) {

    email_to := context.Query("sEmailByDiv")
    cc_to := context.Query("cc_to")
    subject := context.Query("subject")
    body := context.Query("body")

    notifs := c.notifService.EmailByDiv(email_to)

    to := []string{notifs}

    mailer := gomail.NewMessage()
    mailer.SetHeader("From", CONFIG_SENDER_NAME)
    mailer.SetHeader("To", to)
    mailer.SetHeader("Cc", cc_to)
    mailer.SetHeader("Subject", subject)
    mailer.SetBody("text/html", body)

    // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
    dialer := gomail.NewDialer(
        CONFIG_SMTP_HOST,
        CONFIG_SMTP_PORT,
        CONFIG_AUTH_EMAIL,
        CONFIG_AUTH_PASSWORD,
    )

    err := dialer.DialAndSend(mailer)
    if err != nil {
        log.Fatal(err.Error())
    }

    log.Println("Mail sent!")
}

I have Error in :

cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal and cannot use to (variable of type []string) as string value in argument to mailer.SetHeader

I've added a loop and it's like this :

func (c *notifController) SendNotifEmail(context *gin.Context) {

    email_to := context.Query("sEmailByDiv")
    cc_to := context.Query("cc_to")
    subject := context.Query("subject")
    body := context.Query("body")
    // file := context.Query("file")

    notifs := c.notifService.EmailByDiv(email_to)

    to := []string{notifs}

    mailer := gomail.NewMessage()

    addresses := make([]string, len(to))
    for i, recipient := range to {
        addresses[i] = mailer.FormatAddress(recipient, "")
    }

    mailer.SetHeader("From", CONFIG_SENDER_NAME)
    mailer.SetHeader("To", addresses...)
    mailer.SetHeader("Cc", cc_to)
    mailer.SetHeader("Subject", subject)
    mailer.SetBody("text/html", body)
    // mailer.Attach(file)

    // dialer := &gomail.Dialer{Host: CONFIG_SMTP_HOST, Port: CONFIG_SMTP_PORT}
    dialer := gomail.NewDialer(
        CONFIG_SMTP_HOST,
        CONFIG_SMTP_PORT,
        CONFIG_AUTH_EMAIL,
        CONFIG_AUTH_PASSWORD,
    )

    err := dialer.DialAndSend(mailer)
    if err != nil {
        log.Fatal(err.Error())
    }
    log.Println("Mail sent!")
}

and i have error like this :

cannot use notifs (variable of type []entity.Notif) as string value in array or slice literal

this file for entity.Notif from entity/notif.go contain :

package entity

type Notif struct {
    Email string `gorm:"type:varchar(255)" json:"email"`
}

CodePudding user response:

Write a loop to extract the email addresses from notifs as a slice of strings:

addresses := make([]string, len(notifs))
for i, notif := range notifs {
    addresses[i] = notif.Email
}

Set the header using the slice:

mailer.SetHeader("To", addresses...)
  • Related