静态页面发送电子邮件WordPress

时间:2013-09-13 04:15:18

标签: php html wordpress

我是WordPress的新手。我创建了一个静态页面contact.php。在联系页面中,我有一个联系表单,因此用户可以与我联系。当用户点击提交按钮时,它应该转到index.php并提示电子邮件传递成功,否则显示不成功..

这是contact.php的代码

<form class="form" method="post" action="/send-email.php">

    <p class="name">
        <input type="text" name="name" id="name" placeholder="Enter your name" size="40" />
        <label for="name">Name</label>
    </p>

    <p class="email">
        <input type="text" name="email" id="email" placeholder="mail@example.com"   size="40" />
        <label for="email">Email</label>
    </p>

    <p class="web">
        <input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />
        <label for="telephone">Telephone</label>
    </p>        

    <p class="text">
        <label for="message">Message</label>
        <textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
    </p>

    <p class="submit">
        <input type="submit" value="Send" />
        <input type="reset" value="Cancel" />
    </p>

</form>

发送-email.php

<?php
$ToEmail = 'sample@sample.com';
$EmailSubject = 'Contact Form';
$mailheader = "From: " . $_POST["email"] . "\r\n";
$mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
$MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
$MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
$MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");


/* echo "<pre>";
  print_r($MESSAGE_BODY);
  echo "<pre>"; */

header("Location: /index");

但它没有发送。 URL看起来就像www.site.com/send-email.php

我在这里想念的是什么?有任何想法吗?我将衷心感谢您的帮助。感谢。

2 个答案:

答案 0 :(得分:0)

die("Failure")之类的声音可能会引发并记录“失败”消息,而不是在屏幕上打印。或者邮件模块没有安装,所以它遇到了一个错误,它不知道mail()函数甚至是什么 - 不可否认。不太确定。

我的猜测是您的服务器甚至没有设置为正确发送邮件。如果您运行自己的服务器,这可能是一个复杂的设置。如果你在共享服务器上,它确实应该已经设置...除非你的主机不是很好。

在任何情况下,如果您在WordPress中运行,我建议您自己安装WordPress的人员安装Jetpack插件。它包含一项功能,只需点击几下光标即可为页面添加联系表单。

如果该表单不会发送电子邮件,则表示您提出的问题超出了您提供的信息的范围。

答案 1 :(得分:0)

<form class="form" method="post" action="index.php">

    <p class="name">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" placeholder="Enter your name" size="40" />

    </p>

    <p class="email">
         <label for="email">Email</label>
        <input type="text" name="email" id="email" placeholder="mail@example.com"   size="40" />

    </p>

    <p class="web">
         <label for="telephone">Telephone</label>
        <input type="text" name="telephone" id="telephone" placeholder="000-000-000" size="40" />

    </p>        

    <p class="text">
        <label for="message">Message</label>
        <textarea name="message" placeholder="Write something to us" cols="40" rows="5" /></textarea>
    </p>

    <p class="submit">
        <input type="submit" name="submit" value="Send" />
        <input type="reset" value="Cancel" />
    </p>

</form>

和你的index.php

<?php
if(isset($_POST['submit'] == 'Send')) {
    $ToEmail = 'sample@sample.com';
    $EmailSubject = 'Contact Form';
    $mailheader = "From: " . $_POST["email"] . "\r\n";
    $mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
    $MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
    $MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
    $MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
    $status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");

}

if($status){
    echo "your success message";
}
 ?>

试试这个

还有一件事

由于您在wordpress上使用此功能,因此最好使用wordpress的wp_mail功能