Keplers Mail Service

PHP

PHP integration examples for Keplers Mail Service

Send emails using PHP with Keplers Mail Service REST API.

Installation

# No additional dependencies required for basic usage

Basic Setup

# .env
KEPLERS_API_KEY=kms_your_api_key.live_...

REST API Integration

Basic Client

<?php
class KeplersMailClient {
    private $apiKey;
    private $baseUrl;
    
    public function __construct($apiKey, $baseUrl = 'https://api.keplars.com') {
        $this->apiKey = $apiKey;
        $this->baseUrl = rtrim($baseUrl, '/');
    }
    
    public function sendEmail($emailData) {
        return $this->makeRequest('/api/v1/send-email/queue', $emailData);
    }
    
    public function sendInstantEmail($emailData) {
        return $this->makeRequest('/api/v1/send-email/instant', $emailData);
    }
    
    private function makeRequest($endpoint, $data) {
        $url = $this->baseUrl . $endpoint;
        $jsonData = json_encode($data);
        
        $headers = [
            'Authorization: Bearer ' . $this->apiKey,
            'Content-Type: application/json',
            'Content-Length: ' . strlen($jsonData)
        ];
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);
        
        if ($error) {
            throw new Exception("cURL Error: " . $error);
        }
        
        if ($httpCode !== 200) {
            throw new Exception("HTTP Error {$httpCode}: " . $response);
        }
        
        $decoded = json_decode($response, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception("JSON decode error: " . json_last_error_msg());
        }
        
        return $decoded;
    }
}
?>

Usage Examples

<?php
$apiKey = $_ENV['KEPLERS_API_KEY'] ?? getenv('KEPLERS_API_KEY');
$client = new KeplersMailClient($apiKey);

function sendWelcomeEmail($client, $userEmail, $userName) {
    try {
        $emailData = [
            'to' => [$userEmail],
            'subject' => "Welcome {$userName}!",
            'body' => "<h1>Welcome {$userName}!</h1><p>Thank you for joining our platform.</p>",
            'is_html' => true
        ];
        
        $result = $client->sendEmail($emailData);
        echo "Email sent: " . json_encode($result) . "\n";
        return $result;
    } catch (Exception $e) {
        echo "Failed to send email: " . $e->getMessage() . "\n";
        throw $e;
    }
}

function sendVerificationEmail($client, $userEmail, $code) {
    try {
        $emailData = [
            'to' => [$userEmail],
            'subject' => 'Verify Your Email',
            'body' => "Your verification code: {$code}",
            'is_html' => false
        ];
        
        $result = $client->sendInstantEmail($emailData);
        echo "Verification email sent: " . json_encode($result) . "\n";
        return $result;
    } catch (Exception $e) {
        echo "Failed to send verification email: " . $e->getMessage() . "\n";
        throw $e;
    }
}

sendWelcomeEmail($client, '[email protected]', 'John');
sendVerificationEmail($client, '[email protected]', '123456');
?>

Laravel Integration

<?php
// app/Services/EmailService.php
namespace App\Services;

use Exception;

class EmailService {
    private $client;
    
    public function __construct() {
        $apiKey = config('services.keplers.api_key');
        $this->client = new \KeplersMailClient($apiKey);
    }
    
    public function sendWelcomeEmail($email, $name) {
        $emailData = [
            'to' => [$email],
            'subject' => "Welcome {$name}!",
            'body' => "<h1>Welcome {$name}!</h1>",
            'is_html' => true
        ];
        
        return $this->client->sendEmail($emailData);
    }
}

// app/Http/Controllers/AuthController.php
namespace App\Http\Controllers;

use App\Services\EmailService;
use Illuminate\Http\Request;

class AuthController extends Controller {
    private $emailService;
    
    public function __construct(EmailService $emailService) {
        $this->emailService = $emailService;
    }
    
    public function register(Request $request) {
        try {
            $this->emailService->sendWelcomeEmail($request->email, $request->name);
            return response()->json(['success' => true]);
        } catch (Exception $e) {
            return response()->json(['error' => 'Failed to send email'], 500);
        }
    }
}
?>

SMTP Integration

SMTP Client

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

class KeplersEmailSMTPClient {
    private $mailer;
    
    public function __construct($smtpConfig) {
        $this->mailer = new PHPMailer(true);
        
        $this->mailer->isSMTP();
        $this->mailer->Host = 'smtp.keplars.com';
        $this->mailer->SMTPAuth = true;
        $this->mailer->Username = $smtpConfig['username'];
        $this->mailer->Password = $smtpConfig['password'];
        $this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $this->mailer->Port = 587;
    }
    
    public function sendEmail($emailData) {
        try {
            $this->mailer->clearAddresses();
            $this->mailer->addAddress($emailData['to'][0]);
            $this->mailer->setFrom('[email protected]', 'Keplers Mail Service');
            
            $this->mailer->isHTML($emailData['is_html']);
            $this->mailer->Subject = $emailData['subject'];
            $this->mailer->Body = $emailData['body'];
            
            $this->mailer->send();
            
            return [
                'success' => true,
                'data' => ['status' => 'sent', 'method' => 'smtp']
            ];
        } catch (Exception $e) {
            throw new Exception("SMTP send failed: " . $e->getMessage());
        }
    }
}
?>

SMTP Installation

composer require phpmailer/phpmailer

SMTP Usage

<?php
require 'vendor/autoload.php';

$smtpClient = new KeplersEmailSMTPClient([
    'username' => $_ENV['KEPLERS_SMTP_USERNAME'],
    'password' => $_ENV['KEPLERS_SMTP_PASSWORD']
]);

function sendEmailViaSMTP($smtpClient, $userEmail, $subject, $body) {
    try {
        $emailData = [
            'to' => [$userEmail],
            'subject' => $subject,
            'body' => $body,
            'is_html' => true
        ];
        
        $result = $smtpClient->sendEmail($emailData);
        echo "SMTP email sent: " . json_encode($result) . "\n";
        return $result;
    } catch (Exception $e) {
        echo "SMTP send failed: " . $e->getMessage() . "\n";
        throw $e;
    }
}

sendEmailViaSMTP($smtpClient, '[email protected]', 'Test Email', '<p>Hello World!</p>');
?>

Choose REST API for advanced features or SMTP for traditional email client integration.

On this page