0

I have setup a "contact me" form on my website and it doesn't work. Here is the code :

Form Markup :

<form class="contactform" method="post" action="php/process-form.php">
    <div class="row">
        <!-- Name Field Starts -->
        <div class="form-group col-xl-6"> <i class="fa fa-user prefix"></i>
            <input id="name" name="name" type="text" class="form-control" placeholder="YOUR NAME" required>
        </div>
        <!-- Name Field Ends -->
        <!-- Email Field Starts -->
        <div class="form-group col-xl-6"> <i class="fa fa-envelope prefix"></i>
            <input id="email" type="email" name="email" class="form-control" placeholder="YOUR EMAIL" required>
        </div>
        <!-- Email Field Ends -->
        <!-- Comment Textarea Starts -->
        <div class="form-group col-xl-12"> <i class="fa fa-comments prefix"></i>
            <textarea id="comment" name="comment" class="form-control" placeholder="YOUR MESSAGE" required></textarea>
        </div>
        <!-- Comment Textarea Ends -->
    </div>
    <!-- Submit Form Button Starts -->
    <div class="submit-form">
        <button class="btn button-animated" type="submit" name="send"><span><i class="fa fa-send"></i> Send Message</span></button>
    </div>
    <!-- Submit Form Button Ends -->
    <div class="form-message"> <span class="output_message text-center font-weight-600 uppercase"></span>
    </div>
</form>

process-form.php :

<?php
if (isset($_REQUEST['name'],$_REQUEST['email'])) {
    $name = $_REQUEST['name'];
    $mail = $_REQUEST['email'];
    $message = $_REQUEST['comment'];
    $to = 'redacted@for.privacy';
    $subject = 'Contact From My Website';
    $headers = "From: ".$name." <".$mail."> \r\n";
    $send_email = mail($to,$subject,$message,$headers);
    echo ($send_email) ? 'success' : 'error';
}
?>

AJAX :

$(".contactform").on("submit", function() {
    $(".output_message").text("Loading...");

    var form = $(this);
    $.ajax({
        url: form.attr("action"),
        method: form.attr("method"),
        data: form.serialize(),
        success: function(result) {
            if (result == "success") {
                $(".form-inputs").css("display", "none");
                $(".box p").css("display", "none");
                $(".contactform").find(".output_message").addClass("success");
                $(".output_message").text("Message Sent!");
            } else {
                $(".tabs-container").css("height", "440px");

                $(".contactform").find(".output_message").addClass("error");
                $(".output_message").text("Error Sending!");
            }
        }
    });

    return false;
});

Error message from the ajax shows and I don't receive any mail. This code should be correct but I read that I needed an smtp configuration ? I can setup such a thing in my google business email but I don't know how and how to implement it on the website.

  • 1
    I would recommend using the tried and tested PHPMailer-library instead of the low level `mail()` function. Specially if you're planning on using Google's email service. You should then look into using Google's OAuth API to send the email (which PHPMailer supports) since using Googles SMTP requires you to configure the google account to "allow less secure apps", which Google also will annoyingly disable for you from time to time. – M. Eriksson Oct 12 '21 at 21:54

2 Answers2

0

While you are using mail() function, You should configure STMP in php.ini file.

But you can use PHPMailer especially when dealing with Google email service, Because Google doesn't allow less secure apps by default.

Look for this example below using PHPMailer:

$mail = new PHPMailer;
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';

$mail->addAddress($_REQUEST['email'], $_REQUEST['name']);
$mail->addCC('cc@example.com');

$mail->Subject = 'Contact From My Website';
$mail->Body = $_REQUEST['comment'];

echo $mail->send() ? "success" : "error"; 
  • Your answer and the posted example is very incomplete. Even with PHPMailer, you will have the same issue with Google if you're using SMTP (which you haven't set up), or you would need to use their XOAuth solution (which you also aren't using). So the above example code has the same issues as the OP's original code. – M. Eriksson Oct 12 '21 at 22:16
  • Code is not for copy and pasting, I managed to show him how that's easy to send Mails with PHP Mailer rather than mail() – Abdelrhman Said Oct 13 '21 at 05:52
  • An answer should be a solution. Posting an incomplete example and an incomplete explanation (no explanation what "less secure apps" is or what problem PHPMailer solves that mail() doesn't) will just confuse the people reading it. They will test the code, realize it's not working but have _no idea_ why not, resulting in them coming back here posting a new question. How is that helpful? – M. Eriksson Oct 13 '21 at 06:23
  • 1
    That's my first time solving problems on stack overflow, I will save your notes, Thanks a lot. – Abdelrhman Said Oct 13 '21 at 11:53
0

PHP's mail() function does need some configuration in php.ini file in some cases, like using Windows operating system. It is documented in php.net mail documentation page. Either configure the file, if your load will not be hundreds of mails, or use some of the public available libraries for SMTP connection. They provide much more flexibility and usually provide simple api.

On windows

You need to have set up the php.ini

[mail]
SMTP = "your-mail-server-address"
smtp_port = 25

This answer should provide you more than enough explanation and info.

on Ubuntu / Debian

You have to have configured your sendmail client so it can send emails in your network and specified as sendmail command in sendmail_path directive of php.ini file

Alesseon
  • 51
  • 1
  • 4