Sending HTML form to Mail by PHP

I am trying to send a an HTML form entry via mail using PHP. I have tried but it is not working.
Below is my HTML code:

<form action="assets/php/consultationmail.php" method="post">
<div class="input-field">
<input type="text" name="name" placeholder="Full Name" required>
</div>
<div class="input-field">
<input type="email" name="email" placeholder="[email protected]" required>
</div>
<div class="input-field">
<input type="tel" name="Phone" placeholder="0245678910" required>
</div>
<div class="input-field">
<input type="textarea" name="message" placeholder="Message" required>
</div>
<div class=" input-field">
<button type="submit" value="submit" class="template-btn">Get Consultations
<i class="far fa-long-arrow-right"></i></button>
</div>
</form>

And here’s is the PHP Code I added

<?php
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$email_from='[email protected]';
$email_subject='New Consultation Request';
$email_body='A visitor has request for a consultation.<br>Details are below<br>Name:'.$name.'<br>Email:'.$email.'<br>Phone:'.$phone.'<br>Message:'.$message.'<br>';
$to='[email protected]';    
$to=$to;
$subject=$email_subject;
$message=$email_body;
$headers='From:'.$email_from."\r\n".'Reply-To:'.$email."\r\n".'X-Mailer:PHP/'.phpversion();
mail($to,$subject,$message,$headers);
?>

What am I doing wrong, please?

I would use Swiftmailer (This is what I use - Swift Mailer: A feature-rich PHP Mailer – Documentation – Swift Mailer) or PHPMailer to send HTML as it is much simpler →

        /* Setup swiftmailer using your email server information */
        $transport = (new Swift_SmtpTransport('smtp.yourisp.com', 587, 'tls'))
            ->setUsername("[email protected]")
            ->setPassword(EMAIL_PASSWORD);

        // Create the Mailer using your created Transport
        $mailer = new Swift_Mailer($transport);
        /* create message */
        $message = (new Swift_Message('Verification of Account'))
            ->setFrom(['[email protected]' => 'John Smith'])
            ->setTo([$data['email'] => $data['name']])
            ->setBody($data['message'], 'text/html')
            ->attach(entity: Swift_Attachment::fromPath('https://www.phototechguru.com/assets/images/img-logo-003.jpg'));

        /* Send the message */
        return $mailer->send($message);

LOL - I just found out Swift Mailer isn’t supported any longer → Sending Emails with Mailer (Symfony Docs)

That doesn’t tell us anything. What exactly is “not working”? What exactly are the error messages? What do your error logs say?

Also, stop creating variables for nothing. Copying a variable to another variable without changing anything is pointless.

Sponsor our Newsletter | Privacy Policy | Terms of Service