Skip to content
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

[HELP] How to make senderAddress Dynamic #803

Closed
sharukhkhanajm opened this issue Jan 4, 2024 · 1 comment
Closed

[HELP] How to make senderAddress Dynamic #803

sharukhkhanajm opened this issue Jan 4, 2024 · 1 comment
Labels
type/bug Something isn't working

Comments

@sharukhkhanajm
Copy link

Code Implement

package types

import (
	"os"

	"github.com/nikoksr/notify"
	awsSes "github.com/nikoksr/notify/service/amazonses"
)

// CustomNotifier holds the notify instance and any services we've set up.
type CustomNotifier struct {
	notifier   *notify.Notify
	sesService *awsSes.AmazonSES
	// ... other services
}

// NewCustomNotifier creates a new CustomNotifier with all services initialized.
func NewCustomNotifier() (*CustomNotifier, error) {
	// Initialize the notify instance
	n := notify.New()
	var senderAddress string 

	// Initialize all services here (like sesService, discordService, etc.)

	// For AWS SES
	sesSvc, err := awsSes.New(
		os.Getenv("AWS_MAIL_ACCESS_KEY"),
		os.Getenv("AWS_MAIL_SECRET_KEY"),
		os.Getenv("AWS_MAIL_REGION"),
		senderAddress,
	)

	if err != nil {
		return nil, err // Handle the error if SES service fails to initialize
	}

	// ... initialize other services

	// Return a new CustomNotifier
	return &CustomNotifier{
		notifier:   n,
		sesService: sesSvc,
		// ... other services
	}, nil
}

// SendWithSES sends a notification via AWS SES.
func (cn *CustomNotifier) SendWithSES(s *FiberServer, receivers, subject, message string) error {
	cn.notifier.UseServices(cn.sesService)
	cn.sesService.AddReceivers(receivers)
	return cn.notifier.Send(s.Ctx, subject, message)
}

How can I make the final parameter, senderAddress, dynamic in this code?

My use case is i may use noreply@"+os.Getenv("DOMAIN_NAME") or support@"+os.Getenv("DOMAIN_NAME")

@sharukhkhanajm sharukhkhanajm added the type/bug Something isn't working label Jan 4, 2024
@nikoksr
Copy link
Owner

nikoksr commented Aug 1, 2024

Hi @sharukhkhanajm, thanks for opening this issue. This indeed is a flaw in the design of notify v1. This and other flaws are the reason I'm working on a v2 that gives you full control over everything at every stage of the service initialization and message sending processes. For now, probably the simplest fix would be to:

// ... existing code

// Make the sender address configurable for your CustomNotifier, then create different 
// instances of it depending on your needs.
func NewCustomNotifier(senderAddress string) (*CustomNotifier, error) {
    // ... unchanged
}

// rest of your existing code ...

I know this isn't a great solution, like I said, there's reasons I'm working on a new design for Notify's API. Hope this helps nonetheless. Let me know if I can help you otherwise.

@nikoksr nikoksr closed this as completed Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants