简体   繁体   English

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

[英]Using PHP for email file in HTML

I've been having problems using php in my html form. 我在html表单中使用php时遇到了问题。 While it will send, the $_POST variables are empty when I try to grab them in the php file. 虽然它会发送,但是当我尝试在php文件中抓取它们时,$ _POST变量是空的。 Any ideas on what I could be doing wrong? 关于我可能做错的任何想法?

My HTML code: 我的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> 

My php file: 我的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.'">';
?>

The only thing that sends in the email is: 电子邮件中唯一发送的内容是:

Results from Form:

Any help is appreciated, thanks in advance! 任何帮助表示赞赏,提前谢谢!

You should use the header in mail function. 您应该在邮件功能中使用标题。 Add following code in in your code too. 在代码中添加以下代码。

 $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.'');

Good luck. 祝好运。

You need to pass the headers into the mail function which is option. 您需要将标题传递给邮件功能,这是选项。

Here is the functions all parameters 这是函数的所有参数

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] ) bool mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])

$to Receiver, or receivers of the mail. $ to Receiver,或邮件的接收者。

$subject Subject of the email to be sent. $ subject发送电子邮件的主题。

$message Message to be sent. $ message要发送的消息。

$additional_headers this is the optional headers which is used for the mail options $ additional_headers这是用于邮件选项的可选标头

you can to set the following values in 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 The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail $ additional_parameters additional_parameters参数可用于将其他标志作为命令行选项传递给配置为在发送邮件时使用的程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM