Skip Navigation

[Resolved] Sending email after submitting a form to email address in parent post

This support ticket is created 4 years, 10 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Nigel 4 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1445741

I am trying to:
To send email after submitting a form to email address in parent post using the code
below but it is not working.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
 if ( $form_data['id']==1534 ){
   $id_contact = get_post_meta($post_id, '_wpcf_belongs_tenant-contact_id', true);
   $to = get_post_meta($id_contact, "wpcf-contact-email", true);
   $from = "property<noreply@gutech.edu.om>";
   $subject = get_the_title($post_id);
   $mailbody = get_post_meta($post_id, "wpcf-communication_description", true);

   $status= wp_mail($to, $subject, $mailbody, $from);	
}
}
#1445825

When I specify the $to email to be my email it works fine but when I replace it with
$to = get_post_meta($id_contact, "wpcf-contact-email", true);
it can not receive the email.

#1445927

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The parent post ID is not stored in post meta, not since Types 3.0.

If you have created a one-to-many relationship then you need to use the Relationships API to retrieve the parent post ID.

Specifically, you would use the function toolset_get_related_post (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post), like so:

$parent_id = toolset_get_related_post( $post_id, "relationship-slug" );

Can you try that in your code after editing for the correct relationship slug?

#1446041

I have edited the code but still I do not receive any email.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
 if ( $form_data['id']==1534 ){
   $id_contact = toolset_get_related_post( $post_id, "tenant_contact-communicaton" );
   $to = get_post_meta($id_contact, "wpcf-contact-email", true);
   $from = "property<noreply@gutech.edu.om>";
   $subject = get_the_title($post_id);
   $mailbody = get_post_meta($post_id, "wpcf-communication_description", true);
   $status= wp_mail($to, $subject, $mailbody, $from);	
}
}
#1446073

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Dump the variables to your debug.log and that should help determine where it fails.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

Sprinkle some error_log calls in your code, e.g.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
 if ( $form_data['id']==1534 ){
   $id_contact = toolset_get_related_post( $post_id, "tenant_contact-communicaton" );
error_log(print_r($id_contact, true));
   $to = get_post_meta($id_contact, "wpcf-contact-email", true);
error_log(print_r($to, true));
   $from = "property<noreply@gutech.edu.om>";
   $subject = get_the_title($post_id);
   $mailbody = get_post_meta($post_id, "wpcf-communication_description", true);
   $status= wp_mail($to, $subject, $mailbody, $from);   
}
}