0

I am using the below contact from code in the WordPress by creating shortcode. When i submit the form it shows success message but didn't get any email in inbox or spam. Can anybody please help.

function contactform_func( $atts ) {
$atts = shortcode_atts( array(
    'to_email' => '',
    'title' => 'Contact enquiry - '.get_bloginfo('url'),
), $atts );

$cform = "<div class=\"main-form-area\" id=\"contactform_main\">";

$cerr = array();
if( isset($_POST['c_submit']) && $_POST['c_submit']=='Submit' ){
    $name           = trim( $_POST['c_name'] );
    $email          = trim( $_POST['c_email'] );
    $phone          = trim( $_POST['c_phone'] );
    $website        = trim( $_POST['c_website'] );
    $comments       = trim( $_POST['c_comments'] );
    $captcha        = trim( $_POST['c_captcha'] );
    $captcha_cnf    = trim( $_POST['c_captcha_confirm'] );

    if( !$name )
        $cerr['name'] = 'Please enter your name.';
    if( ! filter_var($email, FILTER_VALIDATE_EMAIL) ) 
        $cerr['email'] = 'Please enter a valid email.';
    if( !$phone )
        $cerr['phone'] = 'Please enter your phone number.';
    if( !$comments )
        $cerr['comments'] = 'Please enter your question / comments.';
    if( !$captcha || (md5($captcha) != $captcha_cnf) )
        $cerr['captcha'] = 'Please enter the correct answer.';

    if( count($cerr) == 0 ){
        $subject = $atts['title'];
        $headers = "From: ".$name." <" . strip_tags($email) . ">\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

        $message = '<html><body>
                        <table>
                            <tr><td>Name: </td><td>'.$name.'</td></tr>
                            <tr><td>Email: </td><td>'.$email.'</td></tr>
                            <tr><td>Phone: </td><td>'.$phone.'</td></tr>
                            <tr><td>Website: </td><td>'.$website.'</td></tr>
                            <tr><td>Comments: </td><td>'.$comments.'</td></tr>
                        </table>
                    </body>
                </html>';
        mail( $atts['to_email'], $subject, $message, $headers);
        $cform .= '<div class="success_msg">Thank you! A representative will get back to you very shortly.</div>';
        unset( $name, $email, $phone, $website, $comments, $captcha );
    }else{
        $cform .= '<div class="error_msg">';
        $cform .= implode('<br />',$cerr);
        $cform .= '</div>';
    }
}

$capNum1    = rand(1,4);
$capNum2    = rand(1,5);
$capSum     = $capNum1 + $capNum2;
$sumStr     = $capNum1." + ".$capNum2 ." = ";

$cform .= "<form name=\"contactform\" action=\"#contactform_main\" method=\"post\">
        <p class=\"left\"><input type=\"text\" name=\"c_name\" value=\"". ( ( empty($name) == false ) ? $name : "" ) ."\" placeholder=\"Name\" /></p>
        <p class=\"right\"><input type=\"email\" name=\"c_email\" value=\"". ( ( empty($email) == false ) ? $email : "" ) ."\" placeholder=\"Email\" /></p><div class=\"clear\"></div>
        <p class=\"left\"><input type=\"tel\" name=\"c_phone\" value=\"". ( ( empty($phone) == false ) ? $phone : "" ) ."\" placeholder=\"Phone\" /></p>
        <p class=\"right\"><input type=\"url\" name=\"c_website\" value=\"". ( ( empty($website) == false ) ? $website : "" ) ."\" placeholder=\"Website with prefix http://\" /></p><div class=\"clear\"></div>
        <p><textarea name=\"c_comments\" placeholder=\"Message\">". ( ( empty($comments) == false ) ? $comments : "" ) ."</textarea></p>";
$cform .= "<br /><p class=\"left\">$sumStr<input style=\"width:200px;\" type=\"text\" placeholder=\"Captcha\" value=\"". ( ( empty($captcha) == false ) ? $captcha : "" ) ."\" name=\"c_captcha\" /><input type=\"hidden\" name=\"c_captcha_confirm\" value=\"". md5($capSum)."\"></p><div class=\"clear\"></div>";
$cform .= "<p class=\"sub\"><input type=\"submit\" name=\"c_submit\" value=\"Submit\" /></p>
    </form>
</div>";
return $cform;} add_shortcode( 'contactform', 'contactform_func' );

2 Answers 2

0

Copied from my Answer for "An old plugin self-made stoped sending mail":

Many E-Mail-Hosters changed their rules to prevent spam. Often times, E-Mails that have one domain name in their "from"-Tag and come from a server with a different domain are rejected. So my guess is, maybe the emails are still sent, but not received.

You can check this by changing this line:

$headers = "From: ".$name." <" . strip_tags($email) . ">\r\n";

to something like this:

$my_domain = $_SERVER['HTTP_HOST'];
$headers = 'From: orders@'.$my_domain."\r\n".'Reply-To: '.$senderEmail."\r\n";

and check if the e-mails are sent and received correctly now. (By setting the Reply-To Header, the correct Address is inserted when clicking on "Reply" in the Mail Client.)

1
  • I have replace the line as you suggest but it still shows the success message on frontend but email not received :( Sep 8, 2017 at 13:05
0

Just to make sure the mail() function is successful, try change this part:

$result = mail( $atts['to_email'], $subject, $message, $headers);
if($result){
    $cform .= '<div class="success_msg">Thank you! A representative will get back to you very shortly.</div>';
}

Then if you still see the success message, you know it is something else.

22
  • I did the changes now after fill up the form it does not shows the success message. Does that mean mail function is not working properly ? Sep 8, 2017 at 13:31
  • Yes that is correct. Before the $result line, can you echo the contents of $atts['to_email']? Just to make sure that you are passing the to value through your shortcode. Sep 8, 2017 at 14:01
  • How are you writing the shortcode when you are using it on the site? Sep 8, 2017 at 14:02
  • I am using the shortcode like this [contactform to_email="[email protected]" title="Contact Form"] Also i have notice that this problem is happen with the latest version of WordPress. Before that it was working fine. Can you please provide me any solutions. Thanks for your time :) Sep 11, 2017 at 9:56
  • Right before the mail function add this code: echo $atts['to_email']; exit(); This way we can make sure we know what is being given to the to in the mail function. Sep 11, 2017 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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