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

huangapple go评论85阅读模式
英文:

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

问题

我想从数据库中获取电子邮件数据,然后使用该数据发送电子邮件。

这是来自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!")
}

我在以下位置遇到错误:

无法将类型为[]entity.Notif的变量notifs用作数组或切片字面值中的字符串值,也无法将类型为[]string的变量to用作mailer.SetHeader的参数中的字符串值。

我添加了一个循环,代码如下:

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!")
}

我遇到了以下错误:

无法将类型为`[]entity.Notif`的变量`notifs`用作数组或切片字面值中的字符串值

entity/notif.go中的entity.Notif结构体中包含以下内容:

package entity

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

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"`
}

答案1

得分: 0

使用循环从notifs中提取电子邮件地址作为字符串切片:

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

使用该切片设置邮件头:

mailer.SetHeader("To", addresses...)
英文:

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

huangapple
  • 本文由 发表于 2021年10月7日 12:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69475441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定