1

我将此联系表添加到 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 脚本?

谢谢!

4

1 回答 1

1

如果您想在 wordpress 中发送邮件,请尝试使用插件。有大量的 wordpress 插件。 联系表格 7是我最喜欢的表格。您仍然可以开发自己的,但将其创建为插件。您可以将上述代码转换为简码插件。检查此链接

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

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

于 2013-07-01T03:48:14.323 回答