通过PHP解析HTML表单并发送电子邮件

时间:2016-03-18 14:20:35

标签: php html email

我目前正在尝试创建一个非常基本的PHP脚本,该脚本从html表单中获取内容,将它们存储为变量,然后发送包含所述信息的电子邮件。

执行此操作后,所有内容似乎都与实际的消息信息分开。

我的HTML上写着:

<table style="width:100%"> 
    <form action="form_complete.php" method="POST"> 
    <tr> 
        <th> <input type="text" name="name" required placeholder="Name" /> </th> 
        <th> <input type="text" name="email" required placeholder="Email" /> </th> 
   </tr> 
   <tr> 
        <th> <input type="text" name="mobile" required placeholder="Mobile" /> </th> 
        <th> <input type="text" name="subject" required placeholder="Email Subject" /> </th> 
   </tr> 
   <tr> 
        <th colspan="2"> <textarea name="message" required placeholder="Enter your message"></textarea> </th> 
   <tr> 
        <th> </th> 
        <th> <input id="submit" type="submit" name="submit" value="Send" /> </th> 
   </tr> 

和相关的PHP读取:

if(isset($_POST['submit'])){
$to = "example@123.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile']; 
$subject = $_POST['subject'];
$getMessage = $_POST['message']; 
$subject2 = "Receipt of email submission";
$message = "You have received a new email from " . $name . ". \n    Message: " . $getMessage; 
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);

就像我说的,当我检查电子邮件的内容时,名称解析得很好,手机号码和主题也是如此,但消息的内容似乎并不存在,我不明白为什么。

1 个答案:

答案 0 :(得分:1)

尝试将PhpMailer用于您的电子邮件。 另外如果你想使用PHP邮件功能试试这个:

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);