-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created the send service with mail go package
- Loading branch information
Showing
8 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package mail | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
|
||
"github.com/jordan-wright/email" | ||
) | ||
|
||
const ( | ||
smtpAuthAddress = "smtp.gmail.com" | ||
smtpServerAddress = "smtp.gmail.com:465" | ||
) | ||
|
||
|
||
type EmailSender interface { | ||
SendEmail( | ||
subject string, | ||
content string, | ||
to []string, | ||
cc []string, | ||
bcc []string, | ||
attachFiles []string, | ||
) error | ||
} | ||
|
||
type GmailSender struct { | ||
name string | ||
fromEmailAddress string | ||
fromEmailPassword string | ||
} | ||
|
||
func NewGmailSender(name string, fromEmailAdress string, fromEmailPassword string) EmailSender { | ||
return &GmailSender{ | ||
name: name, | ||
fromEmailAddress: fromEmailAdress, | ||
fromEmailPassword: fromEmailPassword, | ||
} | ||
} | ||
|
||
func (sender *GmailSender) SendEmail( | ||
subject string, | ||
content string, | ||
to []string, | ||
cc []string, | ||
bcc []string, | ||
attachFiles []string, | ||
) error { | ||
|
||
e := email.NewEmail() | ||
e.From = fmt.Sprintf("%s <%s>", sender.name, sender.fromEmailAddress) | ||
e.Subject = subject | ||
e.HTML = []byte(content) | ||
e.To = to | ||
e.Cc = cc | ||
e.Bcc = bcc | ||
|
||
for _, f := range attachFiles { | ||
_, err := e.AttachFile(f) | ||
if err != nil { | ||
return fmt.Errorf("Failed to attach file %s: %w", f, err) | ||
} | ||
} | ||
|
||
smptAuth := smtp.PlainAuth("identity string", sender.fromEmailAddress, sender.fromEmailPassword, smtpAuthAddress) | ||
return e.Send(smtpServerAddress, smptAuth) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/morka17/shiny_bank/v1/src/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSendEmailWithGmail(t *testing.T) { | ||
|
||
if testing.Short() { | ||
t.Skip() | ||
} | ||
|
||
config, err := utils.LoadConfig("../../") | ||
assert.NoError(t, err) | ||
|
||
sender := NewGmailSender(config.EmailSenderName, config.EmailSenderAddress, config.EmailSenderPassword) | ||
|
||
subject := "A test email" | ||
content := ` | ||
<h1>Hello world</h1> | ||
<p>This is a test message from <a href="https://github.com/morka17/api_bank> API Bank</a></p> | ||
` | ||
to := []string{config.EmailSenderAddress} | ||
attachFiles :=[]string{"../../readme.md"} | ||
|
||
err = sender.SendEmail(subject, content, to, nil, nil, attachFiles) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters