github.com/jackgris/buffalo@v0.10.3-0.20171206174715-b1dbfd2402de/mail/README.md (about)

     1  # github.com/gobuffalo/buffalo/mail
     2  
     3  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.
     4  
     5  ## Generator
     6  
     7  ```bash
     8  $ buffalo generate mailer welcome_email
     9  ```
    10  
    11  ## Example Usage
    12  
    13  ```go
    14  //actions/mail.go
    15  package x
    16  
    17  import (
    18  	"log"
    19  
    20  	"github.com/gobuffalo/buffalo/render"
    21  	"github.com/gobuffalo/envy"
    22  	"github.com/gobuffalo/packr"
    23  	"github.com/gobuffalo/plush"
    24  	"github.com/gobuffalo/buffalo/mail"
    25  	"github.com/pkg/errors"
    26  	"gitlab.com/wawandco/app/models"
    27  )
    28  
    29  var smtp mail.Sender
    30  var r *render.Engine
    31  
    32  func init() {
    33  
    34  	//Pulling config from the env.
    35  	port := envy.Get("SMTP_PORT", "1025")
    36  	host := envy.Get("SMTP_HOST", "localhost")
    37  	user := envy.Get("SMTP_USER", "")
    38  	password := envy.Get("SMTP_PASSWORD", "")
    39  
    40  	var err error
    41  	smtp, err = mail.NewSMTPSender(host, port, user, password)
    42  
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  
    47  	//The rendering engine, this is usually generated inside actions/render.go in your buffalo app.
    48  	r = render.New(render.Options{
    49  		TemplatesBox:   packr.NewBox("../templates"),
    50  	})
    51  }
    52  
    53  //SendContactMessage Sends contact message to contact@myapp.com
    54  func SendContactMessage(c *models.Contact) error {
    55  
    56  	//Creates a new message
    57  	m := mail.NewMessage()
    58  	m.From = "sender@myapp.com"
    59  	m.Subject = "New Contact"
    60  	m.To = []string{"contact@myapp.com"}
    61  
    62  	// Data that will be used inside the templates when rendering.
    63  	data := map[string]interface{}{
    64  		"contact": c,
    65  	}
    66  
    67  	// You can add multiple bodies to the message you're creating to have content-types alternatives.
    68  	err := m.AddBodies(data, r.HTML("mail/contact.html"), r.Plain("mail/contact.txt"))
    69  
    70  	if err != nil {
    71  		return errors.WithStack(err)
    72  	}
    73  
    74  	err = smtp.Send(m)
    75  	if err != nil {
    76  		return errors.WithStack(err)
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  ```
    83  
    84  This `SendContactMessage` could be called by one of your actions, p.e. the action that handles your contact form submission.
    85  
    86  ```go
    87  //actions/contact.go
    88  ...
    89  
    90  func ContactFormHandler(c buffalo.Context) error {
    91      contact := &models.Contact{}
    92      c.Bind(contact)
    93  
    94      //Calling to send the message
    95      SendContactMessage(contact)
    96      return c.Redirect(302, "contact/thanks")
    97  }
    98  ...
    99  ```
   100  
   101  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)
   102  
   103  ```go
   104  ...
   105  var smtp mail.Sender
   106  
   107  func init() {
   108      port := envy.Get("SMTP_PORT", "465")
   109      // or 587 with TLS
   110  
   111  	host := envy.Get("SMTP_HOST", "smtp.gmail.com")
   112  	user := envy.Get("SMTP_USER", "your@email.com")
   113  	password := envy.Get("SMTP_PASSWORD", "yourp4ssw0rd")
   114  
   115  	var err error
   116  	sender, err := mail.NewSMTPSender(host, port, user, password)
   117  	sender.Dialer.SSL = true
   118  
   119      //or if TLS
   120      sender.Dialer.TLSConfig = &tls.Config{...}
   121  
   122      smtp = sender
   123  }
   124  ...
   125  ```