直接从表单发送邮件

时间:2013-10-13 21:00:50

标签: php email

我的网站上有一个联系我们页面。我想要的是当有人填写表格并点击发送按钮时。该消息应该到达我的Gmail。我为它编写了以下代码。它不起作用。有没有其他方法可以实现同样的目标。

Html代码:

<form id="ContactForm" action="contacts.php" method="post">
    <div>
        <div  class="wrapper"> <strong>Name:</strong>
            <div class="bg">
                <input type="text" class="input" name="name">
            </div>
        </div>
        <div  class="wrapper"> <strong>Email:</strong>
            <div class="bg">
                <input type="text" class="input" name="email">
            </div>
        </div>
        <div  class="textarea_box"> <strong>Message:</strong>
            <div class="bg">
                <textarea cols="1" rows="1" name="message"></textarea>
            </div>
        </div>
        <a href="javascript:;" onclick="document.getElementById('ContactForm').submit();" class="button"><span>Send</span></a> <a href="#" class="button"><span>Clear</span></a> </div>
          </form>

php code

<?php
session_start();
$to = "someemail@gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>

3 个答案:

答案 0 :(得分:1)

首先,您应该检查PHP注入的输入。

$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);

除此之外,您的邮件脚本似乎没有任何问题。问题很可能是由PHP服务器引起的。您的网络托管是否肯定提供PHP邮件?大多数免费的网络主机都不提供此功能,因为它们通常用于发送垃圾邮件。

答案 1 :(得分:0)

很抱歉,但您的代码很糟糕(特别是那些连接)。使用提供OOP样式的Swift mailer并为您完成所有标题作业。并确保您已安装任何邮件服务器(您是否检查过它?)。

答案 2 :(得分:0)

PHP表单:

<?php

header( 'Content-Type: text/html; charset=utf-8' );

// Your Email
$receiver = 'max.mustermann@domain.tld';


if (isset($_POST['send']))
{






    $name = $_POST['name']

    $email = $_POST['email'];

    if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
    {
        die( 'Please fill in all fields!' );
    }
    else
    {
        $subject   = $_POST['subject'];
        $message = $_POST['message'];
    }




    $mailheader  = "From: Your Site <noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
    $mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
    $mailheader .= "Return-Path: noreply@" .$_SERVER['SERVER_NAME']. "\r\n";
    $mailheader .= "MIME-Version: 1.0\r\n";
    $mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
    $mailheader .= "Message-ID: <" .time(). " noreply@" .$_SERVER['SERVER_NAME']. ">\r\n";
    $mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";


    if (@mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
    {

        echo 'Email send!';
    }
}

?>

HTML表单:

<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>