-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add sendgrid support for single and bulk mail (#5) #8
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
module github.com/commitdev/zero-notification-service | ||
|
||
go 1.14 | ||
go 1.15 | ||
|
||
require ( | ||
github.com/gorilla/mux v1.7.3 | ||
github.com/sendgrid/rest v2.6.2+incompatible // indirect | ||
github.com/sendgrid/rest v2.6.2+incompatible | ||
github.com/sendgrid/sendgrid-go v3.7.2+incompatible | ||
github.com/spf13/viper v1.7.1 | ||
github.com/stretchr/testify v1.3.0 | ||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect | ||
golang.org/x/text v0.3.3 // indirect | ||
) |
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,50 @@ | ||
package mail | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/commitdev/zero-notification-service/internal/server" | ||
"github.com/sendgrid/rest" | ||
sendgridMail "github.com/sendgrid/sendgrid-go/helpers/mail" | ||
) | ||
|
||
type BulkSendAttempt struct { | ||
EmailAddress string | ||
Response *rest.Response | ||
Error error | ||
} | ||
|
||
type Client interface { | ||
Send(email *sendgridMail.SGMailV3) (*rest.Response, error) | ||
} | ||
|
||
// SendBulkMail sends a batch of email messages to all the specified recipients | ||
// All the calls to send mail happen in parallel, with their responses returned on the provided channel | ||
func SendBulkMail(toList []server.Recipient, from server.Sender, message server.MailMessage, client Client, responseChannel chan BulkSendAttempt) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(toList)) | ||
|
||
// Create goroutines for each send | ||
for _, to := range toList { | ||
go func(to server.Recipient) { | ||
response, err := SendIndividualMail(to, from, message, client) | ||
responseChannel <- BulkSendAttempt{to.Address, response, err} | ||
wg.Done() | ||
}(to) | ||
} | ||
// Wait on the all responses to close the channel to signal that the operation is complete | ||
go func() { | ||
wg.Wait() | ||
close(responseChannel) | ||
}() | ||
} | ||
|
||
// SendIndividualMail sends an email message | ||
func SendIndividualMail(to server.Recipient, from server.Sender, message server.MailMessage, client Client) (*rest.Response, error) { | ||
sendFrom := sendgridMail.NewEmail(from.Name, from.Address) | ||
sendTo := sendgridMail.NewEmail(to.Name, to.Address) | ||
sendMessage := sendgridMail.NewSingleEmail(sendFrom, message.Subject, sendTo, message.Body, message.RichBody) | ||
sendMessage.SetTemplateID(message.TemplateId) | ||
sendMessage.SetSendAt(int(message.ScheduleSendAtTimestamp)) | ||
return client.Send(sendMessage) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious what does this do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a way to generate constants that have a specific, unique, but unimportant value. What we're using it for is to be able to refer to config options using a specific
const
likePort
rather than just a string like"port"
which allows us to be more precise about our type checking, avoid typos, etc.I had to add
rune
in there because something changed in go 1.15 which caused a warning due to the conversion from int directly to string. It doesn't really matter in our situation because, again, the value itself is unimportant, as long as it's a unique string.