$form_id, 'schema' => $schema, 'values' => $values, 'entry' => $entry ); $to = array(); switch ( $n['to_type'] ?? 'email' ) { case 'field': $to = self::emails( (string) ( $values[ $n['to_field'] ] ?? '' ) ); break; case 'routing': foreach ( (array) $n['routing'] as $route ) { if ( ! empty( $route['email'] ) && Conditions::passes( array( 'match' => 'all', 'rules' => array( $route ) ), $values ) ) { $to = array_merge( $to, self::emails( Smartcodes::parse( $route['email'], $ctx, 'text' ) ) ); } } break; default: $to = self::emails( Smartcodes::parse( (string) $n['to'], $ctx, 'text' ) ); } $to = array_values( array_unique( $to ) ); if ( ! $to ) { return false; } $from_email = Smartcodes::parse( (string) ( $n['from_email'] ? $n['from_email'] : Settings::get( 'email.from_email' ) ), $ctx, 'text' ); $from_name = Smartcodes::parse( (string) ( $n['from_name'] ? $n['from_name'] : Settings::get( 'email.from_name' ) ), $ctx, 'text' ); $headers = array( 'Content-Type: text/html; charset=UTF-8' ); if ( is_email( $from_email ) ) { $headers[] = 'From: ' . self::header_safe( $from_name ? $from_name : get_bloginfo( 'name' ) ) . ' <' . $from_email . '>'; } foreach ( self::emails( Smartcodes::parse( (string) $n['reply_to'], $ctx, 'text' ) ) as $r ) { $headers[] = 'Reply-To: ' . $r; break; } foreach ( self::emails( Smartcodes::parse( (string) $n['cc'], $ctx, 'text' ) ) as $c ) { $headers[] = 'Cc: ' . $c; } foreach ( self::emails( Smartcodes::parse( (string) $n['bcc'], $ctx, 'text' ) ) as $c ) { $headers[] = 'Bcc: ' . $c; } $subject = self::header_safe( Smartcodes::parse( (string) $n['subject'], $ctx, 'text' ) ); $body = Smartcodes::parse( (string) $n['message'], $ctx, 'html' ); if ( false === strpos( $body, ' $form_id, 'entry' => (int) ( $entry['id'] ?? 0 ), 'name' => $n['name'] ?? '' ); $ok = wp_mail( $args['to'], $args['subject'], $args['html'], $args['headers'], $args['attachments'] ); self::$sending = null; /* translators: 1: notification, 2: recipients */ Logs::add( $form_id, (int) ( $entry['id'] ?? 0 ), 'email', $ok ? 'success' : 'failed', sprintf( __( '%1$s → %2$s', 'fieldwren' ), $n['name'] ?? __( 'Email', 'fieldwren' ), implode( ', ', $args['to'] ) ), $subject ); return (bool) $ok; } /** Log wp_mail failures. */ public static function mail_failed( $error ) { if ( self::$sending && $error instanceof \WP_Error ) { Logs::add( self::$sending['form'], self::$sending['entry'], 'email', 'failed', self::$sending['name'], $error->get_error_message() ); } } /** @return string[] Valid emails from a comma list. */ public static function emails( $s ) { $out = array(); foreach ( preg_split( '/[,;\s]+/', (string) $s ) as $e ) { $e = sanitize_email( trim( $e ) ); if ( $e && is_email( $e ) ) { $out[] = $e; } } return $out; } /** Strip line breaks from header values. */ private static function header_safe( $s ) { return trim( str_replace( array( "\r", "\n" ), ' ', wp_strip_all_tags( html_entity_decode( (string) $s, ENT_QUOTES ) ) ) ); } /** * Email HTML wrapper. * * @param string $body Inner HTML. * @param string $subject Subject (preheader). * @param int $form_id Form id. * @return string */ public static function wrap( $body, $subject, $form_id = 0 ) { $e = Settings::get( 'email' ); if ( 'plain' === ( $e['template'] ?? 'styled' ) ) { return $body; } $custom = apply_filters( 'fwren/email_wrap', null, $body, $subject, $form_id ); if ( null !== $custom ) { return $custom; } $color = sanitize_hex_color( $e['color'] ?? '' ) ? $e['color'] : '#2563eb'; $logo = ! empty( $e['logo'] ) ? '' . esc_attr( get_bloginfo( 'name' ) ) . '' : '' . esc_html( get_bloginfo( 'name' ) ) . ''; ob_start(); ?> <?php echo esc_html( $subject ); ?>