0

I have a CPT and inside the CPT I have a contact form. The contact form directly sends to WordPress dashboard, Now I'm trying to implement when a user submits the contact form they will receive a "Thanks for contacting US" message in their Email address(User fill email in the form) I'm new in WordPress & PHP. I'm using Ajax form validation & pass data. $email = sanitize_email($_POST['email']);. This variable data capture from user email address an after send their email address thanks email. Have any help greatly appreciated.

EDIT

I can Email to my WordPress site hosted email id eg: user@example.com another Gmail,hotmail,live like email proveders not working. I checked a small plugin check email it is not a smtp but that can send email to any email providers. I don't know what wrong with with me, I tried Everything I can.

public function submit_testimonial()
    {

        if (! DOING_AJAX || ! check_ajax_referer('testimonial-nonce', 'nonce') ) {
            return $this->return_json('error');
        }

        $name = sanitize_text_field($_POST['name']);
        $package = sanitize_text_field($_POST['package']);
        $phone = sanitize_text_field($_POST['phone']);
        $date = sanitize_text_field($_POST['date']);
        $email = sanitize_email($_POST['email']);
        $message = sanitize_textarea_field($_POST['message']);


        $data = array(

            'name' => $name,
            'package' => $package,
            'phone' => $phone,
            'email' => $email,
            'date' => $date,
            'date' => $message,
            'approved' => 0,
            'featured' => 0,
        );

        $args = array(
            'post_title' => $name,
            'post_content' => $message,
            'post_author' => 1,
            'post_status' => 'publish',
            'post_type' => 'testimonial',
            'meta_input' => array(
                '_zon_testimonial_key' => $data
            )
        );

        $postID = wp_insert_post( $args );


        if ($postID) {
            $headers = "MIME-Version: 1.0\r\n" .
            "From: " . $current_user->user_email . "\r\n" .
            "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\r\n";
            $to = $email;
            $title = "Test email from";
            $body = "Hello " .  $name . " Your ZonPackage " . $package . " Booking Confirmed ." ;
            $subject ="Zon Package Booking";
            wp_mail( $to, $subject, $body, $headers );
        }




        if ($postID) {
            return $this->return_json('success');

        }

    }

    public function return_json( $status ) {
        $return = array(
            'status' => $status
            );
            wp_send_json($return);

    }
  • It is hard to help without some more details. I understand the wp_insert_post($args) us run and you have the post saved in the database? Just the email is not being sent? – Greg Winiarski Dec 12 '18 at 10:48
  • Email not sending. post saved in database but not working – Noufal Binu Dec 12 '18 at 11:10
  • updated answer below to address issues in your revised question – butlerblog Dec 15 '18 at 13:44
0

I revised your code to address some issues, but there is the possibility that the issue is not your code.

First, you are returning too early. Your original code returns on success before you send the email. I moved this to the end so that it sets a value (success or error) at the end based on whether you have a post ID or not, then returns. That way you get your mail functions as well.

It's unlikely that you need both wp_mail() and mail(). If you're doing that because you think wp_mail() is not working, that's really not going to help. wp_mail() uses the phpMailer library If you haven't done any customization, phpMailer is going to use mail() - so these are ultimately the same unless this was run without WP in which case you'd get errors because functions like wp_insert_post() would be undefined.

Also, the documentation for wp_send_json() indicates that you do not need wp_die() as it handles that for you in the function.

Here's your code with those changes:

public function submit_contact() {

    if (! DOING_AJAX || ! check_ajax_referer('contact-nonce', 'nonce') ) {
        return $this->return_json('error');
    }

    $name    = sanitize_text_field( $_POST['name'] );
    $phone   = sanitize_text_field( $_POST['phone'] );
    $email   = sanitize_email( $_POST['email'] );
    $message = sanitize_textarea_field( $_POST['message'] );

    $data = array(

        'name'     => $name,
        'package'  => $package,
        'phone'    => $phone,
        'email'    => $email,
        'date'     => $date,
        'date'     => $message,
        'approved' => 0,
        'featured' => 0,
    );

    $args = array(
        'post_title'   => $name,
        'post_content' => $message,
        'post_author'  => 1,
        'post_status'  => 'publish',
        'post_type'    => 'contact',
        'meta_input'   => array(
            '_zon_contact_key' => $data
        )
    );

    $postID = wp_insert_post( $args );

    if ( $postID ) {
        wp_mail( 'digitcrop@gmail.com', 'The subject', 'The message' );
    }

    $return_val = ( $postID ) ? 'success' : 'error';
    return $return_val;
}

public function return_json( $status ) {

    $return = array(
        'status' => $status
    );

    wp_send_json($return);

}

If you're still not getting emails, I would do some troubleshooting on the email side of things. If the post is inserted and no email, your code is working and it's likely an issue with the mail send, not your code.

If you're on shared hosting, you need to check into rules for sending email via scripts. WP will send from "wordpress@yourdomain.com" by default. Some hosts require the "from" address be a real email. So check into that. Also, you've got this code set up for testing with a simple subject and message body. There is the possibility that the sending host is rejecting because it thinks it's spam based on the body text being so short.

Note: I updated and clarified this section

My suspicion is that if you put in an email logger, you'll may find that the wp_mail() is running (and logging) as if the mail is sent, but that you still don't receive it. And if that's the case, it's because your host is rejecting the send.

It's easy to avoid all of these things by setting up to send through SMTP. That would avoid most issues. There are a lot of ways to do that, using a plugin or using code. The Internet abounds with examples - this is just one of them.

If you can't use SMTP, then you may need to troubleshoot other possible email issues with WP's default headers and things like that. Here's an article with more information on that kind of troubleshooting.

UPDATE #2 - Address Email Not Being Received

Based on your update, your issue is email not being received. IMO, that's likely due to the way you're setting the headers. As a result, your email is getting rejected.

As I stated in the original answer, you're better off setting up to send through an SMTP account. That will help you avoid a lot of potential problems.

Regardless of whether you do that or not, you need to fix your header issue. Don't mess with headers unless you absolutely need to. I'd recommend you take that out completely.

If you must leave it in, you need to fix the following:

  1. Unless you're changing something, don't declare it.
  2. $current_user is undefined.
  3. Pass headers as array values.
  4. "From" value in the header is malformed.

Unless you're changing something, don't declare it. wp_mail() sets any defaults that are not declared. I would suggest you take out the mime type declaration and the content type for sure.

$current_user is undefined in the code that you posted. Add the following to make sure it's defined:

$current_user = wp_get_current_user();

Pass headers as array values. wp_mail() takes either a string or array, but if it's a string, it gets exploded into an array, so you may as well start with an array.

"From" value in the header is malformed the way you have it. You must also have a "from" name in addition to the address. It needs to be in the following format:

"From: The From Name <example@email.com>"

If you don't have the "name", substitute the email address:

$headers[] = 'From: ' . $current_user->user_email . '<' . $current_user->user_email . '>';

So with those things addressed, here's what I recommend:

    if ($postID) {
        $current_user = wp_get_current_user();
        $headers[] = 'From: ' . $current_user->user_email . '<' . $current_user->user_email . '>';
        $to = $email;
        $title = "Test email from";
        $body = "Hello " .  $name . " Your ZonPackage " . $package . " Booking Confirmed ." ;
        $subject ="Zon Package Booking";
        wp_mail( $to, $subject, $body, $headers );
    }

Keep in mind that may not solve your problem entirely. If your "from" address doesn't have a domain that matches the sending domain, you may still look "spammy" and get rejected. Also, be sure to check your spam folder to see if your messages are being received but are flagged as spam.

  • No Luck, I checked with "email log plugin" it shows the messege execute but still cannot send to gmail. – Noufal Binu Dec 12 '18 at 13:03
  • As I said in my answer, a logger is likely going to tell you that it was "successfully" sent - that's because wp_mail() just returns true or false based on handing the message off. It doesn't actually mean that the message was sent by the server. So your issue is a mail issue (outside WordPress) that you will need to track down (see the last section of the answer above which contains help for this). I recommend starting with setting up SMTP - it solves most problems, is more robust, and makes you look "legit." – butlerblog Dec 12 '18 at 13:52
0

If the post is saved in the database then your mail() nor wp_mail() functions are not run because the function ends here

if ($postID) {
    return $this->return_json('success');
}

Cut these three lines of code and paste them before return $this->return_json('error');

Additionally, you do not need the wp_die() function as the wp_send_json() will run exit; for you.

  • Not Working I did What you said, what is the wrong? – Noufal Binu Dec 12 '18 at 12:21
  • If you made the modifications as explained and still do not get a message in an inbox then try installing a plugin like Email Log wordpress.org/plugins/email-log maybe the email is sent but not delivered. – Greg Winiarski Dec 12 '18 at 12:24
  • I'm already checked that. I doubt on this line wp_mail( 'digitcrop@gmail.com', 'The subject', 'The message'); are you sure this small line of code execute to send email? – Noufal Binu Dec 12 '18 at 12:36
  • i never used wp_mail function – Noufal Binu Dec 12 '18 at 12:38
  • I checked with "email log plugin" it shows the messege execute but still cannot send to gmail. – Noufal Binu Dec 12 '18 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.