-
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.
lesson 59: Send Email in Go using Gmail
- Loading branch information
1 parent
72a9ac1
commit 82e1f0e
Showing
7 changed files
with
108 additions
and
1 deletion.
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,66 @@ | ||
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, fromEmailAddress, 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 _, file := range attachFiles { | ||
_, err := e.AttachFile(file) | ||
if err != nil { | ||
return fmt.Errorf("failed to attach file - %s: %w", file, 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,28 @@ | ||
package mail | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/blessedmadukoma/go-simple-bank/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSendEmailWithGmail(t *testing.T) { | ||
config, err := util.LoadConfig("../") | ||
require.NoError(t, err) | ||
|
||
sender := NewGmailSender(config.EmailSenderName, config.EmailSenderAddress, config.EmailSenderPassword) | ||
|
||
subject := "Hello from Go!" | ||
content := ` | ||
<h1>This is a test email from Go!</h1> | ||
<p>Feel free to delete this email.</p> | ||
` | ||
|
||
to := []string{"bmadukoma@gmail.com"} | ||
files := []string{"../go.mod"} | ||
|
||
err = sender.SendEmail(subject, content, to, nil, nil, files) | ||
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