亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

不能使用通知(類型為 [] 實體的變量。Notif) 作為數組或切片文本中的字符串值

不能使用通知(類型為 [] 實體的變量。Notif) 作為數組或切片文本中的字符串值

Go
楊魅力 2022-10-04 19:23:29
我想從數據庫獲取電子郵件數據以發送包含該數據的電子郵件。此代碼來自控制器/通知控制器.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!")}我在 中遇到錯誤:不能使用通知(類型為 [] 實體的變量。Noteif) 作為數組或切片文本中的字符串值,并且不能在 的參數中使用 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, "")    }
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

編寫一個循環,將通知中的電子郵件地址作為字符串切片提?。?/p>


addresses := make([]string, len(notifs))

for i, notif := range notifs {

    addresses[i] = notif.Email

}

使用切片設置標題:


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


查看完整回答
反對 回復 2022-10-04
  • 1 回答
  • 0 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號