将表单发布到Wordpress中的自定义PHP脚本

时间:2013-07-01 03:33:46

标签: php wordpress

我将此联系表单添加到wordpress中的自定义页面模板:

<form id="contact_form" action="inc/mail.php" method="POST">
    <input id="name" name="name" placeholder="Name">
    <input id="email" name="email" placeholder="Email">
    <input id="phone" name="phone" placeholder="Phone Number">
    <textarea  id="comments" name="comments" placeholder="Comments / Questions"></textarea>
    <input class="button" type="submit" id="submit" value="Submit">
</form>

我创建了一个PHP页面来处理一旦发布的表单:

<?php   
// Gather form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
$client = "";
$clientname = "";
$password = "";

if($name && $email && $comments) {

    // Create instance of PHP mailer
    require("class.phpmailer.php");
    $mail = new PHPMailer();

    $mail->IsSMTP(); 
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465;
    $mail->Username = $client;
    $mail->Password = $password;

    $mail->From = $client;
    $mail->SetFrom($email, $name);
    $mail->AddAddress($client, $clientname);

    $mail->IsHTML(false);   

    $formcontent="From: \n $name \n\n Email: \n $email \n\n Phone: \n $phone \n\n Comments: \n $comments";
    $mail->Subject = "Miss Mary's Inquiry";
    $mail->Body = $formcontent;

    if(!$mail->Send()){
        echo "Message could not be sent. <p>";
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }
}

&GT;

表单和处理程序完全在Wordpress之外工作,但我认为Wordpress阻止访问PHP脚本(mail.php)。有没有办法在没有Wordpress / htaccess干扰的情况下将表单发布到我的PHP脚本?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您想在wordpress中发送邮件,请尝试使用插件。有大量的wordpress插件。 Conatact form 7是我最喜欢的那个。你仍然可以开发自己的,但创建它作为插件。您可以将上面的代码转换为短代码插件。 Check this link

wp_mail( $to, $subject, $message, $headers, $attachments )

此功能通常用于在wordpress中发送邮件