电子邮件TO标头中的PHP变量

时间:2018-12-14 17:52:24

标签: php forms email

我正在尝试使用PHP mail()发送电子邮件。我创建了一个表格,允许用户输入他们的信息,然后按提交按钮发送电子邮件。

我想做的是获取$ email并将其设置为$ to标头以发送给接收者。其他所有东西都运行良好,$ name和$ contact输出仅缺少$ email。

<form action="#" method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>

<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>

<?php

$name = $_POST['name']; 
$email = $_POST['email'];
$contact = $_POST['contact'];

$to = $email;
$subject = "Test Email";
$message = 'Hello '.$name.', contact: '.$contact.', this is a test email';
$headers = "From: Me <". strip_tags('test@mail.com') . ">\r\n";

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

?>

任何提示将不胜感激

1 个答案:

答案 0 :(得分:0)

解决方案:

<?php
if (!array_key_exists('Submitted',$_POST))
{
 ?>
<form method="post" action="#">
<input type="hidden" name="Submitted" value="true"><form action="#" 
 method="post">
<input type="text" name="name" placeholder="Your Name"><br/>
<input type="text" name="email" placeholder="Your Email"><br/>
<input type="text" name="contact" placeholder="Your Mobile"><br/>
<input type="submit" name="submit" value="Submit">
</form>

<?php
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
?>

<?php
    }
else
{

$name = $_POST['name']; 
$contact = $_POST['contact'];


$to = $_POST['email'];
$subject = "Test Email";
$message = 'Hello '.$name.',<br /> contact #: '.$contact.', this is a test 
 email';
$headers = "From: Me <". strip_tags('test@mail.com') . ">\r\n";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

 if(mail($to,$subject,$message,$headers))
    {
        echo "Message Sent";
    }
    else
    {
         echo "Message Not Sent";
    }
  }
 ?>