I want to send email with PHP

I want to send an email with PHP when the user completes the HTML form, then sends the form information via email. I want to do this from the same site view script with the template.

I found this code, but the message was not sent.

< ?php

if (isset($_POST['submit'])) {

$to = $_POST['email']; 

$subject = $_POST['name'];

$message = getRequestURI();

$from = "[email protected]";

$headers = "From:" . $from;

if (mail($to, $subject, $message, $headers)) {
    echo "Mail Sent.";
else {
    echo "failed";

} ?>

You need to setup your outgoing mail service in your php.ini if you want to use the core mail() function.

Otherwise, use a library to configure an outgoing SMTP server. This is a nice lib: https://packagist.org/packages/nette/mail

edited 1d ago

Use mailgun free for 10 000 mail by month with a great service and an easy library.