166 lines
5.2 KiB
PHP
166 lines
5.2 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* Email Sender Class
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BD_Email_Sender {
|
|
public static function send_newsletter($subject, $content) {
|
|
$settings = BD_SimpleEmail::get_settings();
|
|
|
|
if (empty($settings['gmail_address']) || empty($settings['gmail_password'])) {
|
|
return array(
|
|
'success' => false,
|
|
'message' => __('Gmail not configured', 'bd-simpleemail'),
|
|
);
|
|
}
|
|
|
|
$subscribers = BD_SimpleEmail::get_subscribers();
|
|
|
|
if (empty($subscribers)) {
|
|
return array(
|
|
'success' => false,
|
|
'message' => __('No subscribers found', 'bd-simpleemail'),
|
|
);
|
|
}
|
|
|
|
$business = BD_SimpleEmail::get_business_info();
|
|
|
|
$html = self::build_email_html($subject, $content, $business);
|
|
|
|
$sent = 0;
|
|
|
|
foreach ($subscribers as $subscriber) {
|
|
$result = self::send_email(
|
|
$subscriber->email,
|
|
$subject,
|
|
$html,
|
|
$subscriber->unsubscribe_token
|
|
);
|
|
|
|
if ($result) {
|
|
$sent++;
|
|
}
|
|
}
|
|
|
|
return array(
|
|
'success' => true,
|
|
'count' => $sent,
|
|
);
|
|
}
|
|
|
|
private static function send_email($to, $subject, $html, $unsubscribe_token = '') {
|
|
$settings = BD_SimpleEmail::get_settings();
|
|
|
|
$headers = array(
|
|
'Content-Type: text/html; charset=UTF-8',
|
|
'From: ' . $settings['sender_name'] . ' <' . $settings['gmail_address'] . '>',
|
|
);
|
|
|
|
if (!empty($settings['reply_to'])) {
|
|
$headers[] = 'Reply-To: ' . $settings['reply_to'];
|
|
}
|
|
|
|
$headers[] = 'MIME-Version: 1.0';
|
|
|
|
return wp_mail($to, $subject, $html, $headers);
|
|
}
|
|
|
|
private static function build_email_html($subject, $content, $business) {
|
|
$site_url = get_bloginfo('url');
|
|
$site_name = get_bloginfo('name');
|
|
|
|
$unsubscribe_link = add_query_arg(array(
|
|
'unsubscribe' => '{{email}}',
|
|
'token' => '{{token}}',
|
|
), home_url('/unsubscribe/'));
|
|
|
|
$address_html = '';
|
|
if ($business['name']) {
|
|
$address_html = '<p>' . esc_html($business['name']) . '<br>';
|
|
if ($business['address_1']) {
|
|
$address_html .= esc_html($business['address_1']) . '<br>';
|
|
}
|
|
if ($business['address_2']) {
|
|
$address_html .= esc_html($business['address_2']) . '<br>';
|
|
}
|
|
if ($business['city']) {
|
|
$address_html .= esc_html($business['city']) . '<br>';
|
|
}
|
|
if ($business['country']) {
|
|
$address_html .= esc_html($business['country']);
|
|
}
|
|
$address_html .= '</p>';
|
|
}
|
|
|
|
$html = '
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>' . esc_html($subject) . '</title>
|
|
<style>
|
|
body { margin: 0; padding: 0; font-family: Georgia, serif; font-size: 16px; line-height: 1.6; color: #2C2C2C; background-color: #F5F0E8; }
|
|
.email-container { max-width: 600px; margin: 0 auto; background-color: #ffffff; }
|
|
.email-header { background-color: #1B3A6B; padding: 30px; text-align: center; }
|
|
.email-header h1 { color: #C8A96E; margin: 0; font-size: 28px; }
|
|
.email-body { padding: 30px; }
|
|
.email-footer { background-color: #EDE4D0; padding: 20px 30px; text-align: center; font-size: 12px; color: #555555; }
|
|
a { color: #2E6DA4; }
|
|
.unsubscribe-link { color: #888888; text-decoration: underline; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="email-container">
|
|
<div class="email-header">
|
|
<h1>' . esc_html($site_name) . '</h1>
|
|
</div>
|
|
<div class="email-body">
|
|
' . $content . '
|
|
</div>
|
|
<div class="email-footer">
|
|
<p>
|
|
<a href="' . esc_url($site_url) . '">' . esc_html($site_url) . '</a>
|
|
</p>
|
|
' . $address_html . '
|
|
<p>
|
|
<a href="' . esc_url($unsubscribe_link) . '">Unsubscribe</a> from this mailing list.
|
|
</p>
|
|
<p>You received this email because you signed up for updates from ' . esc_html($site_name) . '.</p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
';
|
|
|
|
return $html;
|
|
}
|
|
|
|
public static function encrypt_password($password) {
|
|
$key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_email_secret';
|
|
$iv = openssl_random_pseudo_bytes(16);
|
|
|
|
$encrypted = openssl_encrypt($password, 'AES-256-CBC', $key, 0, $iv);
|
|
|
|
return base64_encode($iv . $encrypted);
|
|
}
|
|
|
|
public static function decrypt_password($encrypted) {
|
|
$data = base64_decode($encrypted);
|
|
|
|
$key = defined('AUTH_KEY') ? AUTH_KEY : 'bdm_email_secret';
|
|
|
|
$iv = substr($data, 0, 16);
|
|
$ciphertext = substr($data, 16);
|
|
|
|
return openssl_decrypt($ciphertext, 'AES-256-CBC', $key, 0, $iv);
|
|
}
|
|
}
|