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)]; } }