@@ -15,6 +15,7 @@ import (
1515 "time"
1616
1717 "github.com/absmach/supermq"
18+ grpcEmailsV1 "github.com/absmach/supermq/api/grpc/emails/v1"
1819 grpcTokenV1 "github.com/absmach/supermq/api/grpc/token/v1"
1920 apiutil "github.com/absmach/supermq/api/http/util"
2021 smqauth "github.com/absmach/supermq/auth"
@@ -815,20 +816,38 @@ func changed(updated *string, old string) bool {
815816 return * updated != old
816817}
817818
818- func (svc service ) SendEmailWithUserId (ctx context.Context , userIds []string , from , subject , header , user , content , footer string ) error {
819- for i , userId := range userIds {
820- u , err := svc .users .RetrieveByID (ctx , userId )
821- if err != nil {
822- return errors .Wrap (svcerr .ErrViewEntity , err )
819+ func (svc service ) SendEmail (ctx context.Context , to []string , toType grpcEmailsV1.ContactType , from string , fromType grpcEmailsV1.ContactType , subject , header , user , content , footer string ) error {
820+ // Convert recipients based on contact type
821+ emails := make ([]string , len (to ))
822+ for i , contact := range to {
823+ switch toType {
824+ case grpcEmailsV1 .ContactType_CONTACT_TYPE_ID :
825+ u , err := svc .users .RetrieveByID (ctx , contact )
826+ if err != nil {
827+ return errors .Wrap (svcerr .ErrViewEntity , err )
828+ }
829+ emails [i ] = u .Email
830+ case grpcEmailsV1 .ContactType_CONTACT_TYPE_EMAIL :
831+ emails [i ] = contact
832+ default :
833+ return errors .Wrap (svcerr .ErrMalformedEntity , errors .New ("invalid contact type for recipients" ))
823834 }
824-
825- userIds [i ] = u .Email
826835 }
827836
828- inviter , err := svc .users .RetrieveByID (ctx , from )
829- if err != nil {
830- return err
837+ // Convert sender based on contact type
838+ var senderName string
839+ switch fromType {
840+ case grpcEmailsV1 .ContactType_CONTACT_TYPE_ID :
841+ inviter , err := svc .users .RetrieveByID (ctx , from )
842+ if err != nil {
843+ return errors .Wrap (svcerr .ErrViewEntity , err )
844+ }
845+ senderName = inviter .FirstName + " " + inviter .LastName
846+ case grpcEmailsV1 .ContactType_CONTACT_TYPE_EMAIL :
847+ senderName = from
848+ default :
849+ return errors .Wrap (svcerr .ErrMalformedEntity , errors .New ("invalid contact type for sender" ))
831850 }
832851
833- return svc .email .SendCustom (userIds , inviter . FirstName + " " + inviter . LastName , subject , header , user , content , footer )
852+ return svc .email .SendCustom (emails , senderName , subject , header , user , content , footer )
834853}
0 commit comments