', 'Content-Type: text/html; charset=UTF-8' ); } // Send the email $sent = wp_mail($to, $subject, $message, $headers); // Log the email $table_name = $wpdb->prefix . 'wpdd_email_logs'; // Create table if it doesn't exist if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { self::create_email_logs_table(); } // Get error message if failed $error_message = ''; if (!$sent) { global $phpmailer; if (isset($phpmailer) && is_object($phpmailer) && !empty($phpmailer->ErrorInfo)) { $error_message = $phpmailer->ErrorInfo; } } // Insert log entry $wpdb->insert( $table_name, array( 'to_email' => $to, 'subject' => $subject, 'message' => $message, 'status' => $sent ? 'sent' : 'failed', 'email_type' => $email_type, 'error_message' => $error_message, 'sent_at' => current_time('mysql') ), array('%s', '%s', '%s', '%s', '%s', '%s', '%s') ); return $sent; } /** * Configure SMTP settings for PHPMailer */ public static function configure_smtp($phpmailer) { $phpmailer->isSMTP(); $phpmailer->Host = get_option('wpdd_smtp_host'); $phpmailer->Port = get_option('wpdd_smtp_port', 587); $phpmailer->SMTPAuth = true; $phpmailer->Username = get_option('wpdd_smtp_username'); $phpmailer->Password = get_option('wpdd_smtp_password'); $phpmailer->SMTPSecure = get_option('wpdd_smtp_encryption', 'tls'); $phpmailer->From = get_option('wpdd_from_email', get_option('admin_email')); $phpmailer->FromName = get_option('wpdd_from_name', get_bloginfo('name')); // Enable debugging for failed sends if (defined('WP_DEBUG') && WP_DEBUG) { $phpmailer->SMTPDebug = 2; $phpmailer->Debugoutput = 'error_log'; } } /** * Create email logs table if it doesn't exist */ private static function create_email_logs_table() { global $wpdb; $table_name = $wpdb->prefix . 'wpdd_email_logs'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id bigint(20) NOT NULL AUTO_INCREMENT, to_email varchar(100) NOT NULL, subject varchar(255) NOT NULL, message longtext NOT NULL, status varchar(20) NOT NULL DEFAULT 'sent', email_type varchar(50) DEFAULT 'general', error_message text DEFAULT NULL, sent_at datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY to_email (to_email), KEY status (status), KEY email_type (email_type), KEY sent_at (sent_at) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } /** * Send order confirmation email */ public static function send_order_confirmation($order_id) { global $wpdb; $order = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpdd_orders WHERE id = %d", $order_id )); if (!$order) { return false; } $product = get_post($order->product_id); if (!$product) { return false; } $subject = sprintf(__('Order Confirmation - %s', 'wp-digital-download'), $product->post_title); $message = sprintf( __('

Thank you for your purchase!

Your order has been confirmed.

Order Details:

You can download your purchase from your account page.

Thank you for your business!

', 'wp-digital-download'), $order->id, esc_html($product->post_title), wpdd_format_price($order->amount, $order->currency), $order->purchase_date ); return self::send($order->customer_email, $subject, $message, 'order_confirmation'); } /** * Send download link email */ public static function send_download_link($order_id, $download_link) { global $wpdb; $order = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpdd_orders WHERE id = %d", $order_id )); if (!$order) { return false; } $product = get_post($order->product_id); if (!$product) { return false; } $subject = sprintf(__('Your Download Link - %s', 'wp-digital-download'), $product->post_title); $message = sprintf( __('

Your Download is Ready!

Click the link below to download your purchase:

Download Now

Product:

%s

Note: This download link will expire. Please download your file as soon as possible.

Thank you!

', 'wp-digital-download'), esc_url($download_link), esc_html($product->post_title) ); return self::send($order->customer_email, $subject, $message, 'download_link'); } }