自定义插件不发送电子邮件

时间:2014-02-25 12:48:43

标签: php wordpress plugins

我正在尝试开发一个简单的联系表单,但表单不发送任何电子邮件,我不知道如何调试以及如何解决此问题。

这是我的插件代码:

<?php
/*
Plugin Name: Contact Form
Plugin URI: http://www.exmple.com/
Description: This plugin allows captcha for a contact form.
Version: 1.0

Author URI: http://www.example.com/
License: GPL2
*/
?>
<?php
function contact_shortcode_func( $atts, $content="" ) {

$content.="<div id='contact_form_errorloc' class='err'>".plugins_url()."</div>
<form method='POST' name='contact_form' 
action='".str_replace( '%7E', '~', $_SERVER['REQUEST_URI'])."'> 
<p>
<label for='name'>Name: </label><br>
<input type='text' name='name1' value=''>
</p>
<p>
<label for='email'>Email: </label><br>
<input type='text' name='email1' value=''>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name='message1' rows='8' cols='30'></textarea>
</p>

<input type='submit' value='Submit' name='submit'>
</form>";

     return $content;

     //echo $your_email ='xxxxx@gmail.com';// <<=== update to your email address

if(isset($_POST['submit']))
{
$your_email ='xxxxx@gmail.com';     //here i am using valid email id    
$name = $_POST['name1'];
    $visitor_email = $_POST['email1'];
    $user_message = $_POST['message1'];
    ///------------Do Validations-------------

        //send the email
        $to = $your_email;
        $subject="New form submission";
        $from = $your_email;
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

        $body = "A user  $name submitted the contact form:\n".
        "Name: $name\n".
        "Email: $visitor_email \n".
        "Message: \n ".
        "$user_message\n".
        "IP: $ip\n";    

        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $visitor_email \r\n";

        wp_mail($to, $subject, $body,$headers);

        //header('Location: thank-you.html');
}

}
add_shortcode( 'cfwc', 'contact_shortcode_func' );

所以任何想法如何解决它。

2 个答案:

答案 0 :(得分:2)

您的邮件功能是在函数返回后写的。这是永远不会达到的代码。

return $content;之后的所有内容都不会被执行。将return移至功能结束处。

答案 1 :(得分:0)

以下是完整的工作表单,您可以从此代码中获取帮助,这可以包含在html表单中作为帖子。

<?php

if(isset($_POST['email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "user@gmail.com";

$email_subject = "Your email subject line";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['comments']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

 if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

 }



$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Telephone: ".clean_string($telephone)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





 // create email headers

 $headers = 'From: '.$email_from."\r\n".

 'Reply-To: '.$email_from."\r\n" .

 'X-Mailer: PHP/' . phpversion();

 @mail($email_to, $email_subject, $email_message, $headers);  

 ?>



 <!-- include your own success html here -->



 <h4 align="center">Thank you for contacting us. We will revert you back.</h4>



<?php

}

?>