• Dear SolidWP Support Team,

    I’m reaching out regarding the recent updates to the Solid Mail plugin. Previously, the “From” email address in emails sent from my forms was set to match the email address entered by users within the form itself. However, since the latest update, it seems that an additional “From” address (configured in the plugin’s settings) is now appended to these outgoing emails.

    I would like to know if there’s a way to revert this setting or customize it so that only the user-entered email from the form is displayed as the “From” address, without appending the plugin’s default address. Is there an option within the plugin’s settings, or could you recommend a PHP function or filter to adjust this behavior?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author vodanh1213

    (@vodanh1213)

    Hi,

    To customize the “From” email address and name for all outgoing emails, you can add the following code snippet to your theme’s functions.php file or in a site-specific plugin:

    add_action( 'phpmailer_init', function ( $phpmailer ) {
    $phpmailer->From = '[email protected]';
    $phpmailer->FromName = 'Your Name';
    }, 99999 );

    If you need any clarification or run into any issues while implementing this solution, please don’t hesitate to reach out again.

    Best Regards,

    Hoang

    Thread Starter gr1ns

    (@gr1ns)

    Hi Hoang,

    Thank you for the code snippet. However, I’m looking to dynamically set the “From” email address to match the email address that users enter in the form, rather than using a fixed address.

    Specifically, I want each email sent to have the “From” address populated with the user’s email from the form submission, so the recipient can see it as the sender’s address. Could you provide guidance on how to implement this?

    Thank you for your help and clarification.

    Plugin Support chandelierrr

    (@shanedelierrr)

    Hi @gr1ns,

    Apologies for the slow turnaround here!

    Our developer recommended this custom snippet to achieve the dynamic setting of the “From” email address based on user input:

    add_action('phpmailer_init', function($phpmailer) {
    $default_provider = $this->providers_repository->get_active_provider();

    if (!is_object($default_provider) || $default_provider->validation() !== true) {
    return;
    }

    // Set email sender details with REQUEST override
    $from_email = isset($_REQUEST['from_email']) ? sanitize_email($_REQUEST['from_email']) : $default_provider->get_from_email();
    $from_name = isset($_REQUEST['from_name']) ? sanitize_text_field($_REQUEST['from_name']) : $default_provider->get_from_name();

    // Configure PHPMailer
    $phpmailer->Mailer = 'smtp';
    $phpmailer->From = $from_email;
    $phpmailer->FromName = $from_name;
    $phpmailer->Sender = $from_email;
    $phpmailer->AddReplyTo($from_email, $from_name);

    // SMTP settings
    $phpmailer->Host = $default_provider->get_host();
    $phpmailer->SMTPSecure = $default_provider->get_secure();
    $phpmailer->Port = $default_provider->get_port();
    $phpmailer->SMTPAuth = $default_provider->is_authentication();
    $phpmailer->SMTPDebug = SMTP::DEBUG_LOWLEVEL;

    // Authentication if required
    if ($phpmailer->SMTPAuth) {
    $phpmailer->Username = $default_provider->get_username();
    $phpmailer->Password = $default_provider->get_password();
    }
    }, 99999);

    Can you please try this out on your theme/child theme’s functions.php file or MU-plugin and see if it works?

    Hope this helps!

    Thread Starter gr1ns

    (@gr1ns)

    Hi,

    Thank you for your response and the provided code snippet.

    I’ve tested the provided solution in my theme’s functions.php file, but I encountered an error related to the usage of $this in the context of the phpmailer_init action. Since the code snippet is a standalone function and not part of a class, $this is unavailable, which results in the following error:

    Uncaught Error: Using $this when not in object context in /path-to-theme/functions.php:55
    Stack trace:
    #0 /wp-includes/class-wp-hook.php(324): {closure}()
    #1 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
    #2 /wp-includes/plugin.php(565): WP_Hook->do_action()
    #3 /wp-includes/pluggable.php(540): do_action_ref_array()
    #4 /wp-content/plugins/ninja-forms/includes/Actions/Email.php(108): wp_mail()

    This error occurs because $this->providers_repository cannot be used unless the function is part of a class.

    Can you confirm if the providers_repository should be passed as a global object or instantiated directly in the snippet? Alternatively, should the snippet be integrated into a class with the $this context?

    Plugin Author vodanh1213

    (@vodanh1213)

    Hi @gr1ns

    I apologize for the confusion in my previous response. You’re absolutely right about the $this context issue. Here’s the corrected code snippet that properly instantiates the ProvidersRepository without requiring a class context:

    add_action('phpmailer_init', function($phpmailer) {
    $repository = new \SolidWP\Mail\Repository\ProvidersRepository();
    $default_provider = $repository->get_active_provider();

    if (!is_object($default_provider) || $default_provider->validation() !== true) {
    return;
    }

    // Set email sender details with REQUEST override
    $from_email = isset($_REQUEST['from_email']) ? sanitize_email($_REQUEST['from_email']) : $default_provider->get_from_email();
    $from_name = isset($_REQUEST['from_name']) ? sanitize_text_field($_REQUEST['from_name']) : $default_provider->get_from_name();

    // Configure PHPMailer
    $phpmailer->Mailer = 'smtp';
    $phpmailer->From = $from_email;
    $phpmailer->FromName = $from_name;
    $phpmailer->Sender = $from_email;
    $phpmailer->AddReplyTo($from_email, $from_name);

    // SMTP settings
    $phpmailer->Host = $default_provider->get_host();
    $phpmailer->SMTPSecure = $default_provider->get_secure();
    $phpmailer->Port = $default_provider->get_port();
    $phpmailer->SMTPAuth = $default_provider->is_authentication();
    $phpmailer->SMTPDebug = SMTP::DEBUG_LOWLEVEL;

    // Authentication if required
    if ($phpmailer->SMTPAuth) {
    $phpmailer->Username = $default_provider->get_username();
    $phpmailer->Password = $default_provider->get_password();
    }
    }, 99999);

    This version directly instantiates the repository object and doesn’t rely on $this. The functionality remains the same, but it’s now structured to work within WordPress’s function scope. Please let me know if you encounter any issues with this implementation.

    Best Regards,
    Hoang

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.