Skip to content

Latest commit

 

History

History
125 lines (93 loc) · 3.11 KB

mail.md

File metadata and controls

125 lines (93 loc) · 3.11 KB

Mail

[[toc]]

Introduction

Goravel can use facades.Mail() to easily send mail locally.

Configuration

Before sending an email, you need to configure the config/mail.go configuration file.

Send Mail

import "github.com/goravel/framework/mail"

err := facades.Mail().To([]string{"[email protected]"}).
  Cc([]string{"[email protected]"}).
  Bcc([]string{"[email protected]"}).
  Attach([]string{"file.png"}).
  Content(mail.Html("<h1>Hello Goravel</h1>")).
  Subject("Subject").
  Send()

Send Mail By Queue

import "github.com/goravel/framework/mail"

err := facades.Mail().To([]string{"[email protected]"}).
  Cc([]string{"[email protected]"}).
  Bcc([]string{"[email protected]"}).
  Attach([]string{"file.png"}).
  Content(mail.Html("<h1>Hello Goravel</h1>")).
  Subject("Subject").
  Queue()

You can also customize the queue:

import "github.com/goravel/framework/mail"

err := facades.Mail().To([]string{"[email protected]"}).
  Cc([]string{"[email protected]"}).
  Bcc([]string{"[email protected]"}).
  Attach([]string{"file.png"}).
  Content(mail.Html("<h1>Hello Goravel</h1>")).
  Subject("Subject").
  Queue(mail.Queue().Connection("high").Queue("mail"))

Setting Sender

Framework uses MAIL_FROM_ ADDRESS and MAIL_FROM_ NAME in the config/mail.go configuration file as global senders. You can also customize the sender, but you need to note that the mail address needs to be consistent with the configured STMP:

import "github.com/goravel/framework/mail"

err := facades.Mail().To([]string{"[email protected]"}).
  From(mail.Address(testFromAddress, testFromName)).
  Cc([]string{"[email protected]"}).
  Bcc([]string{"[email protected]"}).
  Attach([]string{"file.png"}).
  Content(mail.Html("<h1>Hello Goravel</h1>")).
  Subject("Subject").
  Queue(mail.Queue().Connection("high").Queue("mail"))

Using Mailable

The parameters of the email can be set in a Mailable struct. These structs are stored in the app/mails directory. You can quickly create a Mailable using the make:mail Artisan command:

go run . artisan make:mail OrderShipped

The generated OrderShipped struct is as follows:

import "github.com/goravel/framework/contracts/mail"

type OrderShipped struct {
}

func NewOrderShipped() *OrderShipped {
	return &OrderShipped{}
}

func (m *OrderShipped) Attachments() []string {
	return []string{"../logo.png"}
}

func (m *OrderShipped) Content() *mail.Content {
	return &mail.Content{Html: "<h1>Hello Goravel</h1>"}
}

func (m *OrderShipped) Envelope() *mail.Envelope {
	return &mail.Envelope{
		Bcc:     []string{"[email protected]"},
		Cc:      []string{"[email protected]"},
		From:    mail.From{Address: "[email protected]", Name: "from"},
		Subject: "Goravel",
		To:      []string{"[email protected]"},
	}
}

func (m *OrderShipped) Queue() *mail.Queue {
  return &mail.Queue{
    Connection: "high",
    Queue:      "mail",
  }
}

Then you can use the Mailalbe in the Send and Queue methods:

err := facades.Mail().Send(mails.NewOrderShipped())
err := facades.Mail().Queue(mails.NewOrderShipped())