This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #762 from gobuffalo/mail-merge
Proposal: move github.com/gobuffalo/x/mail into Buffalo "Core" fixes #752
- Loading branch information
Showing
12 changed files
with
539 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[{ | ||
"path": "mailers/mailers.go", | ||
"contains": [ | ||
"github.com/gobuffalo/buffalo/mail", | ||
"smtp, err = mail.NewSMTPSender(host, port, user, password)" | ||
], | ||
"!contains": [ | ||
"github.com/gobuffalo/x/mail" | ||
] | ||
}, | ||
{ | ||
"path": "templates/mail/layout.html", | ||
"contains": [ | ||
"<h1>templates/mailers/layout.html</h1>" | ||
] | ||
}, | ||
{ | ||
"path": "mailers/welcome_email.go", | ||
"contains": [ | ||
"err := m.AddBody(r.HTML(\"welcome_email.html\"), render.Data{})" | ||
] | ||
}, | ||
{ | ||
"path": "templates/mail/welcome_email.html", | ||
"contains": [ | ||
"<h2>Welcome Email</h2>", | ||
"<h3>../templates/mail/welcome_email.html</h3>" | ||
] | ||
} | ||
] |
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,31 @@ | ||
package generate | ||
|
||
import ( | ||
"github.com/gobuffalo/buffalo/generators/mail" | ||
"github.com/gobuffalo/buffalo/meta" | ||
"github.com/gobuffalo/makr" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var mailer = mail.Generator{} | ||
|
||
// MailCmd for generating mailers | ||
var MailCmd = &cobra.Command{ | ||
Use: "mailer", | ||
Short: "Generates a new mailer for Buffalo", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return errors.New("you must supply a name for your mailer") | ||
} | ||
mailer.App = meta.New(".") | ||
mailer.Name = meta.Name(args[0]) | ||
data := makr.Data{} | ||
return mailer.Run(".", data) | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
MailCmd.Flags().BoolVar(&mailer.SkipInit, "skip-init", false, "skip initializing mailers/") | ||
} |
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,36 @@ | ||
package mailers | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gobuffalo/envy" | ||
"github.com/gobuffalo/packr" | ||
"github.com/gobuffalo/buffalo/mail" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var smtp mail.Sender | ||
var r *render.Engine | ||
|
||
func init() { | ||
|
||
// Pulling config from the env. | ||
port := envy.Get("SMTP_PORT", "1025") | ||
host := envy.Get("SMTP_HOST", "localhost") | ||
user := envy.Get("SMTP_USER", "") | ||
password := envy.Get("SMTP_PASSWORD", "") | ||
|
||
var err error | ||
smtp, err = mail.NewSMTPSender(host, port, user, password) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
r = render.New(render.Options{ | ||
HTMLLayout: "layout.html", | ||
TemplatesBox: packr.NewBox("../templates/mail"), | ||
Helpers: render.Helpers{}, | ||
}) | ||
} |
3 changes: 3 additions & 0 deletions
3
generators/mail/init/templates/templates/mail/layout.html.tmpl
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,3 @@ | ||
<h1>templates/mailers/layout.html</h1> | ||
|
||
<%= yield %> |
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,84 @@ | ||
package mail | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gobuffalo/buffalo/generators" | ||
"github.com/gobuffalo/buffalo/meta" | ||
"github.com/gobuffalo/makr" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Generator for creating new mailers | ||
type Generator struct { | ||
App meta.App `json:"app"` | ||
Name meta.Name `json:"name"` | ||
SkipInit bool `json:"skip_init"` | ||
} | ||
|
||
// Run the new mailer generator. It will init the mailers directory | ||
// if it doesn't already exist | ||
func (d Generator) Run(root string, data makr.Data) error { | ||
g := makr.New() | ||
defer g.Fmt(root) | ||
data["opts"] = d | ||
|
||
if err := d.initGenerator(data); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
fn := d.Name.File() | ||
g.Add(makr.NewFile(filepath.Join("mailers", fn+".go"), mailerTmpl)) | ||
g.Add(makr.NewFile(filepath.Join("templates", "mail", fn+".html"), mailTmpl)) | ||
return g.Run(root, data) | ||
} | ||
|
||
func (d Generator) initGenerator(data makr.Data) error { | ||
files, err := generators.Find(filepath.Join(generators.TemplatesPath, "mail", "init")) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
g := makr.New() | ||
for _, f := range files { | ||
g.Add(makr.NewFile(f.WritePath, f.Body)) | ||
} | ||
|
||
g.Should = func(data makr.Data) bool { | ||
if d.SkipInit { | ||
return false | ||
} | ||
if _, err := os.Stat(filepath.Join("mailers", "mailers.go")); err == nil { | ||
return false | ||
} | ||
return true | ||
} | ||
return g.Run(".", data) | ||
} | ||
|
||
const mailerTmpl = `package mailers | ||
import ( | ||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gobuffalo/buffalo/mail" | ||
"github.com/pkg/errors" | ||
) | ||
func Send{{.opts.Name.Model}}() error { | ||
m := mail.NewMessage() | ||
// fill in with your stuff: | ||
m.Subject = "{{.opts.Name.Title}}" | ||
m.From = "" | ||
m.To = []string{} | ||
err := m.AddBody(r.HTML("{{.opts.Name.File}}.html"), render.Data{}) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
return smtp.Send(m) | ||
} | ||
` | ||
|
||
const mailTmpl = `<h2>{{.opts.Name.Title}}</h2> | ||
<h3>../templates/mail/{{.opts.Name.File}}.html</h3>` |
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,125 @@ | ||
# github.com/gobuffalo/buffalo/mail | ||
|
||
This package is intended to allow easy Email sending with Buffalo, it allows you to define your custom `mail.Sender` for the provider you would like to use. | ||
|
||
## Generator | ||
|
||
```bash | ||
$ buffalo generate mailer welcome_email | ||
``` | ||
|
||
## Example Usage | ||
|
||
```go | ||
//actions/mail.go | ||
package x | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/gobuffalo/buffalo/render" | ||
"github.com/gobuffalo/envy" | ||
"github.com/gobuffalo/packr" | ||
"github.com/gobuffalo/plush" | ||
"github.com/gobuffalo/buffalo/mail" | ||
"github.com/pkg/errors" | ||
"gitlab.com/wawandco/app/models" | ||
) | ||
|
||
var smtp mail.Sender | ||
var r *render.Engine | ||
|
||
func init() { | ||
|
||
//Pulling config from the env. | ||
port := envy.Get("SMTP_PORT", "1025") | ||
host := envy.Get("SMTP_HOST", "localhost") | ||
user := envy.Get("SMTP_USER", "") | ||
password := envy.Get("SMTP_PASSWORD", "") | ||
|
||
var err error | ||
smtp, err = mail.NewSMTPSender(host, port, user, password) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
//The rendering engine, this is usually generated inside actions/render.go in your buffalo app. | ||
r = render.New(render.Options{ | ||
TemplatesBox: packr.NewBox("../templates"), | ||
}) | ||
} | ||
|
||
//SendContactMessage Sends contact message to contact@myapp.com | ||
func SendContactMessage(c *models.Contact) error { | ||
|
||
//Creates a new message | ||
m := mail.NewMessage() | ||
m.From = "sender@myapp.com" | ||
m.Subject = "New Contact" | ||
m.To = []string{"contact@myapp.com"} | ||
|
||
// Data that will be used inside the templates when rendering. | ||
data := map[string]interface{}{ | ||
"contact": c, | ||
} | ||
|
||
// You can add multiple bodies to the message you're creating to have content-types alternatives. | ||
err := m.AddBodies(data, r.HTML("mail/contact.html"), r.Plain("mail/contact.txt")) | ||
|
||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
err = smtp.Send(m) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
``` | ||
|
||
This `SendContactMessage` could be called by one of your actions, p.e. the action that handles your contact form submission. | ||
|
||
```go | ||
//actions/contact.go | ||
... | ||
|
||
func ContactFormHandler(c buffalo.Context) error { | ||
contact := &models.Contact{} | ||
c.Bind(contact) | ||
|
||
//Calling to send the message | ||
SendContactMessage(contact) | ||
return c.Redirect(302, "contact/thanks") | ||
} | ||
... | ||
``` | ||
|
||
If you're using Gmail or need to configure your SMTP connection you can use the Dialer property on the SMTPSender, p.e: (for Gmail) | ||
|
||
```go | ||
... | ||
var smtp mail.Sender | ||
|
||
func init() { | ||
port := envy.Get("SMTP_PORT", "465") | ||
// or 587 with TLS | ||
|
||
host := envy.Get("SMTP_HOST", "smtp.gmail.com") | ||
user := envy.Get("SMTP_USER", "your@email.com") | ||
password := envy.Get("SMTP_PASSWORD", "yourp4ssw0rd") | ||
|
||
var err error | ||
sender, err := mail.NewSMTPSender(host, port, user, password) | ||
sender.Dialer.SSL = true | ||
|
||
//or if TLS | ||
sender.Dialer.TLSConfig = &tls.Config{...} | ||
|
||
smtp = sender | ||
} | ||
... | ||
``` |
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,6 @@ | ||
package mail | ||
|
||
// Sender interface for any upcomming mailers. | ||
type Sender interface { | ||
Send(Message) error | ||
} |
Oops, something went wrong.