Mail

Gozen provides an easy way to configure your mail settings. The first thing you should do is go into your .env file and paste in the settings for you mail provider.



An example

First add these lines to your routes.go file

r.Get("/mail", mail.MailView)
r.Post("/mail", mail.SendMail)

Now create your controller


mail.go

package mail

import (
	"gozen/system/mail"
	"gozen/system/templates"
	"gozen/system/validation"
	"net/http"
)

func MailView(w http.ResponseWriter, r *http.Request) {
	data := templates.TemplateData{}
	templates.Render(w, r, "mail", data)
}

func SendMail(w http.ResponseWriter, r *http.Request) {

	v := &validation.Validator{}

	v.Rules("email", r.FormValue("email"), "required|email")
		


	email := r.FormValue("email")

	postData := templates.PostData(w, r)

	if v.HasErrors() {
		templates.Errors(w, r, v, postData, "mail")
		return
	}

	//else success
	recipientEmail := email
	templatePath := "mail/email_template.html"

	result := mail.New().
		SetRecipient(recipientEmail).
		SetTemplatePath(templatePath).
		LoadTemplate().
		BuildMessage().
		Send()

	w.Write(result)
}

Add this to your views directory


mail.html

{{ template "_header.html" }}
<div>
   {{ if .Data.FlashData }}
   <div>
      <div class="text-danger">Error</div>
      <div class="text-danger">
         {{ .Data.FlashData }}
      </div>
   </div>
   {{end}}

   <h4>Mail</h4>
   <div>
      Test your emails work, if you have issues please check your email
      settings for your provider in the .env file
   </div>
   <div>
      <form action="/mail" method="POST">
         {{.Csrf}}
         <label for="email">Email</label>
         <div>Email to test</div>
         <input 
            class="" 
            type="text"
            name="email" 
            placeholder="A valid email address" />

         <div class="text-danger">
            {{ .Data.PostDataErrors.email }}
         </div>               
         <button>
            Submit
         </button>
      </form>
   </div>
</div>
{{ template "_footer.html" }}

Important

Before you can successfully test mail, you must provide the correct details in the .env file