mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
valid email (#278)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/micro/micro/v3/service"
|
"github.com/micro/micro/v3/service"
|
||||||
"github.com/micro/micro/v3/service/client"
|
"github.com/micro/micro/v3/service/client"
|
||||||
@@ -59,12 +60,24 @@ type Email struct {
|
|||||||
spamSvc spampb.SpamService
|
spamSvc spampb.SpamService
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Email) Send(ctx context.Context, request *pb.SendRequest, response *pb.SendResponse) error {
|
// validEmail does very light validation
|
||||||
if len(request.From) == 0 {
|
func validEmail(email string) bool {
|
||||||
return errors.BadRequest("email.send.validation", "Missing from address")
|
if len(email) == 0 {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
if len(request.To) == 0 {
|
m, err := regexp.MatchString("^\\S+@\\S+$", email)
|
||||||
return errors.BadRequest("email.send.validation", "Missing to address")
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Email) Send(ctx context.Context, request *pb.SendRequest, response *pb.SendResponse) error {
|
||||||
|
if !validEmail(request.From) {
|
||||||
|
return errors.BadRequest("email.send.validation", "Invalid from address")
|
||||||
|
}
|
||||||
|
if !validEmail(request.To) {
|
||||||
|
return errors.BadRequest("email.send.validation", "Invalid to address")
|
||||||
}
|
}
|
||||||
if len(request.Subject) == 0 {
|
if len(request.Subject) == 0 {
|
||||||
return errors.BadRequest("email.send.validation", "Missing subject")
|
return errors.BadRequest("email.send.validation", "Missing subject")
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -36,6 +36,7 @@ require (
|
|||||||
github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163
|
github.com/micro/micro-go v0.0.0-20211101221015-79ab982f8163
|
||||||
github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280
|
github.com/micro/micro/v3 v3.7.1-0.20211111170433-1ebb8328e280
|
||||||
github.com/miekg/dns v1.1.31 // indirect
|
github.com/miekg/dns v1.1.31 // indirect
|
||||||
|
github.com/onsi/gomega v1.10.5
|
||||||
github.com/oschwald/geoip2-golang v1.5.0
|
github.com/oschwald/geoip2-golang v1.5.0
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33
|
github.com/paulmach/go.geo v0.0.0-20180829195134-22b514266d33
|
||||||
|
|||||||
Reference in New Issue
Block a user