在HTML中使用PHP作为电子邮件文件

时间:2016-10-07 02:09:40

标签: php html

我在html表单中使用php时遇到了问题。虽然它会发送,但是当我尝试在php文件中抓取它们时,$ _POST变量是空的。关于我可能做错的任何想法?

我的HTML代码:



<form class="submitAMessage" name="Submit a Message" method="post" action="sendresults.php">
  <div>
    <h4>Submit a Message:</h4>
    <label for="name">Name:<br><span class="required"></span></label>
    <input type="text" id="name" name="name" placeholder="Your name" required="required" />
  </div>
  <div> <br>
    <label for="email">Email Address:<br><span class="required"></span></label>
    <input type="email" id="email" name="email" placeholder="your@email.com" required="required" />
  </div>
  <div> <br>
    <label for="message">Message:<br><span class="required"></span></label>
    <textarea id="message" name="message" placeholder="Write your message here." required></textarea>
  </div>
  <div>
    <input type="submit" id="submit" name="submit" formmethod="POST" value="Submit" />
  </div>
</form>
&#13;
&#13;
&#13;

我的php文件:

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact Form';

$emailfrom = 'noreply@website.com';

// Your email address. This is where the form information will be sent. 
$emailadd = 'website@gmail.com'; 

// Where to redirect after form is processed. 
$url = 'http://www.website.com/main.html'; 

// Makes all fields required. If set to '1' no field can not be empty.
// If set to '0' any or all fields can be empty.
$req = '0'; 

// --------------------------Do not edit below this line--------------------------
$text = "Results from Form:\n\n";
$name = $_POST['name']; 
$email = $_POST['email'];
$message = $_POST['message'];
$line = '
';

mail($emailadd, $subject, $text.$name.$line.$email.$line.$message, 'From: '.$emailfrom.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

电子邮件中唯一发送的内容是:

Results from Form:

感谢任何帮助,提前谢谢!

2 个答案:

答案 0 :(得分:0)

您应该在邮件功能中使用标题。在代码中添加以下代码。

 $header = "From:abc@somedomain.com \r\n";
 $header .= "Cc:afgh@somedomain.com \r\n";
 $header .= "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html\r\n";

mail($emailadd, $subject, $text.$name.$line.$email.$line.$message,$header, 'From: '.$emailfrom.'');
祝你好运。

答案 1 :(得分:0)

您需要将标题传递给邮件功能,这是选项。

这是函数所有参数

bool mail(字符串$ to,字符串$ subject,字符串$ message [,字符串$ additional_headers [,字符串$ additional_parameters]])

  

$ to 接收方或邮件的接收方。

     

$ subject 要发送的电子邮件的主题。

     

$ message 要发送的消息。

     

$ additional_headers 这是用于邮件选项的可选标头

您可以在标题中设置以下值。

// header configuration for to send the HTML mail
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
  

$ additional_parameters additional_parameters参数可用于将其他标志作为命令行选项传递给配置为在发送邮件时使用的程序