Examples
Welcome Email
A typical onboarding email sent when a new user registers:
use WPZylos\Framework\Mail\Mailable;
function send_welcome_email(string $email, string $name): bool
{
$mail = new Mailable();
return $mail->to($email)
->from('[email protected]', 'My Plugin')
->subject("Welcome to My Plugin, {$name}!")
->greeting("Hello {$name}! 👋")
->line('Thank you for creating an account with us.')
->line('Your account is now active and ready to use.')
->action('Get Started', home_url('/dashboard'))
->line('If you have any questions, just reply to this email.')
->send();
}
Order Confirmation with Template
Using a PHP view template for a rich order confirmation email:
use WPZylos\Framework\Mail\MailManager;
function send_order_confirmation(MailManager $mailManager, array $order): bool
{
return $mailManager->to($order['customer_email'])
->subject("Order #{$order['id']} Confirmed")
->view('order-confirmed', [
'order' => $order,
'items' => $order['items'],
'total' => $order['total'],
'shipping' => $order['shipping_address'],
])
->attach($order['invoice_path'])
->send();
}
Template file (resources/mail/order-confirmed.php):
<h1>Order Confirmed!</h1>
<p>Thank you for your order #<?= htmlspecialchars((string) $order['id']) ?>.</p>
<table style="width:100%;border-collapse:collapse;">
<tr style="background:#f5f5f5;">
<th style="padding:8px;text-align:left;">Item</th>
<th style="padding:8px;text-align:right;">Price</th>
</tr>
<?php foreach ($items as $item): ?>
<tr>
<td style="padding:8px;"><?= htmlspecialchars($item['name']) ?></td>
<td style="padding:8px;text-align:right;">$<?= number_format($item['price'], 2) ?></td>
</tr>
<?php endforeach; ?>
<tr style="border-top:2px solid #333;">
<td style="padding:8px;font-weight:bold;">Total</td>
<td style="padding:8px;text-align:right;font-weight:bold;">$<?= number_format($total, 2) ?></td>
</tr>
</table>
Password Reset Email
use WPZylos\Framework\Mail\Mailable;
function send_password_reset(string $email, string $resetUrl): bool
{
$mail = new Mailable();
return $mail->to($email)
->subject('Reset Your Password')
->greeting('Password Reset Request')
->line('We received a request to reset your password.')
->line('Click the button below to set a new password:')
->action('Reset Password', $resetUrl)
->line('This link will expire in 24 hours.')
->line('If you did not request this reset, please ignore this email.')
->send();
}
Admin Notification with Priority
Sending an urgent notification to site administrators:
use WPZylos\Framework\Mail\Mailable;
function notify_admin_critical_error(string $errorMessage): bool
{
$mail = new Mailable();
return $mail->to(get_option('admin_email'))
->subject('[URGENT] Critical Error Detected')
->header('X-Priority', '1')
->header('X-MSMail-Priority', 'High')
->greeting('**Warning:** Critical Error')
->line('A critical error was detected on your site:')
->line($errorMessage)
->action('View Error Log', admin_url('admin.php?page=error-log'))
->line('Please investigate immediately.')
->send();
}
Bulk Email to Multiple Recipients
use WPZylos\Framework\Mail\MailManager;
function send_newsletter(MailManager $mailManager, array $subscribers, string $content): void
{
$mailManager->setFrom('[email protected]', 'My Newsletter');
foreach ($subscribers as $subscriber) {
$mailManager->to($subscriber['email'])
->subject('Weekly Newsletter')
->greeting("Hi {$subscriber['name']}!")
->line($content)
->action('Read More', home_url('/blog'))
->send();
}
}
Email with Multiple Attachments
use WPZylos\Framework\Mail\Mailable;
function send_report_email(string $email, array $reportFiles): bool
{
$mail = new Mailable();
$mail->to($email)
->subject('Monthly Reports')
->greeting('Monthly Report Package')
->line('Please find your monthly reports attached.');
foreach ($reportFiles as $file) {
$mail->attach($file);
}
$mail->line('Reports are generated as of ' . date('F j, Y') . '.');
return $mail->send();
}
HTML Email with Custom Styling
use WPZylos\Framework\Mail\Mailable;
function send_styled_email(string $email): bool
{
$html = <<<HTML
<div style="font-family:Arial,sans-serif;max-width:600px;margin:0 auto;">
<div style="background:#2271b1;color:#fff;padding:20px;text-align:center;">
<h1 style="margin:0;">My Plugin</h1>
</div>
<div style="padding:20px;">
<h2>Your Account Summary</h2>
<p>Here's a quick overview of your activity this month:</p>
<ul>
<li>Posts created: <strong>12</strong></li>
<li>Comments received: <strong>48</strong></li>
<li>Page views: <strong>3,204</strong></li>
</ul>
</div>
<div style="background:#f5f5f5;padding:10px;text-align:center;font-size:12px;color:#666;">
<p>You received this email because you are subscribed to monthly summaries.</p>
</div>
</div>
HTML;
$mail = new Mailable();
return $mail->to($email)
->subject('Your Monthly Summary')
->html($html)
->send();
}
CC and BCC for Team Notifications
use WPZylos\Framework\Mail\Mailable;
function send_team_notification(string $primaryRecipient, array $teamEmails): bool
{
$mail = new Mailable();
return $mail->to($primaryRecipient)
->cc($teamEmails)
->bcc('[email protected]')
->replyTo('[email protected]')
->subject('New Task Assigned')
->greeting('New Task Assignment')
->line('A new task has been assigned to the team.')
->action('View Task', home_url('/tasks/new'))
->line('Please review and assign a priority.')
->send();
}