forked from tangled.org/core
this repo has no description
1package email 2 3import ( 4 "fmt" 5 "net" 6 "regexp" 7 "strings" 8 9 "github.com/resend/resend-go/v2" 10) 11 12type Email struct { 13 From string 14 To string 15 Subject string 16 Text string 17 Html string 18 APIKey string 19} 20 21func SendEmail(email Email) error { 22 client := resend.NewClient(email.APIKey) 23 _, err := client.Emails.Send(&resend.SendEmailRequest{ 24 From: email.From, 25 To: []string{email.To}, 26 Subject: email.Subject, 27 Text: email.Text, 28 Html: email.Html, 29 }) 30 if err != nil { 31 return fmt.Errorf("error sending email: %w", err) 32 } 33 return nil 34} 35 36func IsValidEmail(email string) bool { 37 // Basic length check 38 if len(email) < 3 || len(email) > 254 { 39 return false 40 } 41 42 // Regular expression for email validation (RFC 5322 compliant) 43 pattern := `^[a-zA-Z0-9.!#$%&'*+/=?^_\x60{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$` 44 45 // Compile regex 46 regex := regexp.MustCompile(pattern) 47 48 // Check if email matches regex pattern 49 if !regex.MatchString(email) { 50 return false 51 } 52 53 // Split email into local and domain parts 54 parts := strings.Split(email, "@") 55 domain := parts[1] 56 57 mx, err := net.LookupMX(domain) 58 if err != nil || len(mx) == 0 { 59 return false 60 } 61 62 return true 63}