3
0

Add function to add recipients

This commit is contained in:
Tomaž Jerman 2021-08-29 10:38:23 +02:00
parent 922f4c3138
commit 03bea8d9bd
3 changed files with 118 additions and 4 deletions

View File

@ -35,6 +35,7 @@ func (h emailHandler) register() {
h.SetHeaders(),
h.SetHeader(),
h.SetAddress(),
h.AddAddress(),
h.Attach(),
h.Embed(),
)
@ -809,7 +810,7 @@ type (
}
)
// SetAddress function Email address
// SetAddress function Email set address
//
// expects implementation of setAddress function:
// func (h emailHandler) setAddress(ctx context.Context, args *emailSetAddressArgs) (err error) {
@ -821,8 +822,8 @@ func (h emailHandler) SetAddress() *atypes.Function {
Kind: "function",
Labels: map[string]string(nil),
Meta: &atypes.FunctionMeta{
Short: "Email address",
Description: "Adds new recipient, sender or reply-to address",
Short: "Email set address",
Description: "Sets the recipient, sender or reply-to addresses",
},
Parameters: []*atypes.Param{
@ -876,6 +877,89 @@ func (h emailHandler) SetAddress() *atypes.Function {
}
}
type (
emailAddAddressArgs struct {
hasMessage bool
Message *emailMessage
hasType bool
Type string
hasAddress bool
Address string
hasName bool
Name string
}
)
// AddAddress function Email add address
//
// expects implementation of addAddress function:
// func (h emailHandler) addAddress(ctx context.Context, args *emailAddAddressArgs) (err error) {
// return
// }
func (h emailHandler) AddAddress() *atypes.Function {
return &atypes.Function{
Ref: "emailAddAddress",
Kind: "function",
Labels: map[string]string(nil),
Meta: &atypes.FunctionMeta{
Short: "Email add address",
Description: "Adds new recipient, sender or reply-to address",
},
Parameters: []*atypes.Param{
{
Name: "message",
Types: []string{"EmailMessage"}, Required: true,
Meta: &atypes.ParamMeta{
Label: "Message to be sent",
},
},
{
Name: "type",
Types: []string{"String"}, Required: true,
Meta: &atypes.ParamMeta{
Label: "Type",
Description: "One of From",
},
},
{
Name: "address",
Types: []string{"String"}, Required: true,
Meta: &atypes.ParamMeta{
Label: "Address",
},
},
{
Name: "name",
Types: []string{"String"},
Meta: &atypes.ParamMeta{
Label: "Name",
},
},
},
Handler: func(ctx context.Context, in *expr.Vars) (out *expr.Vars, err error) {
var (
args = &emailAddAddressArgs{
hasMessage: in.Has("message"),
hasType: in.Has("type"),
hasAddress: in.Has("address"),
hasName: in.Has("name"),
}
)
if err = in.Decode(args); err != nil {
return
}
return out, h.addAddress(ctx, args)
},
}
}
type (
emailAttachArgs struct {
hasMessage bool

View File

@ -117,6 +117,17 @@ func (h emailHandler) setAddress(_ context.Context, args *emailSetAddressArgs) (
return nil
}
func (h emailHandler) addAddress(_ context.Context, args *emailAddAddressArgs) (err error) {
if args.Message.msg == nil {
return fmt.Errorf("email message not initialized")
}
mm := args.Message.msg.GetHeader(args.Type)
mm = append(mm, args.Message.msg.FormatAddress(args.Address, args.Name))
args.Message.msg.SetHeader(args.Type, mm...)
return nil
}
func (h emailHandler) attach(_ context.Context, args *emailAttachArgs) (err error) {
if args.Message.msg == nil {
return fmt.Errorf("email message not initialized")

View File

@ -134,7 +134,26 @@ functions:
setAddress:
kind: function
meta:
short: Email address
short: Email set address
description: Sets the recipient, sender or reply-to addresses
params:
message: *messageParam
type:
required: true
meta: { label: Type, description: One of From, ReplyTo, To, CC, BCC }
types: [ { wf: String } ]
address:
required: true
meta: { label: Address }
types: [ { wf: String } ]
name:
meta: { label: Name }
types: [ { wf: String } ]
addAddress:
kind: function
meta:
short: Email add address
description: Adds new recipient, sender or reply-to address
params:
message: *messageParam