Skip Navigation

[Resolved] CRED API Headers in notifications sent only if from is set

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 14 replies, has 3 voices.

Last updated by Luo Yang 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#559499

Hello,

I have a CRED form and a function that allows sending a notification email to the user '15' :

function header_to_z( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if ($formid==27 && $notification_number==0) {
         
        $myheaders = array();
        $user_id = '15';
        $currentuser = get_user_by('ID', $user_id);
        $myheaders = array( 'To: '.$currentuser->display_name.' <'.$currentuser->user_email.'>' );
        $headers = array_merge($headers, $myheaders);
    }
    return $headers;
}
add_filter('cred_mail_header', 'header_to_z', 10, 5);

The code works great if I specify another "to" in the notification fields, but if I let them empty, the notification is never sent.

I need the notification to be sent even if no field has been specified in backend.

Thank you.

#559622
Screen Shot 2017-08-14 at 12.37.50 PM.png

The email notification system requires at least one "to" address to be set in the wp-admin area, otherwise it will not be triggered. You should see a warning about this. See the attached screenshot.

So please provide at least one valid email address. If you do not want to send an email to that valid address, you should modify the headers in your headers_to_z function to remove the original To: address header.

#559625

Thank you.
you should modify the headers in your headers_to_z function to remove the original To: address header
Could you please help me on how to do this ?

#559680

Sure, first add an email address in the To: field. Then you must find out what headers are currently being passed into your hook. Add some error logs to find out:

function header_to_z( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if ($formid==27 && $notification_number==0) {
        error_log('before merge:');
        error_log(print_r($headers, true));
        $myheaders = array();
        $user_id = '15';
        $currentuser = get_user_by('ID', $user_id);
        $myheaders = array( 'To: '.$currentuser->display_name.' <'.$currentuser->user_email.'>' );
        $headers = array_merge($headers, $myheaders);
        error_log('after merge:');
        error_log(print_r($headers, true));
    }
    return $headers;
}
add_filter('cred_mail_header', 'header_to_z', 10, 5);

Trigger an email notification, and watch your log statements. You should be able to see all the headers before the merge, and all the headers after the merge. Before the merge, you can delete any unwanted headers from the $headers array using PHP unset or array_splice like you see here:
https://stackoverflow.com/questions/369602/delete-an-element-from-an-array

#559709

Thank you.
I have tried this with no result.
The error logs don't mention the email address I want to be deleted :

[14-Aug-2017 20:13:26 UTC] before merge:
[14-Aug-2017 20:13:26 UTC] Array
(
    [0] => Content-Type: text/html
    [1] => From: MB <MB>
)

[14-Aug-2017 20:13:26 UTC] after merge:
[14-Aug-2017 20:13:26 UTC] Array
(
    [0] => Content-Type: text/html
    [1] => From: MB <MB>
    [2] => To: ZB <ZB>
)

Still, I received the mail at ZB address AND at the address I manually set in backend, which I want to delete...

#559745

Oh, I see what you mean. I had not tested this and I assumed it would work, but now I see that you are right - "To:" is not part of the original $headers array. I'm going to ask our developers if I'm overlooking something obvious, because it seems like you should be able to control the headers with code like this. I'll let you know what I find out. Until then, you can choose "Send notification to a specific email address:" and put in a fake email address like test@test.com.

#559746

Thank you.

Until then, you can choose "Send notification to a specific email address:" and put in a fake email address like test@test.com.
That's already what I did but the problem is the email still appears in the recipients list. So I'll wait for a better solution. Thank you.

#559953

Okay I have some feedback for you. CRED mail notifications use the wp_mail function, and the $headers array in the cred_mail_header callback corresponds to the $headers array here:
https://developer.wordpress.org/reference/functions/wp_mail/#using-headers-to-set-from-cc-and-bcc-parameters
According to this documentation, the $to address is not something that can be modified in the $headers array. I have a few options for you:
1. I can file this as a feature request, and pass the information along to our developers for consideration in a future release of CRED.
2. You can use a standard site reply email address like "contact@yoursite.com" in the "to" field, so that a test email address isn't there. You can use the $headers array to supply CC and BCC addresses.
3. You can use a generic field in your CRED form to supply the "To:" address. Use a custom shortcode in this field to populate the correct email address before the form is submitted.

Let me know how you would like to proceed.

#559956

Thank you.

Since there are about 20 forms and many conditions for each one, the generic field would be a really heavy solution.

The best solution would be to allow sending the mail even if no recipient is selected. Or at least to be able to delete the email set manually thanks to a function.

In the meanwhile, I'll use a generic address like "info@mysite", but this really isn't a good solution neither.

Thank you.

#560024

Since there are about 20 forms and many conditions for each one, the generic field would be a really heavy solution.
It's the only solution that works right now. Otherwise I can submit a feature request. Which would you prefer?

#560061

I would prefer a feature request ; it seems to me that one should be able to set up a notification where headers may only be generated by functions.
Thank you.

#560089

Okay great, I am assigning this ticket to Luo, who is our CRED feature request master. He will follow up with you soon.

#560179

Dear Roman,

Thanks for the feedback, your feature request is in waiting for evaluation, but there isn't any ETA for it, I suggest you subscribe to our blog to get the updated news.
https://toolset.com/blog/

#560253

Thank you.
On another post, Beda showed me another hook "cred recipients" ; wouldn't this hook allow me to delete the email I was forced to add in backend ?
Thank you.

#560640

It's a new question, could you create a new thread for it, that will help other users to find the answers.