如何通过php向用户发送邮件

时间:2015-08-22 04:04:25

标签: php email

我的网站上有联系表格,工作正常。我也收到了邮件。

我的网站为this,此处为contact form

我面临的唯一问题是我很容易收到邮件,但我无法将恢复邮件发送给用户。 我也试过了php mail()函数。

这是我的联系表单页面 ' contact.html' :

<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="master.js"></script>
<link rel="stylesheet" type="text/css" href="style1.css">
<title>welcome to peter's official website</title>
</head>
<body>
<div id = "header">
    <div class="menu">
    <a  href="index.html">
    <div class="menuitem">Home
    </div>
    </a>
    <font size="5px"    color="white">|</font>
    <a  href="peter.html">
    <div class="menuitem">About me 
    </div>
    </a>
    <font size="5px"    color="white">|</font>
    <a href="contact.html">
    <div class="menuitem">Contact Me
    </div>
    </a>
    </div>
</div>
<div id="container">
<h1 class="middle-text">Contact Me</h1>
<form id="contact-form" action="mail.php" method="post">
<div>
<label>
    <span>First Name<font color="red" size="4px">*</font> :</span></br>
    <input name="fname" type="text" Placeholder="Your First Name" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
    <span>Last Name<font color="red" size="4px">*</font> :</span></br>
    <input name="lname" type="text" Placeholder="Your Last Name" tabindex="2" required>
</label>
</div>
<div>
<label>
    <span>E-mail<font color="red" size="4px">*</font> :</span></br>
    <input name="email" type="email" Placeholder="Your E-mail" tabindex="3" required>
</label>
</div>
<div>
<label>
    <span>subject<font color="red" size="4px">*</font> :</span></br>
    <input name="subject" type="text" Placeholder="Email subject" tabindex="4" required>
</label>
</div>
<div>
<label>
    <span>Message<font color="red" size="4px">*</font> :</span></br>
    <textarea name="message" type="text" placeholder="Your Message" tabindex="5" required></textarea>
</label>
</div>
<div>
    <button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
</div>
<script src="validation-script.js"></script>
</body>
</html>

这是&#39; mail.php&#39; :

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From:\n$fname $lname\n\nSubject:\n$subject\n\nMessage:\n$message \n\nEmail:\n$email";
$recipient = "contact@peterbousaada.cf";
mail($recipient,$subject, $formcontent) or header("Location:http://peterbousaada.cf/error.html");
$subject = "thank you";
$formcontent = "we have recieved your email, and we will get back to you as soon as possible";
$recipient = $email;
mail($recipient,$subject,$formcontent) or header("Location:http://peterbousaada.cf/error.html");
header("Location:http://peterbousaada.cf/thankyou.html");
?>  
enter code here

为了清楚起见,我尝试重命名第二个mail()函数中的变量     的 $收件人,$主题,$ formcontent 至     的 $电子邮件,$下一子,$下-MSG

请注意我对php很新,所以请尽量详细解释你的答案。 提前致谢

1 个答案:

答案 0 :(得分:-2)

更改你的邮件.php&#39;归档到此,我希望它有效。

 <?php
        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $formcontent="From:\n$fname $lname\n\nSubject:\n$subject\n\nMessage:\n$message \n\nEmail:\n$email";

        $recipient = "contact@peterbousaada.cf";


    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <contact@peterbousaada.cf>' . "\r\n";
    $headers .= 'Cc: abc@example.com' . "\r\n";

    $mail=mail($email,$subject,$message,$headers);
    if($mail!=false)
    {
      echo "mail sent";
    }
    else
    {
      echo "mail not sent";
    }