Sending Emails with SMTP in Go
Send emails through AhaSend’s SMTP servers using Go with the standard librarynet/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:System Requirements
System Requirements
- Go installed on your system (Download Go)
- Basic knowledge of Go programming
- Go modules enabled (Go 1.11+)
AhaSend Setup
AhaSend Setup
- 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:
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
send.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
US Server
Host:
Ports: 587 (recommended), 25, 2525
Security: STARTTLS
Authentication: Required
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 thenet/smtp
package. It’s lightweight but requires manual message construction.
Using gomail.v2 (Recommended)
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.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
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