Skip to content

Commit cbbcbb3

Browse files
committed
Added the feature of smtp configuration
1 parent dd3cd0e commit cbbcbb3

11 files changed

Lines changed: 742 additions & 2 deletions

File tree

api/deployment.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ type CoreAppData struct {
8383
} `json:"apps"`
8484
}
8585

86+
type SmtpConfigSchema struct {
87+
FromEmailId string `json:"FromEmailId"`
88+
FromName string `json:"FromName"`
89+
IsSsl bool `json:"IsSsl"`
90+
Key string `json:"Key"`
91+
Password string `json:"Password"`
92+
Provider string `json:"provider"`
93+
Secret string `json:"Secret"`
94+
SmtpHost string `json:"SmtpHost"`
95+
SmtpPort int `json:"SmtpPort"`
96+
UserName string `json:"UserName"`
97+
}
98+
99+
type VerifySmtpConfigSchema struct {
100+
SmtpConfigSchema
101+
EmailId string `json:"emailId"`
102+
Message string `json:"message"`
103+
Subject string `json:"subject"`
104+
}
105+
106+
type VerifySmtpConfigError struct {
107+
Description string `json:"description"`
108+
Message string `json:"message"`
109+
}
110+
86111
func GetSites() (*SitesReponse, error) {
87112
var url string
88113
url = conf.AdminConsoleAPIDomain + "/deployment/sites?ownerUid=&"
@@ -228,3 +253,61 @@ func CardPay() (bool, error) {
228253
}
229254
return true, nil
230255
}
256+
257+
func GetSMTPConfiguration() (*SmtpConfigSchema, error) {
258+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/config?"
259+
260+
var resultResp SmtpConfigSchema
261+
resp, err := request.Rest(http.MethodGet, url, nil, "")
262+
if err != nil {
263+
return nil, err
264+
}
265+
266+
err = json.Unmarshal(resp, &resultResp)
267+
if err != nil {
268+
return nil, err
269+
}
270+
return &resultResp, nil
271+
}
272+
273+
func AddSMTPConfiguration(data SmtpConfigSchema) (*SmtpConfigSchema, error) {
274+
var url string
275+
if strings.ToLower(data.Provider) == "mailazy"{
276+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/smtpprovider?"
277+
} else {
278+
url = conf.AdminConsoleAPIDomain + "/deployment/smtp-settings?"
279+
}
280+
body, _ := json.Marshal(data)
281+
var resultResp SmtpConfigSchema
282+
resp, err := request.Rest(http.MethodPost, url, nil, string(body))
283+
if err != nil {
284+
return nil, err
285+
}
286+
err = json.Unmarshal(resp, &resultResp)
287+
if err != nil {
288+
return nil, err
289+
}
290+
return &resultResp, nil
291+
}
292+
293+
func VerifySMTPConfiguration(data VerifySmtpConfigSchema) error {
294+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/verifysmtpsettings?"
295+
body, _ := json.Marshal(data)
296+
297+
_, err := request.Rest(http.MethodPost, url, nil, string(body))
298+
299+
if err != nil {
300+
return err
301+
}
302+
return nil
303+
}
304+
305+
func DeleteSMTPConfiguration() error {
306+
url := conf.AdminConsoleAPIDomain + "/deployment/smtp-settings/reset?"
307+
308+
_, err := request.Rest(http.MethodPost, url, nil, "")
309+
if err != nil {
310+
return err
311+
}
312+
return nil
313+
}

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/loginradius/lr-cli/cmd/add/hooks"
1010
"github.com/loginradius/lr-cli/cmd/add/social"
1111
"github.com/loginradius/lr-cli/cmd/add/accessRestriction"
12+
"github.com/loginradius/lr-cli/cmd/add/smtpConfiguration"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -48,5 +49,8 @@ func NewaddCmd() *cobra.Command {
4849
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
4950
cmd.AddCommand(accessRestrictionCmd)
5051

52+
smtpConfigurationCmd := smtpConfiguration.NewsmtpConfigurationCmd()
53+
cmd.AddCommand(smtpConfigurationCmd)
54+
5155
return cmd
5256
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package smtpConfiguration
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strings"
8+
"reflect"
9+
"github.com/MakeNowJust/heredoc"
10+
"github.com/AlecAivazis/survey/v2"
11+
"github.com/loginradius/lr-cli/api"
12+
"strconv"
13+
"github.com/loginradius/lr-cli/cmdutil"
14+
"github.com/loginradius/lr-cli/prompt"
15+
"github.com/loginradius/lr-cli/config"
16+
17+
18+
"github.com/spf13/cobra"
19+
)
20+
21+
22+
var conf = config.GetInstance()
23+
24+
25+
func NewsmtpConfigurationCmd() *cobra.Command {
26+
27+
cmd := &cobra.Command{
28+
Use: "smtp-configuration",
29+
Short: "Add a SMTP configuration",
30+
Long: `Configure your SMTP email settings to allow LoginRadius to send email from your email server automatically.`,
31+
Example: heredoc.Doc(`$ lr add smtp-configuration
32+
SMTP Providers : <Provider>
33+
(If you dont have mailazy account. Please Create a mailazy account via https://app.mailazy.com/signup)(only for mailazy)
34+
? Key: dgfdfg? Key: <Key>(only for mailazy)
35+
? Secret: <Secret>(only for mailazy)
36+
? From Name: <Name>
37+
? From Email Id: <Email ID>
38+
39+
? SMTP User Name: <User name>(not for mailazy)
40+
? SMTP Password: <Password>(not for mailazy)
41+
? Enable SSL(Y/N): Yes(not for mailazy)
42+
43+
SMTP settings saved
44+
? Configure and send an email to verify your configuration settings are correct (Y/N): Yes
45+
? To Email : <Email ID for Verification>
46+
SMTP settings are verified
47+
`),
48+
RunE: func(cmd *cobra.Command, args []string) error {
49+
return addAccessRestriction()
50+
},
51+
}
52+
53+
return cmd
54+
}
55+
56+
func addAccessRestriction() error {
57+
var smtpSchema api.SmtpConfigSchema
58+
var smtpLabels = [] string {"Provider","Key","Secret", "SmtpHost", "SmtpPort",
59+
"FromName","FromEmailId","UserName","Password", "IsSsl"}
60+
var smtpProviders []string
61+
var num int
62+
var isSsl bool
63+
var isMailazy bool
64+
num = len(cmdutil.SMTP_PROVIDERS)
65+
for i := 0; i < num; i++ {
66+
smtpProviders = append(smtpProviders, cmdutil.SMTP_PROVIDERS[i].Name)
67+
}
68+
for _,val := range smtpLabels {
69+
70+
if val == "Provider" {
71+
err := prompt.SurveyAskOne(&survey.Select{
72+
Message: cmdutil.SmtpOptionNames[val] + " :",
73+
Options: smtpProviders,
74+
}, &num)
75+
if err != nil {
76+
return err
77+
}
78+
} else if val == "IsSsl" && num != 0 {
79+
isSsl = cmdutil.SMTP_PROVIDERS[num].EnableSSL
80+
if err := prompt.Confirm(cmdutil.SmtpOptionNames[val] + "(Y/N):",
81+
&isSsl); err != nil {
82+
return err
83+
}
84+
} else {
85+
if num == 0 {
86+
isMailazy = ( val != "Password" && val != "IsSsl" && val != "UserName" && val != "SmtpHost" && val != "SmtpPort")
87+
} else if num == 9 {
88+
isMailazy = (val != "Key" && val != "Secret" )
89+
} else if num != 0 {
90+
isMailazy = (val != "Key" && val != "Secret" && val != "SmtpHost" && val != "SmtpPort")
91+
}
92+
if isMailazy == true {
93+
configObj := reflect.ValueOf(&smtpSchema).Elem()
94+
field := configObj.FieldByName(val)
95+
var promptRes string
96+
97+
if val == "Key" {
98+
fmt.Println("(If you dont have mailazy account. Please Create a mailazy account via https://app.mailazy.com/signup)")
99+
}
100+
prompt.SurveyAskOne(&survey.Input{
101+
Message: cmdutil.SmtpOptionNames[val] + ":",
102+
}, &promptRes, survey.WithValidator(survey.Required))
103+
104+
if strings.TrimSpace(promptRes) == "" {
105+
return errors.New(cmdutil.SmtpOptionNames[val] + " is required")
106+
}
107+
if val == "FromEmailId" && !cmdutil.ValidateEmail.MatchString(promptRes) {
108+
return &cmdutil.FlagError{Err: errors.New("Email has invalid format.")}
109+
}
110+
if val == "SmtpPort" {
111+
smtpPort, err := strconv.ParseInt(promptRes, 10, 64)
112+
if err != nil {
113+
return &cmdutil.FlagError{Err: errors.New("Please enter valid SMTP Port")}
114+
}
115+
field.SetInt(smtpPort)
116+
} else {
117+
field.SetString(strings.TrimSpace(promptRes))
118+
}
119+
}
120+
}
121+
}
122+
if num != 9 {
123+
smtpSchema.Provider = smtpProviders[num]
124+
}
125+
smtpSchema.IsSsl = isSsl
126+
127+
if num == 0 {
128+
if strings.Contains(conf.HubPageDomain ,"devhub.") {
129+
smtpSchema.SmtpHost = "devsmtp.mailazy.com";
130+
smtpSchema.SmtpPort = 588;
131+
} else {
132+
smtpSchema.SmtpHost = "smtp.mailazy.com"
133+
smtpSchema.SmtpPort = 587;
134+
}
135+
smtpSchema.UserName = smtpSchema.Key
136+
smtpSchema.Password = smtpSchema.Secret
137+
smtpSchema.IsSsl = cmdutil.SMTP_PROVIDERS[num].EnableSSL
138+
139+
} else if num != 9 {
140+
var err error
141+
smtpSchema.SmtpHost = cmdutil.SMTP_PROVIDERS[num].SmtpHost
142+
smtpSchema.SmtpPort,err = strconv.Atoi(cmdutil.SMTP_PROVIDERS[num].SmtpPort)
143+
if err != nil {
144+
return err
145+
}
146+
}
147+
148+
var _, err = api.AddSMTPConfiguration(smtpSchema)
149+
if err != nil {
150+
return err
151+
}
152+
fmt.Println("SMTP settings saved")
153+
var verify bool
154+
155+
if err := prompt.Confirm("Configure and send an email to verify your configuration settings are correct (Y/N):",
156+
&verify); err != nil {
157+
return err
158+
}
159+
if !verify {
160+
return nil
161+
}
162+
var emailid string
163+
var verifySchema api.VerifySmtpConfigSchema
164+
165+
prompt.SurveyAskOne(&survey.Input{
166+
Message: "To Email :",
167+
}, &emailid, survey.WithValidator(survey.Required))
168+
169+
var respMap map[string]string
170+
data, _ := json.Marshal(smtpSchema)
171+
json.Unmarshal(data, &respMap)
172+
173+
for _, val := range smtpLabels {
174+
if val != "IsSsl" && val != "SmtpPort" {
175+
configObj := reflect.ValueOf(&verifySchema).Elem()
176+
field := configObj.FieldByName(val)
177+
field.SetString(respMap[val])
178+
}
179+
}
180+
181+
verifySchema.EmailId = emailid
182+
verifySchema.Message = "This is the test email to validate your SMTP credentials for LoginRadius' User Registration feature on your website. <br><br>The SMTP server credentials are verified.<br><br>Thank you,<br>LoginRadius Team"
183+
verifySchema.Subject = "Test Email - LoginRadius"
184+
verifySchema.SmtpPort = smtpSchema.SmtpPort
185+
err = api.VerifySMTPConfiguration(verifySchema)
186+
if err != nil {
187+
return err
188+
}
189+
fmt.Println("SMTP settings are verified")
190+
191+
192+
return nil
193+
194+
}

cmd/delete/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/loginradius/lr-cli/cmd/delete/social"
1111
"github.com/loginradius/lr-cli/cmd/delete/accessRestriction"
12+
"github.com/loginradius/lr-cli/cmd/delete/smtpConfiguration"
1213

1314
"github.com/spf13/cobra"
1415
)
@@ -48,5 +49,8 @@ func NewdeleteCmd() *cobra.Command {
4849
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
4950
cmd.AddCommand(accessRestrictionCmd)
5051

52+
smtpConfigurationCmd := smtpConfiguration.NewsmtpConfigurationCmd()
53+
cmd.AddCommand(smtpConfigurationCmd)
54+
5155
return cmd
5256
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package smtpConfiguration
2+
3+
import (
4+
5+
"fmt"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/loginradius/lr-cli/api"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func NewsmtpConfigurationCmd() *cobra.Command {
14+
15+
16+
cmd := &cobra.Command{
17+
Use: "smtp-configuration",
18+
Short: "Delete/Reset the SMTP Configuration",
19+
Long: `Use this command to remove/reset the configured SMTP email setting.`,
20+
Example: heredoc.Doc(`$ lr delete smtp-configuration
21+
Settings have been reset
22+
`),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
25+
err := api.DeleteSMTPConfiguration()
26+
if err != nil {
27+
return nil
28+
}
29+
fmt.Println("Settings have been reset")
30+
return nil
31+
32+
},
33+
}
34+
35+
return cmd
36+
}

cmd/get/get.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/loginradius/lr-cli/cmd/get/social"
1717
"github.com/loginradius/lr-cli/cmd/get/theme"
1818
"github.com/loginradius/lr-cli/cmd/get/accessRestriction"
19+
"github.com/loginradius/lr-cli/cmd/get/smtpConfiguration"
1920

2021
"github.com/spf13/cobra"
2122
)
@@ -70,5 +71,7 @@ func NewGetCmd() *cobra.Command {
7071
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
7172
cmd.AddCommand(accessRestrictionCmd)
7273

74+
smtpConfigurationCmd := smtpConfiguration.NewsmtpConfigurationCmd()
75+
cmd.AddCommand(smtpConfigurationCmd)
7376
return cmd
7477
}

0 commit comments

Comments
 (0)