Question

I created a contact form, but it doesn't work and I don't know why. What's wrong with my code? This is the warning message: Warning: mail() expects parameter 1 to be string, array given. I try to use test mail server tool from localhost, I tried everything, but nothing happens. If I'm not mistaken, the warning message says that's something wrong with the $adminemail. With var_dump it looks like this: array (size=1) 'i_email' => string 'myemail@gmail.com' (length=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\" />&nbsp;<input type=\"reset\" value=\"Cancel\"/></p>

            </form>
            </div>
Was it helpful?

Solution

As the error says - the first parameter of the mail() function should be a string not an array. Use this instead:

mail($adminemail['i_email'], $subject, $level, $fejresz);

This is because mysql_fetch_assoc() fetches an array (the clue is in the function name: assoc is short for associative array):

$adminemail=mysql_fetch_assoc($eredmeny);

You need to get the string out of that array using the index that you can see when you var_dump() which is probably also your field name:

$adminemail['i_email']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top