Skip to content

Instantly share code, notes, and snippets.

@bgrgicak
Created July 10, 2026 11:39
Show Gist options
  • Select an option

  • Save bgrgicak/d1893d2c1d5e9215666d6cf3f194f224 to your computer and use it in GitHub Desktop.

Select an option

Save bgrgicak/d1893d2c1d5e9215666d6cf3f194f224 to your computer and use it in GitHub Desktop.
Admin email sender plugin
<?php
/**
* Plugin Name: Admin Email Sender
* Description: Adds a wp-admin page that lets administrators send plain-text emails using WordPress wp_mail().
* Version: 1.0.0
* Author: Your Name
* License: GPL-2.0-or-later
*/
if (!defined('ABSPATH')) {
exit;
}
class Admin_Email_Sender {
const MENU_SLUG = 'admin-email-sender';
const NONCE_ACTION = 'admin_email_sender_send';
const NONCE_NAME = 'admin_email_sender_nonce';
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_page']);
add_action('admin_post_admin_email_sender_send', [$this, 'handle_send']);
}
public function add_admin_page() {
add_menu_page(
__('Email Sender', 'admin-email-sender'),
__('Email Sender', 'admin-email-sender'),
'manage_options',
self::MENU_SLUG,
[$this, 'render_page'],
'dashicons-email-alt',
80
);
}
public function render_page() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to access this page.', 'admin-email-sender'));
}
$status = isset($_GET['aes_status']) ? sanitize_key($_GET['aes_status']) : '';
?>
<div class="wrap">
<h1><?php echo esc_html__('Email Sender', 'admin-email-sender'); ?></h1>
<?php if ($status === 'sent') : ?>
<div class="notice notice-success is-dismissible">
<p><?php echo esc_html__('Email sent successfully.', 'admin-email-sender'); ?></p>
</div>
<?php elseif ($status === 'failed') : ?>
<div class="notice notice-error is-dismissible">
<p><?php echo esc_html__('Email could not be sent. Check your WordPress mail configuration.', 'admin-email-sender'); ?></p>
</div>
<?php elseif ($status === 'invalid') : ?>
<div class="notice notice-error is-dismissible">
<p><?php echo esc_html__('Please enter a valid recipient email address, subject, and message.', 'admin-email-sender'); ?></p>
</div>
<?php endif; ?>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
<?php wp_nonce_field(self::NONCE_ACTION, self::NONCE_NAME); ?>
<input type="hidden" name="action" value="admin_email_sender_send">
<table class="form-table" role="presentation">
<tr>
<th scope="row">
<label for="aes_to"><?php echo esc_html__('To', 'admin-email-sender'); ?></label>
</th>
<td>
<input name="aes_to" id="aes_to" type="email" class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row">
<label for="aes_subject"><?php echo esc_html__('Subject', 'admin-email-sender'); ?></label>
</th>
<td>
<input name="aes_subject" id="aes_subject" type="text" class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row">
<label for="aes_message"><?php echo esc_html__('Message', 'admin-email-sender'); ?></label>
</th>
<td>
<textarea name="aes_message" id="aes_message" rows="10" class="large-text" required></textarea>
</td>
</tr>
</table>
<?php submit_button(__('Send Email', 'admin-email-sender')); ?>
</form>
</div>
<?php
}
public function handle_send() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to send emails.', 'admin-email-sender'));
}
check_admin_referer(self::NONCE_ACTION, self::NONCE_NAME);
$to = isset($_POST['aes_to']) ? sanitize_email(wp_unslash($_POST['aes_to'])) : '';
$subject = isset($_POST['aes_subject']) ? sanitize_text_field(wp_unslash($_POST['aes_subject'])) : '';
$message = isset($_POST['aes_message']) ? sanitize_textarea_field(wp_unslash($_POST['aes_message'])) : '';
if (!is_email($to) || empty($subject) || empty($message)) {
$this->redirect_with_status('invalid');
}
$headers = ['Content-Type: text/plain; charset=UTF-8'];
$sent = wp_mail($to, $subject, $message, $headers);
$this->redirect_with_status($sent ? 'sent' : 'failed');
}
private function redirect_with_status($status) {
wp_safe_redirect(
add_query_arg(
'aes_status',
sanitize_key($status),
admin_url('admin.php?page=' . self::MENU_SLUG)
)
);
exit;
}
}
new Admin_Email_Sender();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment