Skip Navigation

[Resolved] Use e-mail field as mailto in contact form

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 13 replies, has 2 voices.

Last updated by Marcel 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2581175

I'm building a directory of therapists. In the database, there's also an e-mail address field which stores the e-mail address of each therapist. However, that won't be published in the front-end.

Instead, in the single post template, there should be a contact form, which sends and e-mail to the therapist that is currently being viewed.

For that, I somehow need to get the e-mail address from the database and set it as the mailto address in the notification settings.

I already built a contact form post type and set a relation between that and the therapist post type.

If that can't work, I also have a Fluent Forms pro licence. The contact form could also be on another page and could capture the e-mail address from the URL, but then the e-mail address should of course be added as a paramater to the URL. So something like this might work too: https://toolset.com/forums/topic/how-to-display-dynamic-content-on-pages-based-on-a-selection-on-the-landing-page/

#2581413

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to send email to the email to the e-mail address field of therapist when you submit the contact form? If yes:
- its possible but there is few steps you will have to follow.

If you can share problem URL where I can see the contact form and admin access details. I will review your current setup and guide you with the next steps or possible solution or workaround.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2581453

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see on your post relationship page you have created the many to many post relationship between the post type "Verwijslijst" and "Contactformulier".

It should be one to many post relationship - right? as One Verwijslijst can have many contact request submitted.

#2581465

Probably 🙂 I always set relationships as many to many, seems the most flexible 🙂

#2581471

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - you should not as every post relationship have their own advantage and dis-advantage and pros and crons and to access related posts for every relationship is different. So, I suggest you first create a proper one to many post relationship and then we can go with next steps.

I see its staging site and if you do not added any related posts yet you should delete the existing post relationship and then create a new one to many post relationship.

#2581475

Ok, I did now.

#2582257

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It seems you will have to configure the SMTP for sending emails so I suggest you should install any of the popular SMTP plugin that suits your requirement and try to submit the form and check if you able to get the email.

In addition to that, I would also suggest you should configure your email submit and body as per your requirement with your form:
=> hidden link

More info:
- https://toolset.com/course-lesson/send-notifications-when-someone-submits-the-form/

I've added the following code to "Custom Code" Section offered by Toolset:
=> hidden link

add_action('cred_save_data', 'func_set_parent_dynaically',10,2);
function func_set_parent_dynaically($post_id, $form_data){
 
    // if a specific form
    if ($form_data['id'] == 2077 ){
        if (isset($_POST['parent_post_id'])){
            $parent_id = $_POST['parent_post_id'];
            $parent = toolset_connect_posts( 'verwijslijst-contactformulier', $parent_id, $post_id );
            
        }
    }
}

add_filter('cred_notification_recipients', 'func_send_notification_to_parent_email_field', 11, 4);
function func_send_notification_to_parent_email_field($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
if (  $form_id == 2077 ) {
  
  $parent_id = toolset_get_related_post($post_id,'verwijslijst-contactformulier');
  
        // Add email recipient
        
        $mailto = get_post_meta($parent_id, 'wpcf-email', true);
        $recipients[] = array(
                          'to' => 'to',
                          'address' => $mailto,
                          'name' => '',
                          'lastname' => '');
}
  
return $recipients;
}

I've also installed the mail log plugin and I can see the email is not send but the code successfully fetch the correct parent email address and its trying to send the email but it seems you will have to setup SMTP to send emails. Here is the email log where I can see it's trying to send emails to correct email address:
=> hidden link

#2582445

Hi Minesh,

Thank you very much. I'm afraid I messed something up though 🙁

This worked, but then I made some changes to the e-mail notification (content, from name, subject) and now it doesn't send the form to the therapist anymore...

#2582887

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the email notification body content as given under:

Naam: [types field='contact-naam' item="%%POST_ID%%"][/types]
E-mail adres: [types field='contact-email' item="%%POST_ID%%"][/types]
Telefoonnummer: [types field='contact-telefoon' item="%%POST_ID%%"][/types]
Bericht:
[types field='contact-bericht' item="%%POST_ID%%"][/types]

Can you please confirm it works as expected now.

#2582961

No, sorry. E-mail is still sent only to the person that fills in the contact form, not to the therapist.

#2582965

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please tell me with what post you are testing and submit the contact form?

#2583111

I am testing this profile: hidden link, but you can make your own test profile with your own e-mail address if you want.

However, the e-mail log plugin you've installed shows it's only sent to the person that fills in the form, not to the therapist.

#2583431

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code added with "cred_notification_recipients" hook with "Custom Code" section as given under to send the email to parent post custom email field.

add_filter('cred_notification_recipients', 'func_send_notification_to_parent_email_field', 11, 4);
function func_send_notification_to_parent_email_field($recipients, $notification, $form_id, $post_id) {
// Check notification name matches target notification
if (  $form_id == 2077 ) {
  

   $parent_id = $_POST['parent_post_id'];
  
        // Add email recipient
        
        $mailto = get_post_meta($parent_id, 'wpcf-email', true);
        $recipients[] = array(
                          'to' => 'to',
                          'address' => $mailto,
                          'name' => '',
                          'lastname' => '');
}
  
return $recipients;
}

I can see now in the email log it's sending email to both emails:
- hidden link

#2583443

My issue is resolved now. Thank you so much, again, couldn't have done this without you!