PHP code example of websitesql / mailer

1. Go to this page and download the library: Download websitesql/mailer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

websitesql / mailer example snippets


$mailConfig = [
    // Required
    'driver' => 'smtp', // Options: 'mail', 'smtp', 'log'
    'from' => '[email protected]', // Sender email address
    'from_name' => 'My Application', // Sender name
    
    // Required for template support
    'template_path' => __DIR__ . '/templates', // Path to email templates
    
    // Required for SMTP
    'smtp_host' => 'smtp.example.com',
    'smtp_port' => 587, // Common ports: 25, 465, 587
    'smtp_username' => 'username',
    'smtp_password' => 'password',
    
    // Optional
    'debug' => false, // Enable debugging (for SMTP)
];

// Create a mail provider instance
$mailer = new \WebsiteSQL\Mailer\MailProvider($mailConfig);

// Example 1: Send HTML email
$mailer->html('<h1>Hello World</h1><p>This is a test email.</p>')
       ->subject('Test HTML Email')
       ->send('[email protected]');

// Example 2: Send plain text email
$mailer->plain('Hello World. This is a test email.')
       ->subject('Test Plain Text Email')
       ->send('[email protected]');

// Example 3: Send email with both HTML and plain text
$mailer->html('<h1>Hello World</h1><p>This is a test email.</p>')
       ->plain('Hello World. This is a test email.')
       ->subject('Test Multipart Email')
       ->send('[email protected]');

// Example 4: Use a template
$mailer->template('welcome', ['name' => 'John Doe'])
       ->subject('Welcome to Our Site')
       ->send('[email protected]');

public function __construct(array $options)

$config = [
    'driver' => 'mail',
    'from' => '[email protected]',
    'from_name' => 'Sender Name'
];

$config = [
    'driver' => 'smtp',
    'from' => '[email protected]',
    'from_name' => 'Sender Name',
    'smtp_host' => 'smtp.example.com',
    'smtp_port' => 587, // 25, 465, or 587
    'smtp_username' => 'username',
    'smtp_password' => 'password',
    'debug' => false // Set to true for debugging
];

$config = [
    'driver' => 'log'
];

// templates/welcome.php
<h1>Welcome, <?= $this->e($name) 

$mailer->template('welcome', ['name' => 'John Doe'])
       ->subject('Welcome')
       ->send('[email protected]');

try {
    $mailer->html('<p>Hello</p>')
           ->subject('Test')
           ->send('[email protected]');
} catch (RuntimeException $e) {
    // Handle the error
    echo "Error: " . $e->getMessage();
}

// Create configuration
$config = [
    'driver' => 'smtp',
    'from' => '[email protected]',
    'from_name' => 'My Application',
    'template_path' => __DIR__ . '/templates',
    'smtp_host' => 'smtp.myapp.com',
    'smtp_port' => 587,
    'smtp_username' => 'smtp_user',
    'smtp_password' => 'smtp_password'
];

// Create mailer
$mailer = new \WebsiteSQL\Mailer\MailProvider($config);

// Send a welcome email
try {
    $mailer->template('welcome', [
              'username' => 'johndoe',
              'app_name' => 'My Application',
              'login_url' => 'https://myapp.com/login'
           ])
           ->subject('Welcome to My Application')
           ->send('[email protected]');
    
    echo "Email sent successfully!";
} catch (RuntimeException $e) {
    echo "Failed to send email: " . $e->getMessage();
}