Filter uncanny-automator

automator_discord_invite_email_headers

Filters the email headers sent for Discord invite notifications. Modify headers before they are sent.

add_filter( 'automator_discord_invite_email_headers', $callback, 10, 2 );

Description

Allows developers to modify the email headers used when sending a Discord invite via email. This hook fires just before the email is sent. Developers can add, remove, or alter header values, such as CC or BCC recipients, or change the content type. The `$this` parameter provides access to the action object for context.


Usage

add_filter( 'automator_discord_invite_email_headers', 'your_function_name', 10, 2 );

Parameters

$headers (mixed)
This parameter contains an array of email headers for the Discord invite email.
$this (mixed)
This parameter contains an array of email headers, which can be modified by the filter.

Return Value

The filtered value.


Examples

// Add a custom 'X-Mailer' header for tracking or debugging purposes.
add_filter( 'automator_discord_invite_email_headers', function( $headers, $automator_instance ) {
    // You can inspect the $automator_instance to potentially get more context
    // about the specific automation that is triggering this email.
    // For example, if you wanted to add a header based on the trigger ID:
    // if ( isset( $automator_instance->trigger_id ) ) {
    //     $headers[] = 'X-Trigger-ID: ' . $automator_instance->trigger_id;
    // }

    $headers[] = 'X-Mailer: UncannyAutomatorDiscordBot';
    return $headers;
}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/discord/actions/discord-invite-member-to-server.php:245

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		// Required fields - throws error if not set and valid.
		$server_id = $this->helpers->get_server_id_from_parsed( $parsed, $this->server_key );

		// Required email fields.
		$email_to   = $this->get_valid_email( $parsed, $this->get_action_meta(), esc_html_x( 'Email', 'Discord', 'uncanny-automator' ) );
		$email_from = $this->get_valid_email( $parsed, 'EMAILFROM', esc_html_x( 'From', 'Discord', 'uncanny-automator' ) );
		$reply_to   = $this->get_valid_email( $parsed, 'REPLYTO', esc_html_x( 'Reply to', 'Discord', 'uncanny-automator' ) );
		$subject    = $this->helpers->get_text_value_from_parsed( $parsed, 'SUBJECT', esc_html_x( 'Email subject is required', 'Discord', 'uncanny-automator' ) );
		$email_body = isset( $action_data['meta']['EMAILBODY'] ) ? $action_data['meta']['EMAILBODY'] : '';
		if ( empty( $email_body ) ) {
			throw new Exception( esc_html_x( 'Email body is required', 'Discord', 'uncanny-automator' ) );
		}

		// Required channel ID - throws error if not set and valid.
		$channel_id = $this->helpers->get_channel_id_from_parsed( $parsed, 'CHANNEL', $server_id );

		// Make request for invite URL.
		$invite_url = $this->get_invite_url( $server_id, $channel_id, $action_data );

		// Prepare token variables.
		$server_name  = $parsed[ $this->server_key . '_readable' ];
		$channel_name = $this->helpers->get_channel_name_token_value(
			$parsed['CHANNEL_readable'],
			$channel_id,
			$server_id
		);

		// Hydrate tokens.
		$this->hydrate_tokens(
			array(
				'SERVER_ID'    => $server_id,
				'SERVER_NAME'  => $server_name,
				'CHANNEL_NAME' => $channel_name,
				'INVITE_URL'   => $invite_url,
			)
		);

		// Prepare the email body tokens.
		$email_body = str_ireplace( '{{invite_url}}', $invite_url, $email_body );
		$email_body = str_ireplace( '{{server_name}}', $server_name, $email_body );
		$email_body = str_ireplace( '{{channel_name}}', $channel_name, $email_body );
		$email_body = str_ireplace( '{{site_name}}', get_bloginfo( 'name' ), $email_body );
		// Parse any additional tokens.
		$email_body = Automator()->parse->text( $email_body, $recipe_id, $user_id, $args );
		// Parse any shortcodes.
		$email_body = do_shortcode( $email_body );
		// Prepare headers.
		$headers = array(
			'From: ' . $email_from,
			'Reply-To: ' . $reply_to,
			'Content-Type: text/html; charset=UTF-8',
		);
		$headers = apply_filters( 'automator_discord_invite_email_headers', $headers, $this );

		// Send the email.
		$mailed = wp_mail( $email_to, $subject, $email_body, $headers );

		if ( ! $mailed ) {
			throw new Exception( esc_html_x( 'Error sending email.', 'Discord', 'uncanny-automator' ) );
		}

		return true;
	}

Scroll to Top