forked from techschool/simplebank
-
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.
send email with gmail (techschool#80)
Co-authored-by: phamlequang <phamlequang@gmail.com>
- Loading branch information
1 parent
663ff51
commit 083a092
Showing
9 changed files
with
110 additions
and
6 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
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,65 @@ | ||
package mail | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
|
||
"github.com/jordan-wright/email" | ||
) | ||
|
||
const ( | ||
smtpAuthAddress = "smtp.gmail.com" | ||
smtpServerAddress = "smtp.gmail.com:587" | ||
) | ||
|
||
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, fromEmailAddress string, fromEmailPassword string) EmailSender { | ||
return &GmailSender{ | ||
name: name, | ||
fromEmailAddress: fromEmailAddress, | ||
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) | ||
} | ||
} | ||
|
||
smtpAuth := smtp.PlainAuth("", sender.fromEmailAddress, sender.fromEmailPassword, smtpAuthAddress) | ||
return e.Send(smtpServerAddress, smtpAuth) | ||
} |
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,30 @@ | ||
package mail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/techschool/simplebank/util" | ||
) | ||
|
||
func TestSendEmailWithGmail(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip() | ||
} | ||
|
||
config, err := util.LoadConfig("..") | ||
require.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="http://techschool.guru">Tech School</a></p> | ||
` | ||
to := []string{"techschool.guru@gmail.com"} | ||
attachFiles := []string{"../README.md"} | ||
|
||
err = sender.SendEmail(subject, content, to, nil, nil, attachFiles) | ||
require.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