联系表单代码不起作用。需要帮助!

时间:2017-09-08 作者:Martina Wills

我在WordPress中通过创建短代码使用下面的联系人代码。当我提交表单时,它会显示成功消息,但收件箱中没有收到任何电子邮件或垃圾邮件。谁能帮忙吗。

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 个回复
SO网友:HU ist Sebastian

抄袭我的答案"An old plugin self-made stoped sending mail":

许多电子邮件寄宿者改变了他们的规则以防止垃圾邮件。通常情况下,在“发件人”标记中有一个域名并且来自不同域的服务器的电子邮件会被拒绝。所以我的猜测是,也许电子邮件仍然发送,但没有收到。

您可以通过更改此行来检查这一点:

$headers = "From: ".$name." <" . strip_tags($email) . ">\\r\\n";
对这样的事情:

$my_domain = $_SERVER[\'HTTP_HOST\'];
$headers = \'From: orders@\'.$my_domain."\\r\\n".\'Reply-To: \'.$senderEmail."\\r\\n";
并检查电子邮件现在是否正确发送和接收。(通过设置“回复到”标题,在邮件客户端中单击“回复”时会插入正确的地址。)

SO网友:Jeff Mattson

只是为了确保mail() 函数成功,请尝试更改此部分:

$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>\';
}
如果你仍然看到成功的信息,你就会知道这是另外一回事。

结束

相关推荐

create shortcodes for posts

$query = new WP_Query(array( \'post_type\' => \'my_posttype\', \'post_status\' => \'publish\', \'posts_per_page\' => -1, \'orderby\' => \'title\', \'order\' => \'ASC\' )); while ($query->have_posts()) {&#