Skip to Content

gofasta g email-template

Generates an HTML email template file for sending transactional emails such as welcome messages, password resets, order confirmations, and notifications. Templates use Go’s html/template package with a base layout for dynamic content rendering.

Aliases: email

Usage

gofasta g email-template <Name>

This command has no flags.

Examples

Generate a welcome email template:

gofasta g email-template Welcome

Generate a password reset template:

gofasta g email-template PasswordReset

Generate an order confirmation template:

gofasta g email OrderConfirmation

What It Generates

Running gofasta g email-template Welcome creates one file:

templates/emails/welcome.html

Generated Code

<!-- templates/emails/welcome.html --> {{"{{"}}define "subject"{{"}}"}}Welcome to {{"{{"}} .AppName {{"}}"}}!{{"{{"}}end{{"}}"}} {{"{{"}}define "content"{{"}}"}} <table role="presentation" width="100%" cellpadding="0" cellspacing="0"> <tr> <td style="padding: 24px 0;"> <h1 style="font-size: 24px; font-weight: 600; color: #1a1a1a; margin: 0 0 16px;"> Welcome, {{"{{"}} .Name {{"}}"}}! </h1> <p style="font-size: 16px; color: #4a4a4a; line-height: 1.6; margin: 0 0 16px;"> Thank you for joining {{"{{"}} .AppName {{"}}"}}. We are excited to have you on board. </p> <p style="font-size: 16px; color: #4a4a4a; line-height: 1.6; margin: 0 0 24px;"> To get started, click the button below to verify your email address: </p> <table role="presentation" cellpadding="0" cellspacing="0"> <tr> <td style="border-radius: 6px; background-color: #00ADD8;"> <a href="{{"{{"}} .VerificationURL {{"}}"}}" style="display: inline-block; padding: 12px 24px; font-size: 16px; color: #ffffff; text-decoration: none; font-weight: 500;"> Verify Email </a> </td> </tr> </table> </td> </tr> </table> {{"{{"}}end{{"}}"}}

The template uses Go’s html/template with a base layout. The subject block defines the email subject line, and the content block defines the email body that is rendered within the base layout.

Template Data

Each template receives a data struct when rendered. Define the struct in your service or job:

type WelcomeEmailData struct { AppName string Name string Email string VerificationURL string }

Sending Emails

Use the email service to render and send templates:

func (s *UserServiceImpl) SendWelcomeEmail(user *models.User, verificationToken string) error { data := WelcomeEmailData{ AppName: "My App", Name: user.Name, Email: user.Email, VerificationURL: fmt.Sprintf("https://myapp.com/verify?token=%s", verificationToken), } return s.emailService.Send(EmailMessage{ To: user.Email, Template: "welcome", Data: data, }) }

Email Configuration

SMTP settings are configured in config/config.yaml:

email: smtp_host: smtp.example.com smtp_port: 587 smtp_user: noreply@example.com smtp_password: your-password from_name: My App from_address: noreply@example.com

Best Practices

  • Use inline styles for email compatibility across email clients
  • Use table-based layouts for reliable rendering
  • Always include a plain-text fallback for accessibility
  • Test with multiple email clients (Gmail, Outlook, Apple Mail)
  • Keep templates under 100KB for deliverability
  • gofasta g job — generate a background job to send emails asynchronously
  • gofasta g task — generate an async task for sending emails
  • gofasta new — create a project with email support configured
Last updated on