Skip Navigation

[Resolved] How to set the Sender email to the email of an custom generic email field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED form that captures name and email address using generic fields. I would like to set the "From" email address using these generic field values.

Solution: There is no easy way to do this in the wp-admin area, so just leave the sender information fields empty. To manipulate the sender's email address, you must use the CRED API to modify the email headers:

function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if( $formid == 12345 ) {
      $sender_email = $_REQUEST['sender-email-field-slug'];
      $sender_name = $_REQUEST['sender-name-field-slug'];
      $myheaders = array( "From: " . $sender_name . " <" . $sender_email . ">" );
      return array_merge($headers, $myheaders);
    }
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

Replace 12345 with the ID of your form, and replace sender-email-field-slug and sender-name-field-slug with the proper slugs from your generic fields.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

This support ticket is created 6 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by leilaG 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#652492

2. How to set the Sender email to the email of an custom generic email field
It's similar to the process described here: https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/#send-email-notification-to-a-user-specified-in-a-generic-field
Add "persist":1 to the generic field configuration object like this:

[cred_generic_field field='genericmail' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"",
"persist":1
}
[/cred_generic_field]
You should then be able to find the generic field listed in the email source fields. You may have to click the "refresh" icon next to the input area to update the options shown here. If you are still unable to find the generic email field listed, submit the form once on the front-end with some value set in the genericmail field. You can use the "default" configuration to do that if necessary. Then refresh the CRED form editor page in wp-admin and try again.

When I add "persist":1 the email field input box is missing from the front end form.

#652864

Hi, can you check to be sure the configuration lines are all separated by a comma? For example, this is an error (no comma after the validate_format line):

[cred_generic_field field='genericmail' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0
"default":"",
"persist":1
}
[/cred_generic_field]

If that does not resolve the problem, please copy and paste the full form code for me to review.

#655787

That worked the email input box is back.

I cannot see any email source fields in the Notification set up. I would like the Set from email and name to be the generic fields that I have added to the form. hidden link

Set From details:
Email (leave blank for default):
Name (leave blank for default):

#667658

You may have to click the "refresh" icon next to the input area to update the options shown there. If you are still unable to find the generic email field listed, submit the form once on the front-end with some value set in the generic email field. You can use the "default" configuration to do that if necessary. Then refresh the CRED form editor page in wp-admin and try again.

#680304
from-email.PNG

I have submitted the form but still can not see any options or a refresh button (Image attached)

Could you please share a screen shot to make sure I am looking in the right place.

Thanks.

#680449

Oh okay, I was mistaken and thought you meant the recipient and not the sender - sorry for the confusion. There is no easy way to do this in the wp-admin area, so just leave the sender information fields empty. To manipulate the sender's email address, you must use the CRED API to modify the email headers:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_mail_header

function customise_cred_notifications( $headers, $formid, $postid, $notification_name, $notification_number ) {
    if( $formid == 12345 ) {
      $sender_email = $_REQUEST['sender-email-field-slug'];
      $sender_name = $_REQUEST['sender-name-field-slug'];
      $myheaders = array( "From: " . $sender_name . " <" . $sender_email . ">" );
      return array_merge($headers, $myheaders);
    }
    return $headers;
}
add_filter('cred_mail_header', 'customise_cred_notifications', 10, 5);

Replace 12345 with the ID of your form, and replace sender-email-field-slug and sender-name-field-slug with the proper slugs from your generic fields.

#686182

Works perfectly!! Thank you