Skip to main content

Sending Emails with SMTP in Go

Send emails through AhaSend’s SMTP servers using Go with the standard library net/smtp package or the more feature-rich gomail.v2 library. This guide covers everything from basic setup to advanced features like attachments and custom headers.
Go Standard Library vs Third-Party: While Go’s net/smtp package provides essential SMTP functionality, it’s low-level and verbose. For complex emails with attachments or custom headers, consider using gomail.v2 for a more convenient API.

Prerequisites

Before you begin, ensure you have:
  • Go installed on your system (Download Go)
  • Basic knowledge of Go programming
  • Go modules enabled (Go 1.11+)
  • Domain verified in your AhaSend account
  • SMTP credentials created (username and password)
  • Access to your AhaSend dashboard for credential management
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.

Connection Settings

Use these settings for all Go SMTP configurations with AhaSend:

Primary Server

Host: send.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

US Server

Host: send-us.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required

Using net/smtp (Standard Library)

Go’s standard library provides basic SMTP functionality through the net/smtp package. It’s lightweight but requires manual message construction.
Use gomail.v2 for production applications as it provides better error handling, attachment support, and cleaner API. Start with sandbox mode during development and use environment variables for credentials.
The gomail.v2 package provides a more convenient and feature-rich API for sending emails with attachments, custom headers, and better message construction.

Installation

Install gomail.v2 using Go modules:

Advanced Examples

Testing with Sandbox Mode

Use sandbox mode to test your Go email integration safely:
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Best Practices

Robust error handling:
  • Always check for errors when sending emails
  • Implement retry logic for temporary failures
  • Log errors with sufficient detail for debugging
  • Use exponential backoff for retries
Efficient SMTP connections:
  • Reuse connections for bulk sending
  • Close connections properly with defer statements
  • Handle connection timeouts gracefully
  • Consider connection pooling for high-volume applications
Secure credential handling:
  • Store credentials in environment variables
  • Use Go’s os.Getenv() to read credentials
  • Never hardcode credentials in source code
  • Rotate credentials regularly
Optimize for performance:
  • Use goroutines for concurrent email sending
  • Implement rate limiting to avoid throttling
  • Add delays between bulk emails
  • Monitor memory usage for large batches

Resources

Go net/smtp Documentation

Official Go standard library SMTP documentation

gomail.v2 Documentation

Complete gomail.v2 package documentation

Go Documentation

Official Go programming language documentation

AhaSend Support

Get help from our engineering team