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 sending, verify your domain and create an SMTP credential. Use a sandbox credential for these examples so no email is delivered. Keep the username and password in your server’s secret store. Before you begin, ensure you have:System Requirements
System Requirements
- Go installed on your system (Download Go)
- Basic knowledge of Go programming
- Go modules enabled (Go 1.11+)
Connection Settings
All message data is stored in the EU. See SMTP limits.
Using net/smtp (Standard Library)
Go’s standard library provides basic SMTP functionality through thenet/smtp package. It’s lightweight but requires manual message construction.
Using gomail.v2 (Recommended)
Thegomail.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
Error Handling
Error Handling
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
Connection Management
Connection Management
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
Security
Security
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
Performance
Performance
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
Related Guides
- Before sending: verify your domain and create an SMTP credential.
- Connection and message rules: SMTP hosts, ports and limits, special headers, sandbox mode, retention and plan features.
- Other ways to send: REST API, AhaSend CLI, Node.js SDK and Go SDK.
- Delivery events: webhook setup and local webhook testing.

