英文:
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...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论