【问题标题】:Posting form to custom PHP script within Wordpress在 Wordpress 中将表单发布到自定义 PHP 脚本
【发布时间】:2013-07-01 03:33:46
【问题描述】:

我将此联系表添加到 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;
    }
}

?>

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

谢谢!

【问题讨论】:

  • 天哪,这太开放了。

标签: php wordpress


【解决方案1】:

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

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

该功能一般用于wordpress中发送邮件

【讨论】:

    猜你喜欢
    • 2012-08-29
    • 2017-04-22
    • 2019-05-31
    • 2016-04-01
    • 2018-09-08
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2012-08-28
    相关资源
    最近更新 更多