mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package smtprelay
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/hanzoai/commerce/config"
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/types/email"
|
|
"github.com/hanzoai/commerce/types/integration"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
)
|
|
|
|
type Request struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Host string `json:"host"`
|
|
Port string `json:"port"`
|
|
From email.Email `json:"mailFrom"`
|
|
To []email.Email `json:"mailTo"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
type Client struct {
|
|
endpoint string
|
|
username string
|
|
password string
|
|
settings integration.SMTPRelay
|
|
client *http.Client
|
|
}
|
|
|
|
func New(ctx context.Context, settings integration.SMTPRelay) *Client {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, time.Second*55)
|
|
defer cancel()
|
|
|
|
// Create standard HTTP client with timeout
|
|
client := &http.Client{
|
|
Timeout: time.Second * 55,
|
|
}
|
|
|
|
return &Client{
|
|
client: client,
|
|
endpoint: config.SMTPRelay.Endpoint,
|
|
username: config.SMTPRelay.Username,
|
|
password: config.SMTPRelay.Password,
|
|
settings: settings,
|
|
}
|
|
}
|
|
|
|
func (c *Client) Request(r *Request) error {
|
|
var payload *bytes.Reader
|
|
|
|
if r != nil {
|
|
payload = bytes.NewReader(json.EncodeBytes(r))
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", c.endpoint, payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.SetBasicAuth(c.username, c.password)
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
res, err := c.client.Do(req)
|
|
|
|
log.Debug(res)
|
|
|
|
return err
|
|
}
|
|
|
|
func (c *Client) Send(message *email.Message) error {
|
|
req := new(Request)
|
|
req.To = message.To
|
|
req.Msg = message.HTML
|
|
return c.Request(req)
|
|
}
|