0

我正在为我的网站联系表使用一个简单的 php 邮件程序。我有一个表格,首先询问他们的联系主题,如果他们选择了一个特定的选项,那么收件人选择将淡入,您可以选择要使用的收件人。但。由于垃圾邮件,我不想在代码中写电子邮件地址。所以我试图通过php变量来实现这一点。

我的邮件看起来像这样:

<?php
$var1="xxx@gmail.com";
$var2="xxx@gmail.com";
$to = "$_POST[kohde]";
$subject = "$_POST[asia]";
$message = "
$_POST[tiedot]\n
$_POST[nimi]
$_POST[email]
$_POST[puhelin]
IP-osoite: $_POST[ip] ";
$from = "xxx.fi";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>

但是按原样使用变量名是行不通的。那么,如何在不将电子邮件硬编码为选项值的情况下选择要与该选项一起使用的电子邮件呢?

 <select name="kohde" style="width:233px;" >
    <option value="$var1">xxx.fi</option>
    <option value="$var">Sivun tekijälle</option>
    </select>
4

2 回答 2

0

您的第一个代码应该是这样的:

<?php
$var1="xxx@gmail.com";
$var2="xxx@gmail.com";

$to = $_POST["kohde"];
$subject = $_POST["asia"];

$message = $_POST["tiedot"] . 
$_POST["nimi"] . 
$_POST["email"] . 
$_POST["puhelin"] . 
"IP-osoite:" . $_POST["ip"];

$from = $_POST["kohde"];
$headers = "From:" . $from;

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

You have to put the quotes around the array selectors, within the brackets, not around the whole variable name. Also, you can add the form value as $from using $_POST["kohde"].

于 2012-06-05T18:08:41.787 回答
0

I would do it this way...

Use this PHP to get the $to address:

$goodAddresses = array(1 => 'a@x.com', 2 => 'b@x.com');
$to = 'default@x.com';
if(isset($goodAddresses[$_POST['kohde']])) {
    $to = $goodAddresses[$_POST['kohde']];
}

And change the values of your select options in our html file to match the array keys in the PHP:

<select name="kohde" style="width:233px;" >
    <option value="1">xxx.fi</option>
    <option value="2">Sivun tekijälle</option>
</select>
于 2012-06-05T18:11:07.620 回答