2023-12-14 22:35:25 +08:00
|
|
|
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
2025-04-17 01:59:11 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-12-14 22:35:25 +08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-12-15 00:00:47 +08:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-12-14 22:35:25 +08:00
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HttpEmailProvider struct {
|
2025-04-13 23:52:04 +08:00
|
|
|
endpoint string
|
|
|
|
method string
|
|
|
|
httpHeaders map[string]string
|
2025-04-17 01:59:11 +08:00
|
|
|
bodyMapping map[string]string
|
|
|
|
contentType string
|
2023-12-14 22:35:25 +08:00
|
|
|
}
|
|
|
|
|
2025-04-17 01:59:11 +08:00
|
|
|
func NewHttpEmailProvider(endpoint string, method string, httpHeaders map[string]string, bodyMapping map[string]string, contentType string) *HttpEmailProvider {
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:35:25 +08:00
|
|
|
client := &HttpEmailProvider{
|
2025-04-13 23:52:04 +08:00
|
|
|
endpoint: endpoint,
|
|
|
|
method: method,
|
|
|
|
httpHeaders: httpHeaders,
|
2025-04-17 01:59:11 +08:00
|
|
|
bodyMapping: bodyMapping,
|
|
|
|
contentType: contentType,
|
2023-12-14 22:35:25 +08:00
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HttpEmailProvider) Send(fromAddress string, fromName string, toAddress string, subject string, content string) error {
|
2023-12-15 00:00:47 +08:00
|
|
|
var req *http.Request
|
|
|
|
var err error
|
2025-04-17 01:59:11 +08:00
|
|
|
|
|
|
|
fromNameField := "fromName"
|
|
|
|
toAddressField := "toAddress"
|
|
|
|
subjectField := "subject"
|
|
|
|
contentField := "content"
|
|
|
|
|
|
|
|
for k, v := range c.bodyMapping {
|
|
|
|
switch k {
|
|
|
|
case "fromName":
|
|
|
|
fromNameField = v
|
|
|
|
case "toAddress":
|
|
|
|
toAddressField = v
|
|
|
|
case "subject":
|
|
|
|
subjectField = v
|
|
|
|
case "content":
|
|
|
|
contentField = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-13 23:52:04 +08:00
|
|
|
if c.method == "POST" || c.method == "PUT" || c.method == "DELETE" {
|
2025-04-17 01:59:11 +08:00
|
|
|
bodyMap := make(map[string]string)
|
|
|
|
bodyMap[fromNameField] = fromName
|
|
|
|
bodyMap[toAddressField] = toAddress
|
|
|
|
bodyMap[subjectField] = subject
|
|
|
|
bodyMap[contentField] = content
|
|
|
|
|
|
|
|
var fromValueBytes []byte
|
|
|
|
if c.contentType == "application/json" {
|
|
|
|
fromValueBytes, err = json.Marshal(bodyMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest(c.method, c.endpoint, bytes.NewBuffer(fromValueBytes))
|
|
|
|
} else {
|
|
|
|
formValues := url.Values{}
|
|
|
|
for k, v := range bodyMap {
|
|
|
|
formValues.Add(k, v)
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest(c.method, c.endpoint, strings.NewReader(formValues.Encode()))
|
|
|
|
}
|
|
|
|
|
2023-12-15 00:00:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-12-14 22:35:25 +08:00
|
|
|
}
|
2023-12-15 00:00:47 +08:00
|
|
|
|
2025-04-17 01:59:11 +08:00
|
|
|
req.Header.Set("Content-Type", c.contentType)
|
2023-12-14 22:35:25 +08:00
|
|
|
} else if c.method == "GET" {
|
2023-12-15 00:00:47 +08:00
|
|
|
req, err = http.NewRequest(c.method, c.endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:35:25 +08:00
|
|
|
q := req.URL.Query()
|
2025-04-17 01:59:11 +08:00
|
|
|
q.Add(fromNameField, fromName)
|
|
|
|
q.Add(toAddressField, toAddress)
|
|
|
|
q.Add(subjectField, subject)
|
|
|
|
q.Add(contentField, content)
|
2023-12-14 22:35:25 +08:00
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("HttpEmailProvider's Send() error, unsupported method: %s", c.method)
|
|
|
|
}
|
|
|
|
|
2025-04-13 23:52:04 +08:00
|
|
|
for k, v := range c.httpHeaders {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:35:25 +08:00
|
|
|
httpClient := proxy.DefaultHttpClient
|
|
|
|
resp, err := httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("HttpEmailProvider's Send() error, custom HTTP Email request failed with status: %s", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|