Skip to content

Commit

Permalink
Fixed codacy golint issues
Browse files Browse the repository at this point in the history
Broke apart example into basic vs full
  • Loading branch information
mrz1836 committed Oct 12, 2019
1 parent 09e7778 commit 4509cb0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
16 changes: 8 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
AwsSes ServiceProvider = iota // AWS SES Service
Mandrill // Mandrill Email Service
Postmark // Postmark Email Service
Smtp // Send via smtp
SMTP // Send via smtp
)

const (
Expand All @@ -43,10 +43,10 @@ type MailService struct {
Important bool `json:"important" mapstructure:"important"` // whether or not this message is important, and should be delivered ahead of non-important messages
MandrillAPIKey string `json:"mandrill_api_key" mapstructure:"mandrill_api_key"` // mandrill api key
PostmarkServerToken string `json:"postmark_server_token" mapstructure:"postmark_server_token"` // ie: abc123...
SmtpHost string `json:"smtp_host" mapstructure:"smtp_host"` // ie: example.com
SmtpPassword string `json:"smtp_password" mapstructure:"smtp_password"` // ie: secretPassword
SmtpPort int `json:"smtp_port" mapstructure:"smtp_port"` // ie: 25
SmtpUsername string `json:"smtp_username" mapstructure:"smtp_username"` // ie: testuser
SMTPHost string `json:"smtp_host" mapstructure:"smtp_host"` // ie: example.com
SMTPPassword string `json:"smtp_password" mapstructure:"smtp_password"` // ie: secretPassword
SMTPPort int `json:"smtp_port" mapstructure:"smtp_port"` // ie: 25
SMTPUsername string `json:"smtp_username" mapstructure:"smtp_username"` // ie: testuser
TrackClicks bool `json:"track_clicks" mapstructure:"track_clicks"` // whether or not to turn on click tracking for the message
TrackOpens bool `json:"track_opens" mapstructure:"track_opens"` // whether or not to turn on open tracking for the message

Expand Down Expand Up @@ -122,15 +122,15 @@ func (m *MailService) StartUp() {
}

// If the smtp credentials exist
if len(m.SmtpHost) == 0 || len(m.SmtpUsername) == 0 || len(m.SmtpPassword) == 0 {
if len(m.SMTPHost) == 0 || len(m.SMTPUsername) == 0 || len(m.SMTPPassword) == 0 {
logger.Data(2, logger.DEBUG, "smtp credentials not set, skipping smtp...")
} else {

// set the credentials
m.smtpAuth = smtp.PlainAuth("", m.SmtpUsername, m.SmtpPassword, m.SmtpHost)
m.smtpAuth = smtp.PlainAuth("", m.SMTPUsername, m.SMTPPassword, m.SMTPHost)

// Add to the list of available providers
m.AvailableProviders = append(m.AvailableProviders, Smtp)
m.AvailableProviders = append(m.AvailableProviders, SMTP)
}

// No service providers found
Expand Down
4 changes: 2 additions & 2 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (m *MailService) SendEmail(email *Email, provider ServiceProvider) (err err
err = m.sendWithAwsSes(email)
} else if provider == Postmark {
err = m.sendWithPostmark(email)
} else if provider == Smtp {
err = m.sendWithSmtp(email)
} else if provider == SMTP {
err = m.sendWithSMTP(email)
}
} else {
err = fmt.Errorf("service provider: %x was not in the list of available service providers: %x, email not sent", provider, m.AvailableProviders)
Expand Down
52 changes: 44 additions & 8 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,45 @@ import (
"github.com/mrz1836/go-mail"
)

// main will load the examples
func main() {

//basicExample()

fullExample()
}

// basicExample is using the least amount of features
func basicExample() {
// Config
mail := new(gomail.MailService)
mail.FromName = "No Reply"
mail.FromUsername = "no-reply"
mail.FromDomain = os.Getenv("EMAIL_FROM_DOMAIN")

// Provider
mail.MandrillAPIKey = os.Getenv("EMAIL_MANDRILL_KEY")

// Start the service
mail.StartUp()

// Create and send a basic email
email := mail.NewEmail()
email.HTMLContent = "<html><body>This is a <b>go-mail</b> test email using <i>HTML</i></body></html>"
email.Recipients = []string{os.Getenv("EMAIL_TEST_TO_RECIPIENT")}
email.Subject = "testing go-mail package - test basicExample"

err := mail.SendEmail(email, gomail.Mandrill)
if err != nil {
logger.Data(2, logger.ERROR, fmt.Sprintf("error in SendEmail: %s using provider %x", err.Error(), gomail.Mandrill))
}

// Congrats!
logger.Data(2, logger.DEBUG, "all emails sent via basicExample()")
}

// fullExample is using the most amount of features
func fullExample() {

// Define your service configuration
mail := new(gomail.MailService)
mail.FromName = "No Reply"
Expand All @@ -32,10 +68,10 @@ func main() {
mail.PostmarkServerToken = os.Getenv("EMAIL_POSTMARK_SERVER_TOKEN") //AKIAY...

// SMTP
mail.SmtpHost = os.Getenv("EMAIL_SMTP_HOST") //example.com
mail.SmtpPort, _ = strconv.Atoi(os.Getenv("EMAIL_SMTP_PORT")) //25
mail.SmtpUsername = os.Getenv("EMAIL_SMTP_USERNAME") //johndoe
mail.SmtpPassword = os.Getenv("EMAIL_SMTP_PASSWORD") //secretPassword
mail.SMTPHost = os.Getenv("EMAIL_SMTP_HOST") //example.com
mail.SMTPPort, _ = strconv.Atoi(os.Getenv("EMAIL_SMTP_PORT")) //25
mail.SMTPUsername = os.Getenv("EMAIL_SMTP_USERNAME") //johndoe
mail.SMTPPassword = os.Getenv("EMAIL_SMTP_PASSWORD") //secretPassword

// Startup the services
mail.StartUp()
Expand All @@ -51,7 +87,7 @@ func main() {
email.Recipients = []string{os.Getenv("EMAIL_TEST_TO_RECIPIENT")}
email.RecipientsCc = []string{os.Getenv("EMAIL_TEST_CC_RECIPIENT")}
email.RecipientsBcc = []string{os.Getenv("EMAIL_TEST_BCC_RECIPIENT")}
email.Subject = "testing go-mail package - test message"
email.Subject = "testing go-mail package - test fullExample"
email.Tags = []string{"admin_alert"}
email.Important = true

Expand All @@ -64,12 +100,12 @@ func main() {
}

// Send the email (basic example using one provider)
provider := gomail.Smtp // AwsSes Mandrill Postmark
provider := gomail.SMTP // AwsSes Mandrill Postmark
err = mail.SendEmail(email, provider)
if err != nil {
logger.Data(2, logger.ERROR, fmt.Sprintf("error in SendEmail: %s using provider %x", err.Error(), provider))
}

// Congrats!
logger.Data(2, logger.DEBUG, "all emails sent!")
logger.Data(2, logger.DEBUG, "all emails sent via fullExample()")
}
6 changes: 3 additions & 3 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/mrz1836/go-logger"
)

// sendWithSmtp sends an email using the smtp service
func (m *MailService) sendWithSmtp(email *Email) (err error) {
// sendWithSMTP sends an email using the smtp service
func (m *MailService) sendWithSMTP(email *Email) (err error) {

// Create new mail message
mail := mailyak.New(fmt.Sprintf("%s:%d", m.SmtpHost, m.SmtpPort), m.smtpAuth)
mail := mailyak.New(fmt.Sprintf("%s:%d", m.SMTPHost, m.SMTPPort), m.smtpAuth)

// Add the to recipients
mail.To(email.Recipients...)
Expand Down

0 comments on commit 4509cb0

Please sign in to comment.