• Resolved puls13

    (@puls13)


    Hi,

    i’m playing around with this plugin. My use case would be to send mails with attachements to an given mail-adress. The adress comes from an ACF-field. In the documentation i found the following:

    function change_email_recipient() {
                            // Fetch email from options field
                            $email = get_field('mail_field');
                            return $mail;
                        }
    add_filter( 'af/form/email/recipient', 'change_email_recipient', 10, 0 );
                        add_filter( 'af/form/email/recipient/key=FORM_KEY', 'change_email_recipient', 10, 0 );
                        add_filter( 'af/form/email/recipient/id=ID', 'change_email_recipient', 10, 0 );

    For me, this looks like, what i need. But: I’t doesn’t work. I hard-coded a recipient adress, but it doesn’t send a mail to this adress.

    Could anybody help, please.

    Thanks,
    Stephan.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter puls13

    (@puls13)

    In addition, because maybe it’s important:

    after adding the filter, i call the form with
    do_shortcode('[advanced_form form="FORM_KEY"');

    Stephan.

    • This reply was modified 4 years, 7 months ago by puls13.
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Hi, Stephan!

    I see a few issues with the code:

    – The variable you declare is called $email but the variable returned is $mail. These should be the same.
    – Only one of the add_filter alternatives should be used. The most common one is add_filter( 'af/form/email/recipient/key=FORM_KEY', 'change_email_recipient', 10, 0 ); where you should change FORM_KEY to your actual form key.

    On top of that, I would also recommend using the function call instead of do_shortcode. Should look like this: advanced_form( 'FORM_KEY' );.

    Hope that helps!

    Thread Starter puls13

    (@puls13)

    Hi Fabian,

    thanks for your answer. But, it doesn’t helped. Sorry.

    • Is there any possibily to see, that the filter sends the mail-adress to the function, which sends the mail? Some debug-options?
    • Maybe the format of the mail is wrong? If i hard-coded it, i take a string. i also tested an array. Nothing happend.
    • And i have a problem with your idea to replace the do_shortcode() function. Because the advanced_form() prints out the form immediatly. But i need it in a return. But there is no get_advanced_form(), right?

      Thanks
      Stephan.

    Plugin Author fabianlindfors

    (@fabianlindfors)

    Could you send over the code you’re currently using including the code you tried with a hard-coded email address?

    You’re right, there is no built-in way to capture the output of the form. You could either use do_shortcode or use advanced_form like this:

    ob_start();
    advanced_form( '...' );
    $rendered_form = ob_get_clean();
    
    Thread Starter puls13

    (@puls13)

    Hi Fabian,

    here it is:

    /**
     * Return HTML (Text of Form)
     *
     * @param $method What should be printed?
     * @param $jobID The ID of the job
     * @param $company The ID of the company
     * @return string A HTML-Formated String
     */
    function PrintBewerbung($method, $jobID, $company) {
        $return = '';
        switch ($method) {
            case 'F': $return .= get_field('stelle_bewerben_freitext',$jobID);
                break;
            case 'U': setCompanyPersonMail($company); $return .= do_shortcode('[advanced_form form="form_5f1808c340797"]');
                break;
            default: $return .= get_field('stelle_bewerben_freitext',$jobID);
                break;
        }
     return $return;
    }
    
    /**
     * Get Mail from ACF
     *
     * @param $company
     * @return string
     */
    function setCompanyPersonMail($company) {
        // get email from acf-field
        //$mail = get_field('company_person_mail',$company);
        $mail = '[email protected]';
    add_filter('af/form/email/recipient/key=form_5f1808c340797', $mail, 10, 0 );
    }

    Thanks for your help.

    Stephan.

    Thread Starter puls13

    (@puls13)

    Hi @fabianlindfors

    sorry for beeing a little bit annoying…

    At the moment, the filter don’t work. But i need the filter, because without the forms don’t work for me.

    Could you please help?

    Thanks
    Stephan.

    Plugin Author fabianlindfors

    (@fabianlindfors)

    There are some problems with your code:

    1. The add_filter call is within the function meaning it will never get called. It should appear right after your function.
    2. There is no value returned from setCompanyPersonMail, you should return $mail at the end of the function.
    3. You’re using get_field which is an ACF function. To get a form value you should use af_get_field instead.
    4. There is no $company argument passed to the filter. You shouldn’t need one either way as af_get_field will work without a post passed to it.
    5. Your add_filter call has $mail as the callback argument. This should be the function to call, setCompanyPersonMail.

    All in all your filter should probably look more like this:

    function setCompanyPersonMail() {
        // $mail = af_get_field('company_person_mail');
        $mail = '[email protected]';
        return $mail;
    }
    add_filter('af/form/email/recipient/key=form_5f1808c340797', 'setCompanyPersonMail', 10, 0 );
    
    Thread Starter puls13

    (@puls13)

    Hi @fabianlindfors ,

    thanks for your answer, but…

    I take your code and insert it into my template:

    function filter_email_recipient( $recipient, $email, $form, $fields) {
    $recipient .= ', [email protected]';
    return $recipient;
    }
    
    add_filter('af/form/email/recipient/ID=1338', 'filter_email_recipient', 10, 4 );
    advanced_form('1338');

    Nothing.

    And: I also tested, if the filter works if i enter the email directly:
    add_filter('af/form/email/recipient/ID=1338', '[email protected]', 10, 4 );

    It also doesn’t work.

    ??

    Plugin Author fabianlindfors

    (@fabianlindfors)

    There are still some issues with your code:

    This af/form/email/recipient/ID=1338 is not the right way to write add the filter. If you want to use the form post ID it should be af/form/email/recipient/id=1338. I would recommend using the form key instead though: af/form/email/recipient/key=FORM_KEY where you’ll have to replace FORM_KEY with your actual form key.

    Also, is 1338 the ID of your form? Previously you used the form key, form_5f1808c340797, which is the recommended way.

    Thread Starter puls13

    (@puls13)

    Hi @fabianlindfors ,

    thanks for your answer.

    Yes, 1338 is the ID of the form with the key form_5f1808c340797. I tried it, because the other one don’t work. I tried every version you described on the documentation. no one works. I tried to enter the mail directly, without call the function. I changed the mail-address. I wrote a new form, with only one text-input. Also don’t work.

    But, maybe we talk about different thinks?
    What i want is: In my notification options i selected “custom recipient” and inserted an adress. This mail works fine!
    And now i want to send the mail not only to this adress, but to another adress, which depends on an specific ACF-Field from the specific post (every post have his own e-mail).

    Maybe i understand the filter wrong?

    Best,
    Stephan.

    Thread Starter puls13

    (@puls13)

    Me again,

    i found a solution: I inserted a mail-field to the form. I pre-filled this field with the af/field/prefill_value filter. Which is working, perfect! And i changed the recipient to this field. Now it works!

    Thanks for your help and time,
    Stephan.

    Plugin Author fabianlindfors

    (@fabianlindfors)

    Sorry about the late reply but I’m happy to hear you worked it out! Let me know if you need help with anything else ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘custom recipient filter not working’ is closed to new replies.