send email with php

How to Send Email with PHP in 5 Minutes

Last Updated on August 2, 2026 by ian

Sending transactional email from PHP — user registrations, password resets, order confirmations — is straightforward once you have a reliable sending infrastructure. This guide shows you how to send email using the ConvertNow API with PHP’s built-in cURL, and with PHPMailer for SMTP.

What you need

• PHP 7.4 or above

• cURL extension enabled (enabled by default on most hosting)

• A free ConvertNow account — convertnow.co

Step 1: Get your API key

Sign up at convertnow.co, go to Email API, click + New Key, and store the key in an environment variable or your config:

// Never hardcode your API key in source code
// Use environment variables or a config file outside your web root
$apiKey = getenv('CONVERTNOW_API_KEY');
// Or from a .env file using vlucas/phpdotenv:
// $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
// $dotenv->load();
// $apiKey = $_ENV['CONVERTNOW_API_KEY'];

Step 2: Send your first email with cURL

<?php

function sendEmail(array $payload): array {
    $apiKey = getenv('CONVERTNOW_API_KEY');

    $ch = curl_init('https://api.convertnow.co/v1/email/send');
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode($payload),
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $data = json_decode($response, true);

    if ($httpCode !== 200) {
        throw new RuntimeException('Email send failed: ' . ($data['error'] ?? 'Unknown error'));
    }

    return $data;
}

// Send a basic email
$result = sendEmail([
    'to'      => '[email protected]',
    'from'    => '[email protected]',
    'subject' => 'Hello from PHP',
    'html'    => '<h1>It works!</h1><p>Your first email from PHP.</p>',
    'text'    => 'It works! Your first email from PHP.',
]);

echo $result['status']; // sent

Step 3: Common email patterns

Welcome email on user registration

// In your registration handler:
sendEmail([
    'to'      => $userEmail,
    'subject' => 'Welcome to MyApp!',
    'html'    => sprintf(
        '<h1>Hi %s!</h1><p>Your account is ready. <a href="%s">Log in here</a>.</p>',
        htmlspecialchars($userName),
        $loginUrl
    ),
    'tags'    => ['welcome', 'onboarding'],
]);

Password reset

sendEmail([
    'to'      => $userEmail,
    'subject' => 'Reset your password',
    'html'    => sprintf(
        '<p>Click <a href="%s">here</a> to reset your password. Link expires in 1 hour.</p>',
        $resetUrl
    ),
    'tags'    => ['password-reset'],
]);

Send with CC, BCC, and attachment

$pdf = base64_encode(file_get_contents('/path/to/invoice.pdf'));

sendEmail([
    'to'      => '[email protected]',
    'cc'      => '[email protected]',
    'bcc'     => ['[email protected]'],
    'subject' => 'Your invoice #INV-001',
    'html'    => '<p>Please find your invoice attached.</p>',
    'attachments' => [[
        'filename'     => 'invoice.pdf',
        'content'      => $pdf,
        'content_type' => 'application/pdf',
    ]],
]);

PHPMailer: use ConvertNow as SMTP relay

If you prefer SMTP or are using PHPMailer in an existing application:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host       = 'out.convertnow.co';
    $mail->SMTPAuth   = true;
    $mail->Username   = '[email protected]';
    $mail->Password   = getenv('CONVERTNOW_API_KEY');
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMIME; // implicit SSL
    $mail->Port       = 465;

    $mail->setFrom('[email protected]', 'My App');
    $mail->addAddress('[email protected]');
    $mail->Subject = 'Hello via PHPMailer';
    $mail->isHTML(true);
    $mail->Body    = '<h1>It works!</h1>';
    $mail->AltBody = 'It works!';

    $mail->send();
    echo 'Email sent';
} catch (Exception $e) {
    echo 'Error: ' . $mail->ErrorInfo;
}

Laravel: configure ConvertNow as your mail driver

In Laravel, add ConvertNow to your config/mail.php mailers array and update your .env:

# .env
MAIL_MAILER=smtp
MAIL_HOST=out.convertnow.co
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_PASSWORD=cn_live_YOUR_KEY
[email protected]
MAIL_FROM_NAME="My App"

Then use Laravel’s Mail facade or Mailable classes as normal:

use Illuminate\Support\Facades\Mail;

Mail::to($user->email)->send(new WelcomeMail($user));
// Or using raw to() syntax:
Mail::raw('Your account is ready.', function ($message) use ($user) {
    $message->to($user->email)->subject('Welcome!');
});

Why not use PHP’s built-in mail() function?

PHP’s built-in mail() sends directly from your server. Without proper SPF, DKIM, and DMARC configuration, these emails almost always land in spam. ConvertNow’s dedicated infrastructure handles all authentication automatically — you get inbox placement without the ops overhead.

Start sending for free — convertnow.co — no credit card required