mirror of
https://github.com/knadh/listmonk.git
synced 2025-01-10 00:08:15 +08:00
6cf43ea674
This is a major feature that builds upon the `Messenger` interface that has been in listmonk since its inception (with SMTP as the only messenger). This commit introduces a new Messenger implementation, an HTTP "postback", that can post campaign messages as a standard JSON payload to arbitrary HTTP servers. These servers can in turn push them to FCM, SMS, or any or any such upstream, enabling listmonk to be a generic campaign messenger for any type of communication, not just e-mails. Postback HTTP endpoints can be defined in settings and they can be selected on campaigns.
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package messenger
|
|
|
|
import (
|
|
"net/textproto"
|
|
|
|
"github.com/knadh/listmonk/models"
|
|
)
|
|
|
|
// Messenger is an interface for a generic messaging backend,
|
|
// for instance, e-mail, SMS etc.
|
|
type Messenger interface {
|
|
Name() string
|
|
Push(Message) error
|
|
Flush() error
|
|
Close() error
|
|
}
|
|
|
|
// Message is the message pushed to a Messenger.
|
|
type Message struct {
|
|
From string
|
|
To []string
|
|
Subject string
|
|
ContentType string
|
|
Body []byte
|
|
Headers textproto.MIMEHeader
|
|
Attachments []Attachment
|
|
|
|
Subscriber models.Subscriber
|
|
|
|
// Campaign is generally the same instance for a large number of subscribers.
|
|
Campaign *models.Campaign
|
|
}
|
|
|
|
// Attachment represents a file or blob attachment that can be
|
|
// sent along with a message by a Messenger.
|
|
type Attachment struct {
|
|
Name string
|
|
Header textproto.MIMEHeader
|
|
Content []byte
|
|
}
|
|
|
|
// MakeAttachmentHeader is a helper function that returns a
|
|
// textproto.MIMEHeader tailored for attachments, primarily
|
|
// email. If no encoding is given, base64 is assumed.
|
|
func MakeAttachmentHeader(filename, encoding string) textproto.MIMEHeader {
|
|
if encoding == "" {
|
|
encoding = "base64"
|
|
}
|
|
h := textproto.MIMEHeader{}
|
|
h.Set("Content-Disposition", "attachment; filename="+filename)
|
|
h.Set("Content-Type", "application/json; name=\""+filename+"\"")
|
|
h.Set("Content-Transfer-Encoding", encoding)
|
|
return h
|
|
}
|