The concept is that the user can select one or more emails from which to send a message to one or more clients. It’s somewhat like an email campaign, but in this case, I want to send emails by extracting emails from an Excel database (which contains two columns: email and password) that will be used for sending (in this case, only Gmail emails) to one or more clients or their emails, also from an Excel database containing only client emails. The idea is to use Gmail SMTP to send emails. I’m not entirely sure if the logic is best implemented in that part. Is there even a possibility to create a plugin in this way?
The rest are descriptions for other parts of the plugin that I was tasked to create:
Dashboard Integration:
Upon installation and activation of the plugin, I want an icon and title of the plugin to appear on the left side of the dashboard. By clicking on that icon, the user will be able to create a campaign. It is necessary to allow the user to choose the campaign name, email address or multiple addresses of clients from the Excel database “clients,” the time and date of sending the email, and up to 10 emails from a list of 1000 emails. Also, fields for entering the Subject and Message need to be provided, which will be sent to the selected clients.
Automatic Campaign Launch:
The campaign will automatically start when the date and time specified by the user match.
Dashboard Functionalities:
The dashboard will have three items on the WordPress sidebar. The first item, when clicked, will display successfully completed campaigns with the campaign name, client email, date, and time of campaign sending. The second item, after clicking, will display ongoing campaigns, with the option to delete. It will also display the campaign name, client email, date, and time of campaign activation. The third item, when clicked, will allow adding new clients to the Excel database “clients.”
Table Display:
All tables will have a maximum of 10 columns. If the user wants to view more, they can click the “Next page” button to see the next 10 columns or “Previous page” to go back to the previous 10 columns.
require_once 'PHPExcel/Classes/PHPExcel.php';
function get_client_emails_from_excel() {
$excelFilePath = 'clients.xlsx';
$spreadsheet = PHPExcel_IOFactory::load($excelFilePath);
$sheet = $spreadsheet->getSheetByName('clients');
$emailAddresses = array();
foreach ($sheet->getRowIterator() as $row) {
$cellValue = $sheet->getCellByColumnAndRow(1, $row->getRowIndex())->getValue();
if (!empty($cellValue)) {
$emailAddresses[] = $cellValue;
}
}
return $emailAddresses;
}
function get_email_addresses_from_excel() {
$excelFilePath = 'database.xlsx';
$spreadsheet = PHPExcel_IOFactory::load($excelFilePath);
$sheet = $spreadsheet->getSheetByName('database');
$emailAddresses = array();
foreach ($sheet->getRowIterator() as $row) {
$cellValue = $sheet->getCellByColumnAndRow(1, $row->getRowIndex())->getValue();
if (!empty($cellValue)) {
$emailAddresses[] = $cellValue;
}
}
return $emailAddresses;
}
function add_email_campaign_item() {
add_menu_page(
'Email Campaign',
'Email Campaign',
'manage_options',
'email_campaign',
'show_email_campaign_page'
);
}
add_action('admin_menu', 'add_email_campaign_item');
function show_email_campaign_page() {
$clientEmails = get_client_emails_from_excel();
$databaseEmails = get_email_addresses_from_excel();
?>
<div class="wrap">
<h1>Email Campaign</h1>
<form method="post" action="">
<label for="campaign_name">Campaign Name:</label><br>
<input type="text" id="campaign_name" name="campaign_name"><br><br>
<label for="recipient_email">Recipient Email or Multiple Emails from Excel "clients" table:</label><br>
<select multiple id="recipient_email" name="recipient_email[]">
<?php foreach ($clientEmails as $email) : ?>
<option value="<?php echo $email; ?>"><?php echo $email; ?></option>
<?php endforeach; ?>
</select><br><br>
<label for="send_datetime">Date and Time of Sending:</label><br>
<input type="datetime-local" id="send_datetime" name="send_datetime"><br><br>
<label for="email_addresses">Email Addresses from Excel "database" table:</label><br>
<select multiple id="email_addresses" name="email_addresses[]">
<?php foreach ($databaseEmails as $email) : ?>
<option value="<?php echo $email; ?>"><?php echo $email; ?></option>
<?php endforeach; ?>
</select><br><br>
<label for="campaign_subject">Campaign Subject:</label><br>
<input type="text" id="campaign_subject" name="campaign_subject"><br><br>
<label for="message_content">Message Content:</label><br>
<textarea id="message_content" name="message_content" rows="4" cols="50"></textarea><br><br>
<input type="submit" value="Create Campaign">
</form>
</div>
<?php
}
function schedule_campaign_activation($campaign_id, $send_datetime) {
$timestamp = strtotime($send_datetime);
wp_schedule_single_event($timestamp, 'activate_campaign_event', array($campaign_id));
}
function activate_campaign($campaign_id) {
$campaign = get_campaign_details($campaign_id);
if ($campaign) {
$campaign_name = $campaign->name;
$recipient_email = $campaign->recipient_email;
$send_datetime = $campaign->send_datetime;
$campaign_subject = $campaign->campaign_subject;
$message_content = $campaign->message_content;
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . $sender_email,
'Reply-To: ' . $sender_email
);
$email_sent = wp_mail($recipient_email, $campaign_subject, $message_content, $headers);
if ($email_sent) {
update_campaign_status($campaign_id, 'completed');
} else {
update_campaign_status($campaign_id, 'failed');
}
}
}
function get_campaign_details($campaign_id) {
global $wpdb;
$query = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}campaigns WHERE id = %d", $campaign_id);
$campaign = $wpdb->get_row($query);
return $campaign;
}
function update_campaign_status($campaign_id, $status) {
global $wpdb;
$wpdb->update(
"{$wpdb->prefix}campaigns",
array('status' => $status),
array('id' => $campaign_id),
array('%s'),
array('%d')
);
}
add_action('activate_campaign_event', 'activate_campaign');
function display_campaign_dashboard_widget() {
global $wpdb;
echo '<h2>Successfully Completed Campaigns:</h2>';
$completed_campaigns = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}campaigns WHERE status = 'completed'");
if ($completed_campaigns) {
echo '<ul>';
foreach ($completed_campaigns as $campaign) {
echo '<li>' . $campaign->name . ' - Recipient: ' . $campaign->recipient_email . ' - Sent At: ' . $campaign->sent_at . '</li>';
}
echo '</ul>';
} else {
echo 'No completed campaigns.';
}
echo '<h2>Campaigns in Progress:</h2>';
$in_progress_campaigns = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}campaigns WHERE status = 'in_progress'");
if ($in_progress_campaigns) {
echo '<ul>';
foreach ($in_progress_campaigns as $campaign) {
echo '<li>' . $campaign->name . ' - Recipient: ' . $campaign->recipient_email . ' - Activated At: ' . $campaign->activated_at . ' <a href="#" class="delete-campaign" data-campaign-id="' . $campaign->id . '">Delete</a></li>';
}
echo '</ul>';
} else {
echo 'No campaigns in progress.';
}
echo '<h2>Add New Clients:</h2>';
echo '<form method="post" action="">';
echo '<label for="new_client_email">New Client Email:</label>';
echo '<input type="email" id="new_client_email" name="new_client_email" required>';
echo '<input type="submit" value="Add Client">';
echo '</form>';
}
function add_new_client_to_excel($email) {
$excelFilePath = 'clients.xlsx';
$spreadsheet = IOFactory::load($excelFilePath);
$sheet = $spreadsheet->getSheetByName('clients');
$highestRow = $sheet->getHighestDataRow();
$newRow = $highestRow + 1;
$sheet->setCellValueByColumnAndRow(1, $newRow, $email);
$writer = PHPExcel_IOFactory::createWriter($spreadsheet, 'Excel2007');
$writer->save($excelFilePath);
}
function process_add_new_client() {
if (isset($_POST['new_client_email'])) {
$new_client_email = sanitize_email($_POST['new_client_email']);
if (!empty($new_client_email)) {
add_new_client_to_excel($new_client_email);
echo "Done";
}
}
}
add_action('admin_init', 'process_add_new_client');
function register_campaign_dashboard_widgets() {
wp_add_dashboard_widget(
'campaign_dashboard_widget',
'Email Campaigns',
'display_campaign_dashboard_widget'
);
}
add_action('wp_dashboard_setup', 'register_campaign_dashboard_widgets');
function enqueue_delete_campaign_js() {
?>
<script>
jQuery(document).ready(function($) {
$('.delete-campaign').click(function(e) {
e.preventDefault();
var campaignId = $(this).data('campaign-id');
var confirmation = confirm('Are you sure you want to delete this campaign?');
if (confirmation) {
$.post(ajaxurl, { action: 'delete_campaign', campaign_id: campaignId }, function(response) {
location.reload();
});
}
});
});
</script>
<?php
}
add_action('admin_footer', 'enqueue_delete_campaign_js');
function display_add_new_clients_page() {
?>
<div class="wrap">
<h1>Add New Clients</h1>
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th>Email Address</th>
</tr>
</thead>
<tbody>
<?php
$emailAddresses = get_email_addresses_from_excel();
$totalEmails = count($emailAddresses);
$emailsPerPage = 10;
$totalPages = ceil($totalEmails / $emailsPerPage);
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($currentPage - 1) * $emailsPerPage;
$pagedEmails = array_slice($emailAddresses, $offset, $emailsPerPage);
foreach ($pagedEmails as $email) {
echo '<tr><td>' . $email . '</td></tr>';
}
?>
</tbody>
</table>
<div class="tablenav">
<div class="tablenav-pages">
<?php
$pageLinks = paginate_links(array(
'base' => add_query_arg('page', '%#%'),
'format' => '',
'prev_text' => '«',
'next_text' => '»',
'total' => $totalPages,
'current' => $currentPage
));
echo $pageLinks;
?>
</div>
</div>
</div>
<?php
}
function add_new_clients_submenu_page() {
add_submenu_page(
'email_campaign',
'Add New Clients',
'Add New Clients',
'manage_options',
'add_new_clients',
'display_add_new_clients_page'
);
}
add_action('admin_menu', 'add_new_clients_submenu_page');
function configure_google_smtp($phpmailer) {
if (isset($_POST['smtp_email']) && isset($_POST['smtp_password'])) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.gmail.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = $_POST['smtp_email'];
$phpmailer->Password = $_POST['smtp_password'];
$phpmailer->SMTPSecure = 'tls';
$phpmailer->From = $_POST['smtp_email'];
$sender_email = $_POST['smtp_email'];
$sender_name = explode('@', $sender_email)[0];
$phpmailer->FromName = $sender_name;
$phpmailer->setFrom($_POST['smtp_email'], $sender_name);
if (!empty($phpmailer->From)) {
$phpmailer->addReplyTo($phpmailer->From, $phpmailer->FromName);
}
}
}
add_action('phpmailer_init', 'configure_google_smtp');```