Home › Forums › General Issues › how to receive an email after sending the form
Hello everyone.
how to receive an email after sending the form here is what i tried
// Sending email after Front-end post submission
add_action('acf/save_post', 'yourdomain_save_post', 15);
function yourdomain_save_post($post_id) {
// Return if not a post
if( get_post_type($post_id) !== 'recette' ) { // here 'post' will be your post type
return;
}
// Return if editing in admin
if( is_admin() ) {
return;
}
// Vars
$post = get_post( $post_id );
$toEmail = get_bloginfo('admin_email');
$subject = $post->post_title;
$body = $post->post_content;
$from = '[email protected]';
wp_mail($toEmail, $subject, $body, $from );
}
<?php /*Template Name: User Submit*/;?>
<?php acf_form_head(); ?>
<?php get_header(); ?>
<div id="container" >
<div class="row">
<div class="col-sm-12">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- a supprimer si on enlève l'éditeur par défaut -->
<!------------>
<p> <?php the_field('Ingredients'); ?></p>
<p> <?php the_field('Cuisson'); ?></p>
<p> <?php the_field('Temps'); ?></p>
<p> <?php the_field('Preparation'); ?></p>
<p> <?php the_field('Difficulté'); ?></p>
<p> <?php the_field('photo'); ?></p>
<p> <?php the_field('date'); ?></p>
<?php $options = array('post_id' => 'new',
'field_groups' => array(4),
'post_title' => false,
'post_type' => 'recette',
'post_status' => 'draft',
//'updated_message' => 'Merci pour votre participation!Votre recette sera publiée prochainement',
'updated_message' => __("Recette publiée", 'acf'),
'return' => home_url('merci'),
//'%post_url%', // Redirect to new post url
//'submit_value' => 'Postez votre recette'
'submit_value' => 'Send'
);
acf_form($options);?>
<?php endwhile; ?>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php //get_footer(); ?>
Hi @flexi2202
This is what I use and it works ok:
$post = get_post( $post_id );
$title = wp_strip_all_tags(get_the_title($post->ID));
$to = "{$admin_email}";
$subject = "{$blog_title} :: {$title}";
$message = "Your message here";
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
// send email
wp_mail($to, $subject, $body, $headers );
Perhaps alter for your own needs and see if it works.
Hello
thanks for the code, but I still do not receive an email, here is my new code
// Sending email after Front-end post submission
add_action('acf/save_post', 'yourdomain_save_post', 15);
function yourdomain_save_post($post_id) {
// Return if not a post
if( get_post_type($post_id) !== 'recette' ) { // here 'post' will be your post type
return;
}
// Return if editing in admin
if( is_admin() ) {
return;
}
// Vars
$post = get_post( $post_id );
$title = wp_strip_all_tags(get_the_title($post->ID));
$to = "{$admin_email}";
$subject = "{$blog_title} :: {$title}";
$message = "Your message here";
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
// send email
wp_mail($to, $subject, $body, $headers );
}
It’s possible that my code example uses $admin_email and $blog_title which I declare elsewhere in my code.
You either need to declare them or set them to your own values and see if that works.
You also didn’t change the FROM email
Have you checked your spam filters?
Does your WordPress site currently send emails?
Have you checked your error logs?
thanks for your help, i just changed the code again, but i still don’t receive email, nothing in spam either. maybe I indicate in the wrong places my email to receive emails
// Sending email after Front-end post submission
add_action('acf/save_post', 'yourdomain_save_post', 15);
function yourdomain_save_post($post_id) {
// Return if not a post
if( get_post_type($post_id) !== 'recette' ) { // here 'post' will be your post type
return;
}
// Return if editing in admin
if( is_admin() ) {
return;
}
// Vars
$post = get_post( $post_id );
$title = wp_strip_all_tags(get_the_title($post->ID));
$to = "{'[email protected]'}";
$subject = "{$blog_title} :: {$title}";
$message = "Your message here";
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
// send email
wp_mail($to, $subject, $body, $headers );
}
Your WP site definitely sends emails?
Your code (which goes in the functions.php file) is still wrong. Try:
$to = "[email protected]";
$subject = "Form submission successful";
$message = "Your message here";
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
// send email
wp_mail($to, $subject, $body, $headers );
Ensure you change the [email protected] in the FROM field to a valid email account on your server
thank you for the correction but unfortunately I do not receive any mail, nor in spam
// Sending email after Front-end post submission
add_action('acf/save_post', 'yourdomain_save_post', 15);
function yourdomain_save_post($post_id) {
// Return if not a post
if( get_post_type($post_id) !== 'recette' ) { // here 'post' will be your post type
return;
}
// Return if editing in admin
if( is_admin() ) {
return;
}
// Vars
$post = get_post( $post_id );
$title = wp_strip_all_tags(get_the_title($post->ID));
$to = "[email protected]";
$subject = "Form submission successful";
$message = "Your message here";
$headers = array(
'Content-Type: text/html; charset=UTF-8',
#'Bcc:' . '[email protected]',
'From: '.get_option( 'blogname' ).' <[email protected]>'
);
// send email
wp_mail($to, $subject, $body, $headers );
}
Does your WP site send emails ok?
Maybe try this smtp plugin to check the site sends emails
Is the form on a custom post page for recette? If not, remove the first conditional
thanks for the help
Does your WP site send emails ok? yes
Is the form on a custom post page for the recipe? Yes
here is the form code
<?php /*Template Name: User Submit*/;?>
<?php acf_form_head(); ?>
<?php get_header(); ?>
<div id="container" >
<div class="row">
<div class="col-sm-12">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<!-- a supprimer si on enlève l'éditeur par défaut -->
<?php the_content(); ?>
<!------------>
<p> <?php the_field('Ingredients'); ?></p>
<p> <?php the_field('Preparation'); ?></p>
<p> <?php the_field('Cuisson'); ?></p>
<p> <?php the_field('Temps'); ?></p>
<p> <?php the_field('Difficulté'); ?></p>
<!-- <p> <?php the_field('image'); ?></p>-->
<p> <?php the_field('gallery'); ?></p>
<?php $options = array('post_id' => 'new',
'field_groups' => array(4),
'post_title' => false,
'post_type' => 'recette',
'post_status' => 'draft',
//'updated_message' => 'Merci pour votre participation!Votre recette sera publiée prochainement',
'return' => home_url('merci'),
//'submit_value' => 'Postez votre recette'
'submit_value' => 'Send'
);
acf_form($options);?>
<?php endwhile; ?>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Does your form work?
Looking at the contact form example
Temporarily change your form code to:
acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'new_post' => array(
'post_type' => 'recette',
'post_status' => 'draft'
),
'return' => home_url('merci'),
'submit_value' => 'Send'
));
See if that works
many thanks for the help
yes my current form works since the recipes arrive in the recipe post type
but it still does not work with your model
So I tried this model but it doesn’t work either
I am staying with hostinger
in the function.php file
add_action('acf/save_post', 'my_save_post');
function my_save_post( $post_id ) {
// bail early if not a contact_form post
if( get_post_type($post_id) !== 'contact_form' ) {
return;
}
// bail early if editing in admin
if( is_admin() ) {
return;
}
// vars
$post = get_post( $post_id );
// get custom fields (field group exists for content_form)
$name = get_field('name', $post_id);
$email = get_field('email', $post_id);
// email data
$to = '[email protected]';
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$subject = $post->post_title;
$body = $post->post_content;
// send email
wp_mail($to, $subject, $body, $headers );
}
and in a contact page
<?php /*Template Name: pagecontactt*/;
acf_form_head();
get_header();
?>
<div id="content">
<?php
acf_form(array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'new_post' => array(
'post_type' => 'contact_form',
'post_status' => 'publish'
),
'return' => home_url('merci'),
'submit_value' => 'Send'
));
?>
</div>
<?php get_footer(); ?>
the server’s mail function works, because I have a contact form on another site in php and I do receive an email after sending
The code above has the wrong post_type
It’s also taken from the example, like I sent you earlier but I’d adjusted it to your custom post.
Do you have any plugins blocking emails?
I know the code used works as it’s the same as I use on my sites.
What if you switch to a default theme and add the code, does it work?
Can you hardcode the values in this line:
$headers = 'From: ' . $name . ' <' . $email . '>' . "\r\n";
I have a feeling you can’t access custom fields at this stage, I think you have to access the info via the field key
I just corrected the post_type and I gave recipe
I wanted to do a simple test with just sending a simple form …
No I have no plugin that blocks emails since from my hostinger mailbox I made a diversion on my Hotmail box
And I do receive emails on my Hotmail …
Yes I’m convinced the code works, but it’s probably just a little detail
I will try again on a new basic theme
But that doesn’t work either
I will create a new subdomain and try with no plugin and I will get back to you
Thank you again for all
Here I just created a new subdomain I used the basic template
I have deactivated all the templates
I just installed ACF and CPT
I took the example of the site with just a contact form and it still does not work …
it’s still weird
That’s very odd!
Perhaps contact your host and see if they can see anything.
You definitely have nothing in the error logs?
yes that’s what I will do. but now I have another problem with my form, I am posting a new message. Thank you again for all your help
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.