mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
feat: fix SendgridEmailProvider error handling, fix send-email template
This commit is contained in:
@ -113,10 +113,13 @@ func (c *ApiController) SendEmail() {
|
|||||||
|
|
||||||
content := emailForm.Content
|
content := emailForm.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
code := "123456"
|
content = provider.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
code := "123456"
|
||||||
// "You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes."
|
// "You have requested a verification code at Casdoor. Here is your code: %s, please enter in 5 minutes."
|
||||||
content = strings.Replace(provider.Content, "%s", code, 1)
|
content = strings.Replace(content, "%s", code, 1)
|
||||||
|
userString := "Hi"
|
||||||
if !strings.HasPrefix(userId, "app/") {
|
if !strings.HasPrefix(userId, "app/") {
|
||||||
var user *object.User
|
var user *object.User
|
||||||
user, err = object.GetUser(userId)
|
user, err = object.GetUser(userId)
|
||||||
@ -124,14 +127,11 @@ func (c *ApiController) SendEmail() {
|
|||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userString := "Hi"
|
|
||||||
if user != nil {
|
if user != nil {
|
||||||
userString = user.GetFriendlyName()
|
userString = user.GetFriendlyName()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
content = strings.Replace(content, "%{user.friendlyName}", userString, 1)
|
content = strings.Replace(content, "%{user.friendlyName}", userString, 1)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, receiver := range emailForm.Receivers {
|
for _, receiver := range emailForm.Receivers {
|
||||||
err = object.SendEmail(provider, emailForm.Title, content, receiver, emailForm.Sender)
|
err = object.SendEmail(provider, emailForm.Title, content, receiver, emailForm.Sender)
|
||||||
|
@ -15,6 +15,10 @@
|
|||||||
package email
|
package email
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sendgrid/sendgrid-go"
|
"github.com/sendgrid/sendgrid-go"
|
||||||
"github.com/sendgrid/sendgrid-go/helpers/mail"
|
"github.com/sendgrid/sendgrid-go/helpers/mail"
|
||||||
)
|
)
|
||||||
@ -23,6 +27,14 @@ type SendgridEmailProvider struct {
|
|||||||
ApiKey string
|
ApiKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SendgridResponseBody struct {
|
||||||
|
Errors []struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
Field interface{} `json:"field"`
|
||||||
|
Help interface{} `json:"help"`
|
||||||
|
} `json:"errors"`
|
||||||
|
}
|
||||||
|
|
||||||
func NewSendgridEmailProvider(apiKey string) *SendgridEmailProvider {
|
func NewSendgridEmailProvider(apiKey string) *SendgridEmailProvider {
|
||||||
return &SendgridEmailProvider{ApiKey: apiKey}
|
return &SendgridEmailProvider{ApiKey: apiKey}
|
||||||
}
|
}
|
||||||
@ -32,6 +44,25 @@ func (s *SendgridEmailProvider) Send(fromAddress string, fromName, toAddress str
|
|||||||
to := mail.NewEmail("", toAddress)
|
to := mail.NewEmail("", toAddress)
|
||||||
message := mail.NewSingleEmail(from, subject, to, "", content)
|
message := mail.NewSingleEmail(from, subject, to, "", content)
|
||||||
client := sendgrid.NewSendClient(s.ApiKey)
|
client := sendgrid.NewSendClient(s.ApiKey)
|
||||||
_, err := client.Send(message)
|
response, err := client.Send(message)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if response.StatusCode >= 300 {
|
||||||
|
var responseBody SendgridResponseBody
|
||||||
|
err = json.Unmarshal([]byte(response.Body), &responseBody)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
messages := []string{}
|
||||||
|
for _, sendgridError := range responseBody.Errors {
|
||||||
|
messages = append(messages, sendgridError.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("SendGrid status code: %d, error message: %s", response.StatusCode, strings.Join(messages, " | "))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -110,6 +110,26 @@ class VerificationListPage extends BaseListPage {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: i18next.t("general:Client IP"),
|
||||||
|
dataIndex: "remoteAddr",
|
||||||
|
key: "remoteAddr",
|
||||||
|
width: "100px",
|
||||||
|
sorter: true,
|
||||||
|
...this.getColumnSearchProps("remoteAddr"),
|
||||||
|
render: (text, record, index) => {
|
||||||
|
let clientIp = text;
|
||||||
|
if (clientIp.endsWith(": ")) {
|
||||||
|
clientIp = clientIp.slice(0, -2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<a target="_blank" rel="noreferrer" href={`https://db-ip.com/${clientIp}`}>
|
||||||
|
{clientIp}
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: i18next.t("verification:Receiver"),
|
title: i18next.t("verification:Receiver"),
|
||||||
dataIndex: "receiver",
|
dataIndex: "receiver",
|
||||||
|
Reference in New Issue
Block a user