从contact.html提交后,sendmail.php变为空白

时间:2015-06-08 08:59:38

标签: php html wordpress

我有来自我正在处理的contact.html的联系表单,但我似乎无法使其正常运行。每当我点击按钮"发送消息"它会转到空白的sendmail.php页面。它并没有显示任何错误。如果我填写它并不重要,它将具有相同的结果。

P / s:我仍然是php的noobie,这个页面来自模板,所以我真的不敢改变太多。 (除非你有解决办法lol)

联系表格:

<form class="contact-form row" method="POST" action="sendmail.php">

                        <div class="usermessagea"></div>
                        <fieldset>

                            <ul>

                                <li class="text-field with-icon span3">

                                    <label for="name-contact-form">
                                        <span class="mainlabel">Name</span>
                                    </label>

                                    <div class="input-prepend"><span class="add-on">
                                        <img src="images/contact-form/user.png" alt="" title="" /></span><input type="text" name="yit_contact[name]" id="name-contact-form" class="with-icon required" value="" />
                                    </div>

                                    <div class="msg-error"></div>

                                    <div class="clear"></div>
                                </li>

                                <li class="text-field with-icon span3">

                                    <label for="email-contact-form">
                                        <span class="mainlabel">Email</span>
                                    </label>

                                    <div class="input-prepend">
                                        <span class="add-on">
                                            <img src="images/contact-form/envelope.png" alt="" title="" />
                                        </span>
                                        <input type="text" name="yit_contact[email]" id="email-contact-form" class="with-icon required email-validate" value="" />
                                    </div>

                                    <div class="msg-error"></div>

                                    <div class="clear"></div>
                                </li>

                                <li class="text-field with-icon span3">

                                    <label for="phone-contact-form">
                                        <span class="mainlabel">Phone</span>
                                    </label>

                                    <div class="input-prepend">
                                        <span class="add-on">
                                            <img src="images/contact-form/phone.png" alt="" title="" />
                                        </span>
                                        <input type="text" name="yit_contact[phone]" id="phone-contact-form" class="with-icon" value="" />
                                    </div>

                                    <div class="msg-error"></div>

                                    <div class="clear"></div>
                                </li>

                                <li class="textarea-field with-icon span9">

                                    <label for="message-contact-form">
                                        <span class="mainlabel">Message</span>
                                    </label>

                                    <div class="input-prepend">
                                        <span class="add-on">
                                            <img src="images/contact-form/pencil.png" alt="" title="" />
                                        </span>
                                        <textarea name="yit_contact[message]" id="message-contact-form" rows="8" cols="30" class="with-icon required"></textarea>
                                    </div>

                                    <div class="msg-error"></div>

                                    <div class="clear"></div>
                                </li>

                                <li class="submit-button span9">
                                    <input type="text" name="yit_bot" id="yit_bot" />
                                    <input type="hidden" name="yit_action" value="sendemail"/>
                                    <input type="hidden" name="yit_referer" value="get-in-touch.html" />
                                    <input type="hidden" name="id_form" value="4" />
                                    <input type="submit" name="yit_sendemail" value="Send Message" class="sendmail alignright" />
                                    <div class="clear"></div>
                                </li>
                            </ul>
                        </fieldset>
                    </form>

sendmail.php:

<?php
/**
 * Define the from email
 */ 

// email
define('TO_EMAIL', 'myemail@email.com.my');
define('FROM_EMAIL', 'email');
define('FROM_NAME', 'name');

/**
 * define the body of the email. You can add some shortcode, with this format: %ID%
 * 
 * ID = the id have you insert on the html markup.
 * 
 * e.g.
 * <input type="text" name="email" />
 *       
 * You can add on BODY, this:
 * email: %email%   
 */ 

define( 'BODY', '%message%<br /><br /><small>email send by %name%, email %email%, tel.: %phone%.</small>' );
define( 'SUBJECT', 'Email from yoursite.com' );

// here the redirect, when the form is submitted
define( 'ERROR_URL', 'contact-error.html' );
define( 'SUCCESS_URL', 'contact-success.html' );
define( 'NOTSENT_URL', 'contact-error.html' );

// the message feedback of ajax request
$msg = array(
    'error' => '<p class="error">Warning! Fill correctly the fields marked in red</p>',
    'success' => '<p class="success">Email sent correctly. Thanks to get in touch us!</p>',
    'not-sent' => '<p class="error">An error has been encountered. Please try again.</p>'
);      

// the field required, by name
$required = array( 'name', 'email', 'message' );

/**
 * Send the email.
 * 
 * SERVER-SIDE: the functions redirect to some URL, in base of result of control and send.
 * The urls must be set in the constants above: ERROR_URL, SUCCESS_URL, NOTSENT_URL
 * 
 * CLIENT-SIDE: in js/contact.js, there is already script for real-time checking of fields
 * and for ajax request of send email, that request in this page (sendmail.php) and echo the feedback message.    
 */   
sendemail();

// NO NEED EDIT
function sendemail() 
{                                
    global $msg, $required;

    if ( isset( $_POST['ajax'] ) )
        $ajax = $_POST['ajax'];
    else
        $ajax = false;

if ( isset( $_POST['yit_action'] ) AND $_POST['yit_action'] == 'sendmail' )  
{

   $body = BODY;

   $post_data = array_map( 'stripslashes', $_POST["yit_contact"] );
       //print_r($post_data);
        //die;

   foreach ( $required as $id_field ) {

            if( $post_data[$id_field] == '' || is_null( $post_data[$id_field] ) ) {
           if ( $ajax )
              end_ajax( $msg['error'] );
           else
          redirect( ERROR_URL );
       }                       
    }

   if( !is_email( $post_data['email'] ) OR $post_data['email'] == '' )
       if ( $ajax )
          end_ajax( $msg['error'] );
       else
          redirect( ERROR_URL );

   foreach( $post_data as $id => $var )
   {
    if( $id == 'message' ) $var = nl2br($var);
$body = str_replace( "%$id%", $var, $body );
}

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: '.FROM_NAME.' <'.FROM_EMAIL.'>' . "\r\n" . 'Reply-To: ' . $post_data['email'];

   $sendmail = mail( TO_EMAIL, SUBJECT, $body, $headers );


if ( $sendmail ) 
       if ( $ajax )
          end_ajax( $msg['success'] );
       else
          redirect( SUCCESS_URL );
   else
       if ( $ajax )
          end_ajax( $msg['not-sent'] );
       else
          redirect( NOTSENT_URL );
} 
}

function is_email($email) 
{
    if (!preg_match("/[a-z0-9][_.a-z0-9-]+@([a-z0-9][0-9a-z-]+.)+([a-z]{2,4})/" , $email))
    {
        return false;
    }
    else
    {
        return true;
    }
}

function end_ajax( $msg = '' ) {
    echo $msg;
    die;
}           

function redirect( $redirect = '' ) {
    header( 'Location: ' . $redirect );
    die;
}
?>

它与php设置有关吗?我使用php ver 5.3.29并且安全模式为On。我做了一些研究表明这可能是问题,但我找不到办法将其关掉。我使用的是Plesk 12,但我无法找到任何关闭安全模式的选项。

任何反馈都会非常感激!

嗨,大家好,感谢回复,但我终于想出了问题所在。它的电子邮件是域名的电子邮件地址。我不知道为什么,但我创建了一个测试电子邮件testing@domain.com并将其用作$ to并且工作正常。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您的脚本中存在错误,

if ( isset( $_POST['yit_action'] ) AND $_POST['yit_action'] == 'sendmail' )

if (isset($_POST["yit_action"]) AND $_POST["yit_action"] == "sendemail")

你忘记了E; - )