//--------------------------------------------------------------------------- // SBE 2026-11-18: Welcome emails, sent straight to the customer. // // These used to be mailed to help@buildy.ai, which was the cue for someone to // send the real welcome email by hand. The subject and body now live on // wp_orders_groups (email_subject / email_body) and go to the buyer directly. // // Run orders_group_emails.sql once to add the email_body column and load the // 18 templates exported from Zammad. //--------------------------------------------------------------------------- if ( ! defined('ORDERS_BUILDY_FROM_EMAIL') ) define('ORDERS_BUILDY_FROM_EMAIL', 'help@buildy.ai'); if ( ! defined('ORDERS_BUILDY_FROM_NAME') ) define('ORDERS_BUILDY_FROM_NAME', 'Buildy Team'); if ( ! defined('ORDERS_GROUP_EMAIL_LOG') ) define('ORDERS_GROUP_EMAIL_LOG', dirname(__FILE__) . '/logs/welcome_email.log'); /** * Fold every non-ASCII character to an HTML numeric entity. * * wp_orders_groups is latin1 and cannot store emoji at all. Because the body * is sent as HTML, '🚀' renders as the rocket it was written as, so * the column only ever needs to hold ASCII. Pure byte maths - no mbstring. * * @param string $text UTF-8 in. * @return string ASCII out. */ function orders_group_email_encode( $text ) { $text = (string) $text; if ( $text === '' ) return $text; $out = ''; $len = strlen( $text ); for ( $i = 0; $i < $len; $i++ ) { $c = ord( $text[$i] ); if ( $c < 0x80 ) { $out .= $text[$i]; continue; } if ( ($c & 0xE0) === 0xC0 ) { $n = 1; $cp = $c & 0x1F; } elseif ( ($c & 0xF0) === 0xE0 ) { $n = 2; $cp = $c & 0x0F; } elseif ( ($c & 0xF8) === 0xF0 ) { $n = 3; $cp = $c & 0x07; } else { continue; } // stray continuation byte - drop it for ( $k = 1; $k <= $n; $k++ ) { if ( ! isset($text[$i + $k]) ) break; $cp = ( $cp << 6 ) | ( ord($text[$i + $k]) & 0x3F ); } $i += $n; $out .= '&#' . $cp . ';'; } return $out; } /** * Turn stored entities back into UTF-8, for showing in the admin editor. * * @param string $text * @return string */ function orders_group_email_decode( $text ) { return html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ); } /** * Substitute {first_name} / {last_name} / {name} / {email} / {product}. * * Unknown tags are left alone rather than blanked, so a typo is visible in a * test send instead of silently producing "Hi ,". * * @param string $text * @param array $vars * @return string */ function orders_group_email_merge( $text, $vars ) { if ( empty($vars) || ! is_array($vars) ) return $text; foreach ( $vars as $k => $v ) { $text = str_replace( '{' . $k . '}', (string) $v, $text ); } return $text; } /** * Send a product group's welcome email to the customer. * * @param int $group_id * @param string $to_email * @param array $vars Merge values, e.g. array('first_name' => 'Jack'). * @return bool True if wp_mail accepted it. */ function orders_group_send_welcome_email( $group_id, $to_email, $vars = array() ) { global $wpdb; $group_id = (int) $group_id; $to_email = trim( (string) $to_email ); if ( ! $group_id || ! is_email($to_email) ) { orders_group_email_log( "INVALID group={$group_id} to=\"{$to_email}\"" ); return false; } $group = $wpdb->get_row( $wpdb->prepare( "SELECT group_id, name, email_subject, email_body FROM {$wpdb->prefix}orders_groups WHERE group_id = %d", $group_id ) ); if ( ! $group ) { orders_group_email_log( "NO SUCH GROUP group={$group_id} to={$to_email}" ); return false; } $subject = trim( (string) $group->email_subject ); $body = (string) $group->email_body; // Never send a blank welcome email - say nothing went out and why. if ( $subject === '' || trim($body) === '' ) { $missing = ( $subject === '' ) ? 'subject' : 'body'; orders_group_email_log( "SKIPPED group={$group_id} \"{$group->name}\" to={$to_email}: no {$missing} configured" ); return false; } $subject = orders_group_email_merge( orders_group_email_decode($subject), $vars ); $body = orders_group_email_merge( $body, $vars ); // Stored as plain text with entities; make URLs clickable, keep the line // breaks, and send as HTML so the entities render. $html = nl2br( make_clickable( $body ) ); $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . ORDERS_BUILDY_FROM_NAME . ' <' . ORDERS_BUILDY_FROM_EMAIL . '>', 'Reply-To: ' . ORDERS_BUILDY_FROM_EMAIL, ); $sent = wp_mail( $to_email, $subject, $html, $headers ); orders_group_email_log( ( $sent ? 'SENT' : 'FAILED' ) . " group={$group_id} \"{$group->name}\" to={$to_email} subject=\"{$subject}\"" ); return (bool) $sent; } /** * Append one line to the welcome-email log. * * @param string $line * @return void */ function orders_group_email_log( $line ) { @error_log( date('Y-m-d H:i:s') . ' ' . $line . "\n", 3, ORDERS_GROUP_EMAIL_LOG ); }