rl-warmup-plugin/includes/class-rl-mailwarmer-email-handler.php

157 lines
5.1 KiB
PHP

<?php
/**
* Handles email sending for RL MailWarmer.
*/
if (!defined('ABSPATH')) {
exit;
}
class RL_MailWarmer_Email_Handler
{
/**
* Initialize the email handler.
*/
public static function init()
{
// Additional hooks or filters can be added here if needed
}
/**
* Send an email based on the provided email data.
*
* @param array $email The email data, including sender, recipient, subject, and body.
* @return bool True if the email was sent successfully, false otherwise.
*/
public static function send_email(array $email): bool
{
// Load WordPress PHPMailer
$phpmailer = wp_mail();
// Configure SMTP settings
add_action('phpmailer_init', function ($phpmailer) use ($email) {
$phpmailer->isSMTP();
$phpmailer->Host = $email['smtp_host'];
$phpmailer->SMTPAuth = true;
$phpmailer->Username = $email['smtp_username'];
$phpmailer->Password = $email['smtp_password'];
$phpmailer->SMTPSecure = $email['smtp_encryption']; // SSL or TLS
$phpmailer->Port = $email['smtp_port'];
});
// Prepare the email
$to = $email['recipient'];
$subject = $email['subject'];
$body = $email['body'];
$headers = [
'From: ' . $email['sender_name'] . ' <' . $email['sender_email'] . '>',
'Reply-To: ' . $email['reply_to'],
];
// Send the email
return wp_mail($to, $subject, $body, $headers);
}
/**
* Generate random email content.
*
* @param int $campaign_id The campaign ID.
* @param string $recipient The recipient email address.
* @return array The email content including subject and body.
*/
public static function generate_email_content(int $campaign_id, string $recipient): array
{
// Fetch predefined topics and names
$topics = explode("\n", get_field('default_topic_pool', 'option'));
$first_names = explode("\n", get_field('valid_first_name_pool', 'option'));
$last_names = explode("\n", get_field('valid_last_name_pool', 'option'));
// Randomize participants and topics
$subject = self::random_element($topics);
$sender_name = self::random_element($first_names) . ' ' . self::random_element($last_names);
$body_content = "Hi {$recipient},\n\n" .
"I wanted to reach out to discuss {$subject}. Let's connect soon!\n\n" .
"Best,\n{$sender_name}";
return [
'subject' => $subject,
'body' => $body_content,
];
}
/**
* Generate email schedule based on warmup calculations.
*
* @param int $campaign_id The campaign ID.
* @return array The email schedule.
*/
public static function generate_email_schedule(int $campaign_id): array
{
$start_date = get_field('start_date', $campaign_id) ?: date('Ymd');
$warmup_period = (int) get_field('warmup_period', $campaign_id);
$target_volume = (int) get_field('target_volume', $campaign_id);
$emails_per_day = $target_volume / ($warmup_period * 7);
$schedule = [];
$current_date = strtotime($start_date);
for ($week = 0; $week < $warmup_period; $week++) {
for ($day = 1; $day <= 7; $day++) {
if (self::is_weekend($current_date)) {
$current_date = strtotime('+1 day', $current_date);
continue;
}
for ($email_count = 0; $email_count < $emails_per_day; $email_count++) {
$schedule[] = [
'send_time' => date('Y-m-d H:i:s', self::random_business_hour($current_date)),
'sent' => false,
];
}
$current_date = strtotime('+1 day', $current_date);
}
}
return $schedule;
}
/**
* Check if a timestamp falls on a weekend.
*
* @param int $timestamp The timestamp to check.
* @return bool True if it's a weekend, false otherwise.
*/
private static function is_weekend(int $timestamp): bool
{
$day = date('N', $timestamp);
return $day >= 6;
}
/**
* Generate a random business hour timestamp for a given day.
*
* @param int $timestamp The day's timestamp.
* @return int The timestamp within business hours.
*/
private static function random_business_hour(int $timestamp): int
{
$start_hour = 8; // 8 AM
$end_hour = 18; // 6 PM
$random_hour = mt_rand($start_hour, $end_hour - 1);
$random_minute = mt_rand(0, 59);
return strtotime(date('Y-m-d', $timestamp) . " {$random_hour}:{$random_minute}:00");
}
/**
* Get a random element from an array.
*
* @param array $array The array to pick from.
* @return mixed The random element.
*/
private static function random_element(array $array)
{
return $array[array_rand($array)];
}
}