我创建了一个联系表单,但它不起作用,我不知道为什么。我的代码出了什么问题?这是警告消息:警告:mail()期望参数1为字符串,给定数组。我尝试使用localhost的测试邮件服务器工具,我尝试了一切,但没有任何反应。如果我没有弄错的话,警告信息显示$ adminemail有问题。使用var_dump它看起来像这样: 数组(大小= 1) ' i_email' => string' myemail@gmail.com' (长度= 20)
<?php
if (isset($_POST['ok'])) {
$name = mysql_real_escape_string(ucwords(strtolower(strip_tags(trim($_POST['name'])))));
$email = mysql_real_escape_string(strtolower(strip_tags(trim($_POST['email']))));
$tel = mysql_real_escape_string(strtolower(strip_tags(trim($_POST['tel']))));
$subject = mysql_real_escape_string(ucfirst(strtolower(strip_tags(trim($_POST['subject'])))));
$message = mysql_real_escape_string(strtolower(strip_tags(trim($_POST['message']))));
$captcha = $_POST['captcha'];
if (strlen($name) < 5)
$hibak[] = "Wrong name!";
if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) || (strlen($email) < 6))
$hibak[] = "Wrong email!";
if (strlen($tel) < 10)
$hibak[] = "Wrong tel!";
if (strlen($subject) < 5)
$hibak[] = "Wrong subject!";
if (strlen($message) < 30)
$hibak[] = "Min 30 characters long";
if (empty($captcha))
$hibak[] = "Captcha is empty!";
if ($captcha != $_SESSION['captcha'])
$hibak[] = "Wrong captcha!";
if (isset($hibak)) {
$asd = "<ul>\n";
foreach($hibak as $hiba) {
$asd.= "<li>{$hiba}</li>\n";
}
$asd.= "</ul>\n";
}
else {
$sql = "SELECT i_email FROM info WHERE i_id=1";
$eredmeny=mysql_query($sql);
$adminemail=mysql_fetch_assoc($eredmeny);
$level= "Details of the sender:\n";
$level.= "Name: {$name} \n";
$level.= "E-mail: {$email}\n";
$level.= "Tel: {$tel}\n";
$level.= "Subject: {$subject}\n";
$level.= "Message:{$message}\n";
$fejlec="From: {$email}"."\r\n".
"Reply-To:{$email}\n";
if(mail($adminemail, $subject, $level, $fejresz)){
$asd = "<p><em>Mail sent!</em></p>\n";}
else{
$asd="<p><em>Something is wrong!</em></p>\n";
}
unset($name);
unset($tel);
unset($email);
unset($captcha);
unset($subject);
unset($message);
}
}
?>
<div class=\"jobb\">
<form action=\"\" method=\"post\">
<label for=name>Name:</label>
<br />
<input type=text id=name name=name />
<br />
<label for=email>E-mail:</label>
<br />
<input type=email id=email name=email />
<br />
<label for=tel>Tel:</label>
<br />
<input type=tel id=tel name=tel />
<br />
<label for=subject>Subject:</label>
<br />
<input type=text id=subject name=subject />
<br />
<label for=message>Message:</label>
<br />
<textarea id=message name=message rows=5 cols=30></textarea>
<br />
<label for=\"captcha\">Captcha:</label><br />
".$kod."<br />
<input type=\"text\" id=\"captcha\" name=\"captcha\" required=\"required\" /></p>
<input type=\"submit\" id=\"ok\" name=\"ok\" value=\"Send\" /> <input type=\"reset\" value=\"Cancel\"/></p>
</form>
</div>
答案 0 :(得分:2)
正如错误所说 - mail()函数的第一个参数应该是字符串而不是数组。请改用:
mail($adminemail['i_email'], $subject, $level, $fejresz);
这是因为mysql_fetch_assoc()获取一个数组(线索在函数名中:assoc是关联数组的缩写):
$adminemail=mysql_fetch_assoc($eredmeny);
您需要使用var_dump()时可以看到的索引从该数组中获取字符串,这可能也是您的字段名称:
$adminemail['i_email']