BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Adjust the batch creation of email accounts (with random passwords)
lotk0b3c511e4ee7
diff --git a/core/api/mail_boxes/v1/mailboxes.go b/core/api/mail_boxes/v1/mailboxes.goindex 796b4693..a09fe1ed 100644--- a/core/api/mail_boxes/v1/mailboxes.go+++ b/core/api/mail_boxes/v1/mailboxes.go@@ -41,10 +41,10 @@ type BatchAddMailboxReq struct {g.Meta `path:"/mailbox/batch_create" tags:"MailBox" method:"post" summary:"Batch create mailbox" in:"body"`Authorization string `json:"authorization" dc:"Authorization" in:"header"`Domain string `json:"domain" v:"required|domain" dc:"Domain"`- Password string `json:"password" v:"required|min-length:8" dc:"Password"`- Quota int `json:"quota" v:"required|min:1" dc:"Quota" d:"5242880"`- Count int `json:"count" v:"required|min:2" dc:"Count" d:"10"`- Prefix string `json:"prefix" v:"regex:[\\w-]{0,}" dc:"Email name prefix, optional" d:"user"`++ Quota int `json:"quota" v:"required|min:1" dc:"Quota" d:"5242880"`+ Count int `json:"count" v:"required|min:2" dc:"Count" d:"10"`+ Prefix string `json:"prefix" v:"regex:[\\w-]{0,}" dc:"Email name prefix, optional" d:"user"`}type BatchAddMailboxRes struct {diff --git a/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go b/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.goindex ef0c1766..cc868570 100644--- a/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go+++ b/core/internal/controller/mail_boxes/mail_boxes_v1_batch_add_mailbox.go@@ -18,7 +18,7 @@ func (c *ControllerV1) BatchAddMailbox(ctx context.Context, req *v1.BatchAddMailreturn nil, fmt.Errorf("You cannot create more than 5000 batches")}- createdEmails, err := mail_boxes.BatchAdd(ctx, req.Domain, req.Password, req.Quota, req.Count, req.Prefix)+ createdEmails, err := mail_boxes.BatchAdd(ctx, req.Domain, req.Quota, req.Count, req.Prefix)if err != nil {return nil, err}diff --git a/core/internal/service/mail_boxes/mail_boxes.go b/core/internal/service/mail_boxes/mail_boxes.goindex 313f4937..aa757473 100644--- a/core/internal/service/mail_boxes/mail_boxes.go+++ b/core/internal/service/mail_boxes/mail_boxes.go@@ -198,7 +198,17 @@ func PasswdMD5Crypt(ctx context.Context, password string) (string, error) {return result, nil}-func BatchAdd(ctx context.Context, domain, password string, quota int, count int, prefix string) ([]string, error) {+func generateRandomPassword(charset string, length int) string {+ if charset == "" {+ charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"+ }+ password := make([]byte, length)+ for i := range password {+ password[i] = charset[rand.Intn(len(charset))]+ }+ return string(password)+}+func BatchAdd(ctx context.Context, domain string, quota int, count int, prefix string) ([]string, error) {if prefix == "" {randomPre := make([]byte, 4)@@ -223,8 +233,8 @@ func BatchAdd(ctx context.Context, domain, password string, quota int, count inttimestamp := time.Now().Unix()- passwordEncoded := PasswdEncode(ctx, password)- passwordCrypted, err := PasswdMD5Crypt(ctx, password)+ //passwordEncoded := PasswdEncode(ctx, password)+ //passwordCrypted, err := PasswdMD5Crypt(ctx, password)if err != nil {return nil, fmt.Errorf("Generate password md5-crypt failed: %w", err)}@@ -242,6 +252,11 @@ func BatchAdd(ctx context.Context, domain, password string, quota int, count intlocalPart := fmt.Sprintf("%s%d%s", prefix, i, string(randomChars))username := localPart + "@" + domain+ password := generateRandomPassword("", 8) // todo++ passwordEncoded := PasswdEncode(ctx, password)+ passwordCrypted, _ := PasswdMD5Crypt(ctx, password)+mailbox := v1.Mailbox{Username: username,Password: passwordCrypted,