-1

I'm finishing up my project now but can't seem to see where i went wrong on my PHP form. To me everything is correct yet i'm getting an error.

Warning: mail() expects parameter 4 to be string, array given in /customers/f/a/b/webpx.be/httpd.www/sendemail.php on line 15

My php form:

<?php
$name       = @trim(stripslashes($_POST['name'])); 
$email      = @trim(stripslashes($_POST['email'])); 
$message    = @trim(stripslashes($_POST['message'])); 
$to         = 'email@email.com';//replace with your email

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

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

die;

?>

<?php
if ($_POST['submit']) {
    if (mail($to, $subject, $message, $headers)) { 
        echo '<p>Thank you for your Email! We will contact you very soon.</p>';
    } 
    else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

<style type="text/css">
    p{text-align:center;font-size:50px;background:#0091a2;margin-top:30px;padding:20px;width:500px;margin:0 auto;color:#fff}
</style>

My HTML:

<form method="post" action="sendemail.php" onsubmit="return validation();">
                            <div class="row">
                                <div class="form-group col-md-6">
                                    <input type="text" name="name" class="form-control" placeholder="voornaam + familienaam" required="required">
                                </div>
                                <div class="form-group col-md-6">
                                    <input type="email" name="email" class="form-control" placeholder="e-mail" required="required">
                                </div>
                                <div class="form-group col-md-12">
                                    <textarea rows="6" name="message" class="form-control" placeholder="Uw boodschap ..." required="required"></textarea>
                                </div>
                                <div class="form-group col-md-12">
                                  <button type="submit" class="btn btn-lg btn-dark-bg" data-loading-text="Sending...">Verstuur bericht</button>
                                </div>
                            </div>
                        </form>

The only thing I think that can be wrong is the "to" in my form. Could someone point me in the right direction please?

Thx in advance!

John Conde
  • 217,595
  • 99
  • 455
  • 496

2 Answers2

3

$headers in an array and it needs to be a string. Each header should be separated by a new line. So implode() your string with a newline character:

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
$headers = implode("\r\n", $headers);

mail($to, $subject, $message, $headers);
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

I use this one for it, but it is not as clear as implode(). It is working fine.

$headers  = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1"."\r\n";
$headers .= "From: $user_email"."\r\n";
$headers .= "Reply-To: $user_email\r\n"; ."\r\n";
$headers .= "X-Mailer: PHP/".phpversion()"."\r\n";

mail($to, $subject, $message, $headers);
James Risner
  • 5,451
  • 11
  • 25
  • 47
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33036454) – configbug Oct 31 '22 at 17:22