PHP - 从任何地址发送电子邮件

时间:2013-09-04 02:03:06

标签: php email

我正在网站上工作,对于网站,我需要一个可以填写发送电子邮件的表格。表单必须有一个名字和姓氏的地方,发送电子邮件的电子邮件地址,以及邮件发送到的电子邮件地址,消息主题,当然还有消息的位置。我知道这是可能的,因为我已经看到它在过去完成,但我不知道该怎么做。有人可以请我帮忙吗?非常感谢。

修改

HTML          

        <div id="contact" align="center"> <!--Contact Instructions-->
        <h1 align="center">Contact Us</h1>
        <p style="font-size:20px">
        At the bottom of the page you will find a contact form in which you can submit your questions. Please describe your problem to the best of your ability, the more details you give us, the faster you will get a response, and hopefully a solution.
        </p>



        <form action="notphp.php" method="post" id="contact form" style="font-size:18px">
        First name: <input type="text" name="First" required placeholder="Required"> <br>
        Last name: <input type="text" name="Last" placeholder="Optional"> <br>
        Email: <input type="email" name="Email" required placeholder="Required"> <br>
        Question: <textarea required name="Question" placeholder="Required" id=" Question" cols="100" rows="10"></textarea> <br>
        <input type="submit" value="Submit" id="sendbutton">
        </form>
        </div>
    </body>
</html>

PHP

<?php
$first = $_POST['First'];
$last = $_POST['Last'];
$email = $_POST['Email'];
$question = $_POST['Question'];

if(mail("somebogusemailaddress@gmail.com", $first." Question", "Name: ".$first." ".$last." ".$email."\n".$question))
echo("Your question was successfully submited.");
else echo ("Your message was not submitted successfully, please try again.");
?>

<html>
<head>
<script type="text/javascript">
<!--
function Redirect()
{
    window.location="index.html";
}

document.write("Your message has been accepted. You will be redirected to the home page in a moment.");
setTimeout('Redirect()', 5000);
//-->
</script>
</head>
</html>

1 个答案:

答案 0 :(得分:1)

要设置电子邮件的“发件人地址”,您需要在标题中设置它。

$headers = 'From: '.$email_from."\r\n".
  'Reply-To: '.$email_from."\r\n" .
  'X-Mailer: PHP/' . phpversion();
$email_to = 'test@test.com';
$email_subject = 'This is my subject';

mail($email_to, $email_subject, $email_message, $headers);