通过php直接发送联系表格的内容到电子邮件

时间:2016-08-11 12:01:50

标签: javascript php wordpress forms email

我的wordpress网站上有联系表格,但是当点击提交按钮时,我无法从表格中获取信息以发送到我的电子邮件地址。

点击提交按钮并发送电子邮件后,我希望确认信息显示在提交按钮下方,消息已发送/错误(如果没有)。注意:我将更新联系表单的css以容纳额外的文本。

这是我的表单代码:

<form action="secure_email.php" method="post" id="contact-form-content">
    <h5>You have had a look, so let's get cracking. Email me at me@myemail.com or use this nifty thing.</h5><br></br> 
    <legend>Contact Form</legend>
    <input type="text" placeholder="Full Name" name="full-name" id="full-name" required;><br></br> 
    <input type="text" placeholder="Email" name="email" id="email" required;><br></br>
    <textarea placeholder="Message" name="message" id="message" rows="100" cols="100" wrap="hard" required;></textarea><br></br> 
    <button type="submit" name="submit" value="submit">Send</button>
</form>

这是secure_email.php文件:

        <?php 
        if(isset($_POST['submit'])){
            $to = "me@myemail.com"; // this is your Email address
            $email = $_POST['email']; // this is the sender's Email address
            $full-name = $_POST['full-name'];
            $subject = "Form submission";
            $message = $full-name . " " . $email . " wrote the following:" . "\n\n" . $_POST['message'];

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            mail($to, $subject, $message, $headers);

 if (isset($_POST['submit'])) 
        {
        if (mail($to, $subject, $message, $headers))

        echo "Thank you for contacting me!";
        }
        else 
        { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
    ?>

只是更新所有这些是工作代码,联系表单详细信息发送到我的给定电子邮件,联系表单中显示确认消息并滚动回联系表单以查看确认消息

    <div id="contact-form">
    <form action="#contact" method="post" id="contact-form-content">
    <h5>You have had a look, so let's get cracking. Email me at me@myemail.com or use this nifty thing.</h5><br></br> 
    <legend>Contact Form</legend>
    <input type="text" placeholder="Full Name" name="fullname" id="fullname" required;><br></br> 
    <input type="text" placeholder="Email" name="email" id="email" required;><br></br>
    <textarea placeholder="Message" name="message" id="message" rows="100" cols="100" wrap="hard" required;></textarea><br></br> 
    <button type="submit" name="submit" value="submit">Send</button>
<?php 
if(isset($_POST['submit'])){
    $to = "me@myemail.com"; // this is your Email address
    $email = $_POST['email']; // this is the sender's Email address
    $fullname = $_POST['fullname'];
    $subject = "Form submission";
    $message = $fullname . " " . $email . " wrote the following:" . "\n\n" . $_POST['message'];
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

if (mail($to, $subject, $message, $headers)){
        echo "Thank you for contacting me!";
        }
        else 
        { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
        }
    }
?>
</form>
</div>
</div>

2 个答案:

答案 0 :(得分:0)

请使用邮件功能。

PHP邮件功能

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);

WordPress邮件功能

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

答案 1 :(得分:-1)

你要么

使用Ajax将表单数据发布到secure-email.php,然后在JavaScript中添加成功消息。

或者

发布到与表单相同的页面,检测表单是否已提交并发送您的电子邮件,然后在邮件成功发送时回显您的成功消息。点击提交时页面将刷新,并显示消息。