使用php变量发送带有php的电子邮件

时间:2013-03-28 14:31:35

标签: php phpmailer

我正在使用PHP脚本发送带有php变量的电子邮件(使用POST方法通过表单获取)。 这是我的代码:

<?php
/* These will gather what the user has typed into the fieled. */

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$questionField = $_POST['question'];

/* These are the variable that tell the subject of the email and where the email will be sent.*/

$emailSubject = 'Recupero password LightSchool';
$mailto = $_POST['email'];
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';

/* This takes the information and lines it up the way you want it to be sent in the email. */

$body = '<br><hr><br> Name: '.$name.' <br> Email: '.$email.' <br> Question: '.$question.' <br>';

$headers = "From: $email\r\n"; // This takes the email and displays it as who this email is from.
$headers .= "Content-type: text/html\r\n"; // This tells the server to turn the coding into the text.
$success = mail($mailto, $emailSubject, $body, $headers); // This tells the server what to send.

?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title>MY Studenti</title>
    <link rel="stylesheet" href="style.css" type="text/css" />
    </head>  
    <body style="background-image:url(http://studenti.lightschool.it/new/bkg.png)">  
    <div id="main">
    <h1>
    <img src="http://images.lightschool.it/logo/medium250x250.png" alt="" height="66" style="float: left; margin-right: 20px" width="62" />MY Studenti</h1>
    <p>Recupera password ti permette di recuperare la tua password, tramite 
    l'inserimento del tuo nome utente.</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <form id="form1" name="form1" method="post" action="reset-pwd.php">
<table width="455" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="175" height="44" align="center"><label for"name">name</label></td>
    <td width="280"><input name="name" type="text" id="name" size="30" />
      </td>
  </tr>
  <tr>
    <td height="45" align="center"><label for="email">email</label></td>
    <td><input name="email" type="text" id="email" size="30" /></td>
  </tr>
  <tr>
    <td height="41" align="center"><label for="question">question</label></td>
    <td><textarea name="question" cols="30" rows="5" id="question"></textarea></td>
  </tr>
  <tr>
    <td height="38">&nbsp;</td>
    <td><label>
      <input type="submit" name="Submit" id="Submit" value="Submit" />
    </label></td>
  </tr>
</table>

</form>
    </div>
    </body>
    </html>

此PHP脚本成功运行,但有三个变量(名称,电子邮件和问题)不会显示在已发送的电子邮件中。 我也试过这个var_dump($_POST['email']);而没有解决。 我该如何解决这个问题?

感谢。

PS:我知道在这个网站和其他网站上发布了很多这样的问题,但这些发布的解决方案都没有帮助我。

2 个答案:

答案 0 :(得分:4)

更改

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$questionField = $_POST['question'];

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

答案 1 :(得分:1)

你正在给你的Vars打两个不同的东西。你要小心这个。如果你没有过滤任何人的电子邮件,那么有人可以提供一个地址列表并开始从你的盒子发送垃圾邮件。