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

Issue

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 an 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 contains:

package entity

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

Solution

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...)

Answered By – Bayta Darell

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.