Skip to content

Commit

Permalink
Merge pull request #7 from DarkRockMountain/feature/refactor-provider…
Browse files Browse the repository at this point in the history
…s-package

refactor(providers): separate all email providers into individual packages
  • Loading branch information
DarkRockMountain-admin authored Jun 4, 2024
2 parents a770d81 + 46c6810 commit 864920a
Show file tree
Hide file tree
Showing 33 changed files with 71 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .examples/.serverless/aws_lambda/lambda_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/providers"
"github.com/darkrockmountain/gomail/providers/smtp"
)

// handler is the main Lambda function handler that processes the incoming API Gateway request
Expand Down Expand Up @@ -38,11 +38,11 @@ func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyRespo
}
user := os.Getenv("SMTP_USER")
password := os.Getenv("SMTP_PASSWORD")
authMethod := providers.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))
authMethod := smtp.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))

// Initialize the SMTP email sender with the retrieved configuration values.
// You can choose any other email sender implementation by replacing NewSmtpEmailSender with another constructor.
sender, err := providers.NewSmtpEmailSender(
sender, err := smtp.NewSmtpEmailSender(
host,
port,
user,
Expand Down
6 changes: 3 additions & 3 deletions .examples/.serverless/azure_functions/azure_email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"

"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/providers"
"github.com/darkrockmountain/gomail/providers/smtp"
)

// SendEmail is the HTTP handler that processes the incoming request
Expand All @@ -31,11 +31,11 @@ func SendEmail(w http.ResponseWriter, r *http.Request) {
}
user := os.Getenv("SMTP_USER")
password := os.Getenv("SMTP_PASSWORD")
authMethod := providers.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))
authMethod := smtp.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))

// Initialize the SMTP email sender with the retrieved configuration values.
// You can choose any other email sender implementation by replacing NewSmtpEmailSender with another constructor.
sender, err := providers.NewSmtpEmailSender(
sender, err := smtp.NewSmtpEmailSender(
host,
port,
user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"

"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/providers"
"github.com/darkrockmountain/gomail/providers/smtp"
)

type ResponseHeaders struct {
Expand Down Expand Up @@ -39,9 +39,9 @@ func Main(ctx context.Context, emailReq gomail.EmailMessage) Response {
}
user := os.Getenv("SMTP_USER")
password := os.Getenv("SMTP_PASSWORD")
authMethod := providers.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))
authMethod := smtp.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))

sender, err := providers.NewSmtpEmailSender(host, port, user, password, authMethod)
sender, err := smtp.NewSmtpEmailSender(host, port, user, password, authMethod)
if err != nil {
return generateResponse(http.StatusInternalServerError, "Failed to initialize email sender")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/providers"
"github.com/darkrockmountain/gomail/providers/smtp"
)

func SendEmail(w http.ResponseWriter, r *http.Request) {
Expand All @@ -30,9 +30,9 @@ func SendEmail(w http.ResponseWriter, r *http.Request) {
}
user := os.Getenv("SMTP_USER")
password := os.Getenv("SMTP_PASSWORD")
authMethod := providers.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))
authMethod := smtp.AuthMethod(os.Getenv("SMTP_AUTH_METHOD"))

sender, err = providers.NewSmtpEmailSender(
sender, err = smtp.NewSmtpEmailSender(
host,
port,
user,
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,52 +46,52 @@ go mod tidy
### Usage

#### 1. SMTP Email Sender
- Configure your SMTP server settings in `providers/smtp_email_sender.go`.
- Configure your SMTP server settings in `providers/smpt/smtp_email_sender.go`.
- Refer to the [SMTP Credentials Documentation](./docs/SMTP_Credentials.md) for details on obtaining credentials.
- Run the `smtpExample()` function to send a test email.
#### 2. Gmail Email Sender
- Configure your Gmail API credentials in `providers/gmail_email_sender.go`.
- Configure your Gmail API credentials in `providers/gmail/gmail_email_sender.go`.
- Refer to the [Gmail Credentials Documentation](./docs/Gmail_Credentials_API_Key.md) for details on obtaining credentials.
- Run the `gExample()` function to send a test email.
#### 3. Gmail Email Sender using OAuth2
- Configure your Gmail API credentials and token in `providers/gmail_email_sender_oauth2.go`.
- Configure your Gmail API credentials and token in `providers/gmail/gmail_email_sender_oauth2.go`.
- Refer to the [Gmail OAuth2 Credentials Documentation](./docs/Gmail_Credentials_OAuth2.md) for details on obtaining credentials.
- Run the `gExampleOauth2()` function to send a test email.
#### 4. Microsoft 365 Email Sender
- Configure your Microsoft Graph API credentials in `providers/microsoft365_email_sender.go`.
- Configure your Microsoft Graph API credentials in `providers/microsoft365/microsoft365_email_sender.go`.
- Refer to the [Microsoft 365 Credentials Documentation](./docs/Microsoft365_Credentials_ROPC.md) for details on obtaining credentials.
- Run the `msGraphExample()` function to send a test email.
#### 5. SendGrid Email Sender
- Configure your SendGrid API key in `providers/sendgrid_email_sender.go`.
- Configure your SendGrid API key in `providers/sendgrid/sendgrid_email_sender.go`.
- Refer to the [SendGrid Credentials Documentation](./docs/SendGrid_Credentials.md) for details on obtaining credentials.
- Run the `sendgridExample()` function to send a test email.
#### 6. AWS SES Email Sender
- Configure your AWS SES credentials in `providers/ses_email_sender.go`.
- Configure your AWS SES credentials in `providers/ses/ses_email_sender.go`.
- Refer to the [AWS SES Credentials Documentation](./docs/AWS_SES_Credentials.md) for details on obtaining credentials.
- Run the `sesExample()` function to send a test email.
#### 7. Mailgun Email Sender
- Configure your Mailgun API key in `providers/mailgun_email_sender.go`.
- Configure your Mailgun API key in `providers/mailgun/mailgun_email_sender.go`.
- Refer to the [Mailgun Credentials Documentation](./docs/Mailgun_Credentials.md) for details on obtaining credentials.
- Run the `mailgunExample()` function to send a test email.
#### 8. Mandrill Email Sender
- Configure your Mandrill API key in `providers/mandrill_email_sender.go`.
- Configure your Mandrill API key in `providers/mandrill/mandrill_email_sender.go`.
- Refer to the [Mandrill Credentials Documentation](./docs/Mandrill_Credentials.md) for details on obtaining credentials.
- Run the `mandrillExample()` function to send a test email.
#### 9. Postmark Email Sender
- Configure your Postmark API key in `providers/postmark_email_sender.go`.
- Configure your Postmark API key in `providers/postmark/postmark_email_sender.go`.
- Refer to the [Postmark Credentials Documentation](./docs/Postmark_Credentials.md) for details on obtaining credentials.
- Run the `postmarkExample()` function to send a test email.
#### 10. SparkPost Email Sender
- Configure your SparkPost API key in `providers/sparkpost_email_sender.go`.
- Configure your SparkPost API key in `providers/sparkpost/sparkpost_email_sender.go`.
- Refer to the [SparkPost Documentation](https://developers.sparkpost.com/api/) for details on obtaining credentials.
- Run the `sparkpostExample()` function to send a test email.
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_API_Key.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ apiKey := "your-api-key"
user := "me"

// Initialize the GmailEmailSender
emailSender, err := providers.NewGmailEmailSenderAPIKey(apiKey, user)
emailSender, err := gmail.NewGmailEmailSenderAPIKey(apiKey, user)
if err != nil {
log.Fatalf("Failed to create GmailEmailSender: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_JWT.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jsonCredentials := []byte(`{
user := "[email protected]"

// Initialize the GmailEmailSender
emailSender, err := providers.NewGmailEmailSenderJWT(jsonCredentials, user)
emailSender, err := gmail.NewGmailEmailSenderJWT(jsonCredentials, user)
if err != nil {
log.Fatalf("Failed to create GmailEmailSender: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions docs/Gmail_Credentials_OAuth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jsonCredentials := []byte(`{
user := "[email protected]"

// Initialize the GmailEmailSender
emailSender, err := providers.NewGmailEmailSenderServiceAccount(jsonCredentials, user)
emailSender, err := gmail.NewGmailEmailSenderServiceAccount(jsonCredentials, user)
if err != nil {
log.Fatalf("Failed to create GmailEmailSender: %v", err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ tokenManager := &MyTokenManager{
}

// Create GmailEmailSender with the parsed credentials and token
emailSender, err := providers.NewGmailEmailSenderOauth2(credentials, tokenManager, "me")
emailSender, err := gmail.NewGmailEmailSenderOauth2(credentials, tokenManager, "me")
if err != nil {
log.Fatalf("Failed to create email sender: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_ServiceAccount copy.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jsonCredentials := []byte(`{
user := "[email protected]"

// Initialize the GmailEmailSender
emailSender, err := providers.NewGmailEmailSenderJWTAccess(jsonCredentials, user)
emailSender, err := gmail.NewGmailEmailSenderJWTAccess(jsonCredentials, user)
if err != nil {
log.Fatalf("Failed to create GmailEmailSender: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/Gmail_Credentials_ServiceAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jsonCredentials := []byte(`{
user := "[email protected]"

// Initialize the GmailEmailSender
emailSender, err := providers.NewGmailEmailSenderServiceAccount(jsonCredentials, user)
emailSender, err := gmail.NewGmailEmailSenderServiceAccount(jsonCredentials, user)
if err != nil {
log.Fatalf("Failed to create GmailEmailSender: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/SMTP_Credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ password := "your-password"
authMethod := AUTH_PLAIN

// Create SmtpEmailSender with the SMTP server settings
emailSender, _ := providers.NewSmtpEmailSender(
emailSender, _ := gmail.NewSmtpEmailSender(
host,
port,
user,
Expand Down
7 changes: 4 additions & 3 deletions email_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// This project is organized into several packages:
//
// - providers: Contains implementations for various email providers.
// - credentials: Contains implementations for managing email credentials.
// - examples: Contains example applications demonstrating how to use the library.
// - docs: Contains documentation for configuring different email providers.
//
Expand All @@ -22,12 +23,12 @@
// package main
//
// import (
// "github.com/darkrockmountain/gomail/providers"
// "github.com/darkrockmountain/gomail/providers/sendgrid"
// )
//
// func main() {
// sender := providers.NewSendGridEmailSender("your-api-key")
// err := sender.SendEmail("[email protected]", "Subject", "Email body")
// sender := sendgrid.NewSendGridEmailSender("your-api-key")
// err := sender.SendEmail(EmailMessage{To:[]string{"[email protected]"}, Subject:"Subject", Text:"Email body"})
// if err != nil {
// log.Fatal(err)
// }
Expand Down
5 changes: 3 additions & 2 deletions providers/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
//
// import (
// "github.com/darkrockmountain/gomail"
// "github.com/darkrockmountain/gomail/providers/sendgrid"
// )
//
// func main() {
// sender := providers.NewSendGridEmailSender("your-api-key")
// err := sender.SendEmail("[email protected]", "Subject", "Email body")
// sender := sendgrid.NewSendGridEmailSender("your-api-key")
// err := sender.SendEmail(EmailMessage{To:[]string{"[email protected]"}, Subject:"Subject", Text:"Email body"})
// if err != nil {
// log.Fatal(err)
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package gmail

import (
"bytes"
Expand All @@ -10,6 +10,7 @@ import (

"github.com/darkrockmountain/gomail"
"github.com/darkrockmountain/gomail/credentials"
"github.com/darkrockmountain/gomail/providers"
"golang.org/x/oauth2/google"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (ms *gmailMessageSenderWrapper) send(message *gmail.Message) (*gmail.Messag
// Returns:
// - error: An error if sending the email fails.
func (s *gmailMessageSenderWrapper) SendEmail(message gomail.EmailMessage) error {
mimeMessage, err := buildMimeMessage(message)
mimeMessage, err := providers.BuildMimeMessage(message)
if err != nil {
return fmt.Errorf("unable to build MIME message: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package gmail

import (
"bytes"
Expand Down
14 changes: 7 additions & 7 deletions providers/helper_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/darkrockmountain/gomail"
)

// buildMimeMessage constructs the MIME message for the email, including text, HTML, and attachments.
// BuildMimeMessage constructs the MIME message for the email, including text, HTML, and attachments.
// This function builds a multipart MIME message based on the provided email message. It supports plain text,
// HTML content, and multiple attachments.
//
Expand All @@ -35,12 +35,12 @@ import (
// },
// },
// }
// mimeMessage, err := buildMimeMessage(message)
// mimeMessage, err := BuildMimeMessage(message)
// if err != nil {
// log.Fatalf("Failed to build MIME message: %v", err)
// }
// fmt.Println(string(mimeMessage))
func buildMimeMessage(message gomail.EmailMessage) ([]byte, error) {
func BuildMimeMessage(message gomail.EmailMessage) ([]byte, error) {
var msg bytes.Buffer

// Determine boundaries
Expand Down Expand Up @@ -127,7 +127,7 @@ func buildMimeMessage(message gomail.EmailMessage) ([]byte, error) {
return msg.Bytes(), nil
}

// strPtr takes a string value and returns a pointer to that string.
// StrPtr takes a string value and returns a pointer to that string.
// This function is useful when you need to work with string pointers, such as in
// scenarios where you need to pass a string by reference or handle optional string fields.
//
Expand All @@ -140,19 +140,19 @@ func buildMimeMessage(message gomail.EmailMessage) ([]byte, error) {
// Example usage:
//
// name := "John Doe"
// namePtr := strPtr(name)
// namePtr := StrPtr(name)
// fmt.Println(namePtr) // Output: memory address of the string
// fmt.Println(*namePtr) // Output: "John Doe"
//
// Detailed explanation:
// The strPtr function creates a pointer to the given string `str`.
// The StrPtr function creates a pointer to the given string `str`.
// This can be particularly useful in the following scenarios:
// 1. Passing strings by reference to functions, which can help avoid copying large strings.
// 2. Working with data structures that use pointers to represent optional fields or nullable strings.
// 3. Interfacing with APIs or libraries that require or return string pointers.
//
// By using this function, you can easily obtain a pointer to a string and utilize it in contexts
// where pointers are needed, thus enhancing flexibility and efficiency in your Go programs.
func strPtr(str string) *string {
func StrPtr(str string) *string {
return &str
}
2 changes: 1 addition & 1 deletion providers/helper_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestBuildMimeMessage(t *testing.T) {

for _, test := range tests {
t.Run(test.message.Subject, func(t *testing.T) {
result, err := buildMimeMessage(test.message)
result, err := BuildMimeMessage(test.message)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package mailgun

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package mailgun

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// mandrill_email_sender.go
package providers
package mandrill

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package mandrill

import (
"net/http"
Expand Down
Loading

0 comments on commit 864920a

Please sign in to comment.