Skip to content

Commit 37d93a5

Browse files
authored
feat: update SendgridEmailProvider to support dynamic host/path, add From name field (casdoor#3576)
* feat: add fields into UI FromName, Host, Endpoint * feat: update SendgridEmailProvider support dynamic host/path client init, code convention
1 parent e926a07 commit 37d93a5

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

email/provider.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func GetEmailProvider(typ string, clientId string, clientSecret string, host str
2424
} else if typ == "Custom HTTP Email" {
2525
return NewHttpEmailProvider(endpoint, method)
2626
} else if typ == "SendGrid" {
27-
return NewSendgridEmailProvider(clientSecret)
27+
return NewSendgridEmailProvider(clientSecret, host, endpoint)
2828
} else {
2929
return NewSmtpEmailProvider(clientId, clientSecret, host, port, typ, disableSsl)
3030
}

email/sendgrid.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ package email
1717
import (
1818
"encoding/json"
1919
"fmt"
20-
"strings"
20+
"net/http"
2121

2222
"github.com/sendgrid/sendgrid-go"
2323
"github.com/sendgrid/sendgrid-go/helpers/mail"
2424
)
2525

2626
type SendgridEmailProvider struct {
27-
ApiKey string
27+
ApiKey string
28+
Host string
29+
Endpoint string
2830
}
2931

3032
type SendgridResponseBody struct {
@@ -35,23 +37,25 @@ type SendgridResponseBody struct {
3537
} `json:"errors"`
3638
}
3739

38-
func NewSendgridEmailProvider(apiKey string) *SendgridEmailProvider {
39-
return &SendgridEmailProvider{ApiKey: apiKey}
40+
func NewSendgridEmailProvider(apiKey string, host string, endpoint string) *SendgridEmailProvider {
41+
return &SendgridEmailProvider{ApiKey: apiKey, Host: host, Endpoint: endpoint}
4042
}
4143

42-
func (s *SendgridEmailProvider) Send(fromAddress string, fromName, toAddress string, subject string, content string) error {
44+
func (s *SendgridEmailProvider) Send(fromAddress string, fromName string, toAddress string, subject string, content string) error {
45+
client := s.initSendgridClient()
46+
4347
from := mail.NewEmail(fromName, fromAddress)
4448
to := mail.NewEmail("", toAddress)
4549
message := mail.NewSingleEmail(from, subject, to, "", content)
46-
client := sendgrid.NewSendClient(s.ApiKey)
47-
response, err := client.Send(message)
50+
51+
resp, err := client.Send(message)
4852
if err != nil {
4953
return err
5054
}
5155

52-
if response.StatusCode >= 300 {
56+
if resp.StatusCode >= 300 {
5357
var responseBody SendgridResponseBody
54-
err = json.Unmarshal([]byte(response.Body), &responseBody)
58+
err = json.Unmarshal([]byte(resp.Body), &responseBody)
5559
if err != nil {
5660
return err
5761
}
@@ -61,8 +65,23 @@ func (s *SendgridEmailProvider) Send(fromAddress string, fromName, toAddress str
6165
messages = append(messages, sendgridError.Message)
6266
}
6367

64-
return fmt.Errorf("SendGrid status code: %d, error message: %s", response.StatusCode, strings.Join(messages, " | "))
68+
return fmt.Errorf("status code: %d, error message: %s", resp.StatusCode, messages)
69+
}
70+
71+
if resp.StatusCode != http.StatusAccepted {
72+
return fmt.Errorf("status code: %d", resp.StatusCode)
6573
}
6674

6775
return nil
6876
}
77+
78+
func (s *SendgridEmailProvider) initSendgridClient() *sendgrid.Client {
79+
if s.Host == "" || s.Endpoint == "" {
80+
return sendgrid.NewSendClient(s.ApiKey)
81+
}
82+
83+
request := sendgrid.GetRequest(s.ApiKey, s.Endpoint, s.Host)
84+
request.Method = "POST"
85+
86+
return &sendgrid.Client{Request: request}
87+
}

web/src/ProviderEditPage.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ class ProviderEditPage extends React.Component {
822822
</Col>
823823
</Row>
824824
{
825-
(this.state.provider.type === "WeChat Pay" || this.state.provider.type === "CUCloud") || (this.state.provider.category === "Email" && (this.state.provider.type === "Azure ACS" || this.state.provider.type === "SendGrid")) ? null : (
825+
(this.state.provider.type === "WeChat Pay" || this.state.provider.type === "CUCloud") || (this.state.provider.category === "Email" && (this.state.provider.type === "Azure ACS")) ? null : (
826826
<Row style={{marginTop: "20px"}} >
827827
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
828828
{this.getClientSecret2Label(this.state.provider)} :
@@ -908,7 +908,7 @@ class ProviderEditPage extends React.Component {
908908
</Row>
909909
)
910910
}
911-
{this.state.provider.category === "Storage" || ["Custom HTTP SMS", "Custom HTTP Email", "CUCloud"].includes(this.state.provider.type) ? (
911+
{this.state.provider.category === "Storage" || ["Custom HTTP SMS", "Custom HTTP Email", "SendGrid", "CUCloud"].includes(this.state.provider.type) ? (
912912
<div>
913913
{["Local File System", "CUCloud"].includes(this.state.provider.type) ? null : (
914914
<Row style={{marginTop: "20px"}} >
@@ -922,7 +922,7 @@ class ProviderEditPage extends React.Component {
922922
</Col>
923923
</Row>
924924
)}
925-
{["Custom HTTP SMS", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo", "Synology", "Casdoor", "CUCloud"].includes(this.state.provider.type) ? null : (
925+
{["Custom HTTP SMS", "SendGrid", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo", "Synology", "Casdoor", "CUCloud"].includes(this.state.provider.type) ? null : (
926926
<Row style={{marginTop: "20px"}} >
927927
<Col style={{marginTop: "5px"}} span={2}>
928928
{Setting.getLabel(i18next.t("provider:Endpoint (Intranet)"), i18next.t("provider:Region endpoint for Intranet"))} :
@@ -934,7 +934,7 @@ class ProviderEditPage extends React.Component {
934934
</Col>
935935
</Row>
936936
)}
937-
{["Custom HTTP SMS", "Local File System", "CUCloud"].includes(this.state.provider.type) ? null : (
937+
{["Custom HTTP SMS", "SendGrid", "Local File System", "CUCloud"].includes(this.state.provider.type) ? null : (
938938
<Row style={{marginTop: "20px"}} >
939939
<Col style={{marginTop: "5px"}} span={2}>
940940
{["Casdoor"].includes(this.state.provider.type) ?
@@ -948,7 +948,7 @@ class ProviderEditPage extends React.Component {
948948
</Col>
949949
</Row>
950950
)}
951-
{["Custom HTTP SMS", "CUCloud"].includes(this.state.provider.type) ? null : (
951+
{["Custom HTTP SMS", "SendGrid", "CUCloud"].includes(this.state.provider.type) ? null : (
952952
<Row style={{marginTop: "20px"}} >
953953
<Col style={{marginTop: "5px"}} span={2}>
954954
{Setting.getLabel(i18next.t("provider:Path prefix"), i18next.t("provider:Path prefix - Tooltip"))} :
@@ -960,7 +960,7 @@ class ProviderEditPage extends React.Component {
960960
</Col>
961961
</Row>
962962
)}
963-
{["Custom HTTP SMS", "Synology", "Casdoor", "CUCloud"].includes(this.state.provider.type) ? null : (
963+
{["Custom HTTP SMS", "SendGrid", "Synology", "Casdoor", "CUCloud"].includes(this.state.provider.type) ? null : (
964964
<Row style={{marginTop: "20px"}} >
965965
<Col style={{marginTop: "5px"}} span={2}>
966966
{Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} :
@@ -1067,18 +1067,16 @@ class ProviderEditPage extends React.Component {
10671067
</React.Fragment>
10681068
) : this.state.provider.category === "Email" ? (
10691069
<React.Fragment>
1070-
{["SendGrid"].includes(this.state.provider.type) ? null : (
1071-
<Row style={{marginTop: "20px"}} >
1072-
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
1073-
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
1074-
</Col>
1075-
<Col span={22} >
1076-
<Input prefix={<LinkOutlined />} value={this.state.provider.host} onChange={e => {
1077-
this.updateProviderField("host", e.target.value);
1078-
}} />
1079-
</Col>
1080-
</Row>
1081-
)}
1070+
<Row style={{marginTop: "20px"}} >
1071+
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
1072+
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
1073+
</Col>
1074+
<Col span={22} >
1075+
<Input prefix={<LinkOutlined />} value={this.state.provider.host} onChange={e => {
1076+
this.updateProviderField("host", e.target.value);
1077+
}} />
1078+
</Col>
1079+
</Row>
10821080
{["Azure ACS", "SendGrid"].includes(this.state.provider.type) ? null : (
10831081
<Row style={{marginTop: "20px"}} >
10841082
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>

0 commit comments

Comments
 (0)