Send emails through AhaSend’s SMTP servers using PHP with popular libraries like PHPMailer and Symfony Mailer. This guide covers everything from basic setup to advanced features like attachments and HTML content.
Avoid Native mail() Function: PHP’s built-in mail() function is limited - it only works with localhost SMTP servers, doesn’t support authentication or attachments, and makes HTML emails difficult to implement. Use the recommended libraries below instead.

Prerequisites

Before you begin, ensure you have:
  • PHP installed on your system (Download PHP)
  • Composer for package management (Install Composer)
  • Basic knowledge of PHP and command line usage
  • 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

PHPMailer Integration

PHPMailer is the most popular and feature-rich PHP email library, supporting HTML messages, attachments, and advanced SMTP features.

Installation

Install PHPMailer using Composer:
composer require phpmailer/phpmailer

Basic Email Example

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->isSMTP();
    $mail->Host       = 'send.ahasend.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'YOUR_SMTP_USERNAME';
    $mail->Password   = 'YOUR_SMTP_PASSWORD';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Recipients
    $mail->setFrom('[email protected]', 'Your App Name');
    $mail->addAddress('[email protected]', 'John Doe');

    // Content
    $mail->isHTML(true);
    $mail->Subject = 'Welcome to our platform!';
    $mail->Body    = '<h1>Welcome!</h1><p>Thanks for signing up.</p>';
    $mail->AltBody = 'Welcome! Thanks for signing up.';

    $mail->send();
    echo 'Email sent successfully!';
} catch (Exception $e) {
    echo "Failed to send email: {$mail->ErrorInfo}";
}
?>

PHPMailer with Special Headers

Add AhaSend’s special headers for tracking, sandbox mode, and more:
PHPMailer with Headers
<?php
$mail = new PHPMailer(true);

try {
    // SMTP configuration (same as above)
    $mail->isSMTP();
    $mail->Host = 'send.ahasend.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'YOUR_SMTP_USERNAME';
    $mail->Password = 'YOUR_SMTP_PASSWORD';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    // Recipients and content
    $mail->setFrom('[email protected]', 'Your App Name');
    $mail->addAddress('[email protected]', 'John Doe');

    // Add special headers
    $mail->addCustomHeader('ahasend-track-opens', 'true');
    $mail->addCustomHeader('ahasend-track-clicks', 'true');
    $mail->addCustomHeader('ahasend-tags', 'welcome,onboarding,php');

    // For sandbox testing:
    // $mail->addCustomHeader('AhaSend-Sandbox', 'true');

    $mail->isHTML(true);
    $mail->Subject = 'Welcome to our platform!';
    $mail->Body = '<h1>Welcome!</h1><p>Click <a href="https://yourdomain.com">here to get started</a>.</p>';

    $mail->send();
    echo 'Email sent with tracking enabled!';
} catch (Exception $e) {
    echo "Error: {$mail->ErrorInfo}";
}
?>

Symfony Mailer Integration

Symfony Mailer is a modern, powerful email library that’s part of the Symfony ecosystem and used by Laravel.

Installation

Install Symfony Mailer using Composer:
composer require symfony/mailer

Basic Examples

<?php
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;

require 'vendor/autoload.php';

// Create transport using DSN
$dsn = 'smtp://YOUR_SMTP_USERNAME:[email protected]:587';
$transport = Transport::fromDsn($dsn);
$mailer = new Mailer($transport);

// Create email
$email = (new Email())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Welcome to our platform!')
    ->text('Welcome! Thanks for signing up.')
    ->html('<h1>Welcome!</h1><p>Thanks for signing up.</p>');

try {
    $mailer->send($email);
    echo 'Email sent successfully!';
} catch (Exception $e) {
    echo 'Failed to send email: ' . $e->getMessage();
}
?>

Laravel Configuration

Laravel uses Symfony Mailer internally. Configure AhaSend SMTP by updating your .env file:

Environment Configuration

Laravel .env Configuration
MAIL_MAILER=smtp
MAIL_HOST=send.ahasend.com
MAIL_PORT=587
MAIL_USERNAME=YOUR_SMTP_USERNAME
MAIL_PASSWORD=YOUR_SMTP_PASSWORD
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="Your App Name"

Sending Emails in Laravel

<?php
use Illuminate\Support\Facades\Mail;
use Illuminate\Mail\Message;

// Send a simple email
Mail::raw('Welcome! Thanks for signing up.', function (Message $message) {
    $message->to('[email protected]', 'John Doe')
            ->subject('Welcome to our platform!');
});

echo 'Email sent successfully!';
?>

Best Practices

Always use try-catch blocks:
  • Wrap email sending in try-catch statements
  • Log errors for debugging
  • Provide user-friendly error messages
  • Consider retry mechanisms for temporary failures
Protect your credentials:
  • Store SMTP credentials in environment variables
  • Never commit credentials to version control
  • Use different credentials for different environments
  • Regularly rotate SMTP passwords
Optimize for better performance:
  • Use queue systems for bulk emails (Laravel Queues, etc.)
  • Reuse SMTP connections when sending multiple emails
  • Consider async sending for non-critical emails
  • Monitor sending rates to avoid limits

Testing with Sandbox Mode

Use sandbox mode to test your PHP email integration safely:
Sandbox Testing Examples
<?php
// PHPMailer sandbox
$mail->addCustomHeader('AhaSend-Sandbox', 'true');
$mail->addCustomHeader('AhaSend-Sandbox-Result', 'deliver'); // or 'bounce', 'defer', etc.

// Symfony Mailer sandbox
$email->getHeaders()
    ->addTextHeader('AhaSend-Sandbox', 'true')
    ->addTextHeader('AhaSend-Sandbox-Result', 'deliver');

// Laravel sandbox
$message->getHeaders()
    ->addTextHeader('AhaSend-Sandbox', 'true')
    ->addTextHeader('AhaSend-Sandbox-Result', 'deliver');
?>
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Troubleshooting

Common authentication issues:
  • Verify SMTP username and password are correct
  • Ensure you’re using SMTP credentials, not your dashboard login
  • Check that credentials haven’t expired
  • Confirm domain is verified in AhaSend
Network and connection issues:
  • Try different ports: 587 (recommended), 25, or 2525
  • Ensure STARTTLS is enabled, not SSL
  • Check firewall settings aren’t blocking SMTP
  • Verify server allows outbound SMTP connections
Laravel configuration problems:
  • Clear config cache: php artisan config:clear
  • Verify .env file syntax (no spaces around equals signs)
  • Check mail configuration: php artisan config:show mail
  • Test with php artisan tinker for debugging

Resources

Pro Tip: Start with sandbox mode when developing, use environment variables for credentials, and always provide both HTML and plain text versions of your emails for better compatibility.