PHP code example of pardnchiu / mailer

1. Go to this page and download the library: Download pardnchiu/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/ */

    

pardnchiu / mailer example snippets




use pardnchiu\Mailer;

// Direct static call
$result = Mailer::send([
  "email"     => "[email protected]",
  "subject"   => "Test Email",
  "body"      => "<h1>This is a test email</h1>",
  "isHtml"    => true
]);

if ($result) {
  echo "Email sent successfully";
} else {
  echo "Email sending failed";
}



use pardnchiu\Mailer;

// Initialize mail client
$mailer = new Mailer();

// Basic email sending (calling static method through instance)
$result = $mailer::send([
  "email"     => "[email protected]",
  "subject"   => "Test Email",
  "body"      => "<h1>This is a test email</h1>",
  "isHtml"    => true
]);

if ($result) {
  echo "Email sent successfully";
} else {
  echo "Email sending failed";
}

$result = Mailer::send([
  "email"     => "[email protected]",          // Recipient ( "Email Content",                  // Content ( Sender email (optional, defaults to configured account)
  "fromName"  => "Sender Name",                   // Sender name (optional)
  "cc"        => ["[email protected]"],              // CC (optional)
  "bcc"       => ["[email protected]"],             // BCC (optional)
  "priority"  => "high",                          // Priority: high/normal/low (optional)
  "isHtml"    => true                             // HTML format (optional, default false)
]);

$config = [
  "email" => [
    "[email protected]" => "User One",
    "[email protected]" => "User Two",
    "[email protected]"
  ],
  "subject" => "Group Notification",
  "body"    => "This is a group notification email"
];

$result = Mailer::send($config);

$config = [
  "email"   => "[email protected]",
  "cc"      => [
    "[email protected]" => "CC User One",
    "[email protected]"
  ],
  "bcc"     => [
    "[email protected]" => "BCC User One",
    "[email protected]"
  ],
  "subject" => "Important Notice",
  "body"    => "Email content"
];

$result = Mailer::send($config);

$results = Mailer::sendBulk(
  [
    "[email protected]" => "User One",
    "[email protected]" => "User Two",
    "[email protected]" => "User Three"
  ],
  "Bulk Notification Email",
  "<h1>This is a bulk sent email</h1>",
  [
    "isHtml"    => true,
    "priority"  => "normal",
    "fromName"  => "System Administrator"
  ]
);

// Check sending results
foreach ($results as $email => $success) {
  if ($success) {
    echo "Successfully sent to {$email}\n";
  } else {
    echo "Failed to send to {$email}\n";
  }
}

try {
  $mailer = new Mailer();
  
  $result = Mailer::send([
    "email"   => "[email protected]",
    "subject" => "Test Email",
    "body"    => "Test content"
  ]);
  
  if ($result) {
    echo "Email sent successfully";
  } else {
    echo "Email sending failed";
  }
    
} catch (\Exception $e) {
  error_log("Email sending error: " . $e->getMessage());
  
  if (strpos($e->getMessage(), "SMTP connect() failed") !== false) {
    echo "SMTP connection failed, please check server settings";
  } elseif (strpos($e->getMessage(), "Authentication") !== false) {
    echo "SMTP authentication failed, please check username and password";
  } else {
    echo "Email sending exception: " . $e->getMessage();
  }
}

// Test SMTP connection
try {
  $mailer = new Mailer();
  
  // Send test email
  $result = Mailer::send([
      "email"   => "[email protected]",
      "subject" => "SMTP Connection Test",
      "body"    => "If you receive this email, SMTP configuration is correct."
  ]);
  
  echo $result ? "SMTP configuration is correct" : "SMTP configuration error";
  
} catch (\Exception $e) {
  echo "SMTP test failed: " . $e->getMessage();
}

$results = Mailer::sendBulk([
    "[email protected]" => "User One",
    "[email protected]" => "User Two",
    "[email protected]" => "User Three"
], "Notification", "Content");

$successCount = array_sum($results);
$totalCount = count($results);
$failureCount = $totalCount - $successCount;

echo "Bulk sending completed - Success: {$successCount}, Failed: {$failureCount}, Total: {$totalCount}";