Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some options for mail #588

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Mail interface {
// Content set the content of Mail.
Content(content Content) Mail
// From set the sender of Mail.
From(address From) Mail
From(address Address) Mail
// Queue a given Mail
Queue(mailable ...Mailable) error
// Send the Mail
Expand Down Expand Up @@ -41,15 +41,15 @@ type Queue struct {
Queue string
}

type From struct {
type Address struct {
Address string
Name string
}

type Envelope struct {
Bcc []string
Cc []string
From From
From Address
Subject string
To []string
}
51 changes: 21 additions & 30 deletions mail/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
)

type Application struct {
attaches []string
bcc []string
cc []string
clone int
config config.Config
from mail.From
html string
queue queuecontract.Queue
subject string
to []string
attachments []string
bcc []string
cc []string
clone int
config config.Config
from mail.Address
html string
queue queuecontract.Queue
subject string
to []string
}

func NewApplication(config config.Config, queue queuecontract.Queue) *Application {
Expand All @@ -30,9 +30,9 @@
}
}

func (r *Application) Attach(files []string) mail.Mail {
func (r *Application) Attach(attachments []string) mail.Mail {

Check warning on line 33 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L33

Added line #L33 was not covered by tests
instance := r.instance()
instance.attaches = files
instance.attachments = attachments

Check warning on line 35 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L35

Added line #L35 was not covered by tests

return instance
}
Expand All @@ -58,15 +58,14 @@
return instance
}

func (r *Application) From(from mail.From) mail.Mail {
func (r *Application) From(address mail.Address) mail.Mail {

Check warning on line 61 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L61

Added line #L61 was not covered by tests
instance := r.instance()
instance.from = from
instance.from = address

Check warning on line 63 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L63

Added line #L63 was not covered by tests

return instance
}

func (r *Application) Queue(mailable ...mail.Mailable) error {

if len(mailable) > 0 {
r.setUsingMailable(mailable[0])
}
Expand All @@ -79,7 +78,7 @@
{Value: r.to, Type: "[]string"},
{Value: r.cc, Type: "[]string"},
{Value: r.bcc, Type: "[]string"},
{Value: r.attaches, Type: "[]string"},
{Value: r.attachments, Type: "[]string"},

Check warning on line 81 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L81

Added line #L81 was not covered by tests
})

if len(mailable) > 0 {
Expand All @@ -100,7 +99,7 @@
if len(mailable) > 0 {
r.setUsingMailable(mailable[0])
}
return SendMail(r.config, r.subject, r.html, r.from.Address, r.from.Name, r.to, r.cc, r.bcc, r.attaches)
return SendMail(r.config, r.subject, r.html, r.from.Address, r.from.Name, r.to, r.cc, r.bcc, r.attachments)

Check warning on line 102 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L102

Added line #L102 was not covered by tests
}

func (r *Application) Subject(subject string) mail.Mail {
Expand Down Expand Up @@ -130,33 +129,25 @@
}

func (r *Application) setUsingMailable(mailable mail.Mailable) {
content := mailable.Content()
if content != nil && content.Html != "" {
if content := mailable.Content(); content != nil && content.Html != "" {

Check warning on line 132 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L132

Added line #L132 was not covered by tests
r.html = content.Html
}

envelope := mailable.Envelope()
if envelope != nil {
if len(mailable.Attachments()) > 0 {
r.attachments = mailable.Attachments()

Check warning on line 136 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L135-L136

Added lines #L135 - L136 were not covered by tests
}
if envelope := mailable.Envelope(); envelope != nil {

Check warning on line 138 in mail/application.go

View check run for this annotation

Codecov / codecov/patch

mail/application.go#L138

Added line #L138 was not covered by tests
if envelope.From.Address != "" {
r.from = envelope.From
}

if len(envelope.To) > 0 {
r.to = envelope.To
}

if len(envelope.Cc) > 0 {
r.cc = envelope.Cc
}

if len(envelope.Bcc) > 0 {
r.bcc = envelope.Bcc
}

if len(mailable.Attachments()) > 0 {
r.attaches = mailable.Attachments()
}

if envelope.Subject != "" {
r.subject = envelope.Subject
}
Expand Down
54 changes: 45 additions & 9 deletions mail/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *ApplicationTestSuite) TestSendMailBy465Port() {
Bcc([]string{testBcc}).
Attach([]string{"../logo.png"}).
Subject("Goravel Test 465").
Content(mail.Content{Html: "<h1>Hello Goravel</h1>"}).
Content(Html("<h1>Hello Goravel</h1>")).
Send())
}

Expand All @@ -70,20 +70,20 @@ func (s *ApplicationTestSuite) TestSendMailBy587Port() {
Bcc([]string{testBcc}).
Attach([]string{"../logo.png"}).
Subject("Goravel Test 587").
Content(mail.Content{Html: "<h1>Hello Goravel</h1>"}).
Content(Html("<h1>Hello Goravel</h1>")).
Send())
}

func (s *ApplicationTestSuite) TestSendMailWithFrom() {
mockConfig := mockConfig(587, s.redisPort)
app := NewApplication(mockConfig, nil)
s.Nil(app.From(mail.From{Address: testFromAddress, Name: testFromName}).
s.Nil(app.From(Address(testFromAddress, testFromName)).
To([]string{testTo}).
Cc([]string{testCc}).
Bcc([]string{testBcc}).
Attach([]string{"../logo.png"}).
Subject("Goravel Test 587 With From").
Content(mail.Content{Html: "<h1>Hello Goravel</h1>"}).
Content(Html("<h1>Hello Goravel</h1>")).
Send())
}

Expand All @@ -104,7 +104,7 @@ func (s *ApplicationTestSuite) TestQueueMail() {

app := NewApplication(mockConfig, queueFacade)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
go func(ctx context.Context) {
s.Nil(queueFacade.Worker(nil).Run())
Expand All @@ -119,11 +119,45 @@ func (s *ApplicationTestSuite) TestQueueMail() {
Bcc([]string{testBcc}).
Attach([]string{"../logo.png"}).
Subject("Goravel Test Queue").
Content(mail.Content{Html: "<h1>Hello Goravel</h1>"}).
Content(Html("<h1>Hello Goravel</h1>")).
Queue())
time.Sleep(3 * time.Second)
}

func (s *ApplicationTestSuite) TestQueueMailWithConnection() {
mockConfig := mockConfig(587, s.redisPort)
mockLog := &logmock.Log{}

queueFacade := queue.NewApplication(mockConfig, mockLog)
queueFacade.Register([]queuecontract.Job{
NewSendMailJob(mockConfig),
})

app := NewApplication(mockConfig, queueFacade)

ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
go func(ctx context.Context) {
s.Nil(queueFacade.Worker(&queuecontract.Args{
Connection: "redis",
Queue: "test",
}).Run())

for range ctx.Done() {
return
}
}(ctx)
time.Sleep(3 * time.Second)
s.Nil(app.To([]string{testTo}).
Cc([]string{testCc}).
Bcc([]string{testBcc}).
Attach([]string{"../logo.png"}).
Subject("Goravel Test Queue with connection").
Content(Html("<h1>Hello Goravel</h1>")).
Queue(Queue().OnConnection("redis").OnQueue("test")))
time.Sleep(3 * time.Second)
}

func (s *ApplicationTestSuite) TestQueueMailWithMailable() {
mockConfig := mockConfig(587, s.redisPort)
mockLog := &logmock.Log{}
Expand All @@ -135,7 +169,7 @@ func (s *ApplicationTestSuite) TestQueueMailWithMailable() {

app := NewApplication(mockConfig, queueFacade)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
go func(ctx context.Context) {
s.Nil(queueFacade.Worker(nil).Run())
Expand Down Expand Up @@ -219,14 +253,16 @@ func (m *TestMailable) Attachments() []string {
}

func (m *TestMailable) Content() *mail.Content {
return &mail.Content{Html: "<h1>Hello Goravel</h1>"}
html := Html("<h1>Hello Goravel</h1>")

return &html
}

func (m *TestMailable) Envelope() *mail.Envelope {
return &mail.Envelope{
Bcc: []string{testBcc},
Cc: []string{testCc},
From: mail.From{Address: testFromAddress, Name: testFromName},
From: Address(testFromAddress, testFromName),
Subject: "Goravel Test 587 With Mailable",
To: []string{testTo},
}
Expand Down
60 changes: 60 additions & 0 deletions mail/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package mail

import (
"github.com/goravel/framework/contracts/mail"
)

func Address(address, name string) mail.Address {
return mail.Address{
Address: address,
Name: name,

Check warning on line 10 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L7-L10

Added lines #L7 - L10 were not covered by tests
}
}

func Html(html string) mail.Content {
return mail.Content{
Html: html,

Check warning on line 16 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L14-L16

Added lines #L14 - L16 were not covered by tests
}
}

type QueueMail struct {
queue *mail.Queue
}

func Queue() *QueueMail {
return &QueueMail{
queue: &mail.Queue{},

Check warning on line 26 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L24-L26

Added lines #L24 - L26 were not covered by tests
}
}

// Attachments attach files to the mail
func (receiver *QueueMail) Attachments() []string {
return []string{}

Check warning on line 32 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}

// Content set the content of the mail
func (receiver *QueueMail) Content() *mail.Content {
return &mail.Content{}

Check warning on line 37 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L36-L37

Added lines #L36 - L37 were not covered by tests
}

// Envelope set the envelope of the mail
func (receiver *QueueMail) Envelope() *mail.Envelope {
return &mail.Envelope{}

Check warning on line 42 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

// Queue set the queue of the mail
func (receiver *QueueMail) Queue() *mail.Queue {
return receiver.queue

Check warning on line 47 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (receiver *QueueMail) OnConnection(connection string) *QueueMail {
receiver.queue.Connection = connection

Check warning on line 51 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L50-L51

Added lines #L50 - L51 were not covered by tests

return receiver

Check warning on line 53 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L53

Added line #L53 was not covered by tests
}

func (receiver *QueueMail) OnQueue(queue string) *QueueMail {
receiver.queue.Queue = queue

Check warning on line 57 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L56-L57

Added lines #L56 - L57 were not covered by tests

return receiver

Check warning on line 59 in mail/options.go

View check run for this annotation

Codecov / codecov/patch

mail/options.go#L59

Added line #L59 was not covered by tests
}
12 changes: 6 additions & 6 deletions mocks/mail/Mail.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading