0

I am trying to make a PHP script which sends mail from a html contact form, the script doesn't throw any error, but it doesn't send mails. The code is below.

mail.php

<?php
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];
    $email_from = 'stronka@obabie.com'; //<== update the email address
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n" .
    "Here is the message:\n $message" .
    $to = "bruno.kedzierski@wp.pl";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    mail($to, $email_subject, $email_body, $headers);
    header('Location: index.html');
?>

My HTML file:

<div class="container" style="width: 200px; float: left; margin-left: 300px; margin-top: 25px">
    <form action="mail.php" method="post">
        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Imie i nazwisko</label>
            <input type="text" class="form-control" placeholder="Imie i nazwisko " name="name">
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>E-mail</label>
            <input type="email" class="form-control" placeholder="E-mail" name="email" required>
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Numer telefonu</label>
            <input class="form-control" placeholder="Twoj numer" type="tel">
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto; width: 400px;">
            <label>Wiadomosc</label>
            <textarea class="form-control" placeholder="Twoja wiadomosc" style="height: 100px" name="message"> </textarea>
            </label>
        </div>

        <div class="form-group" style="margin-left: auto;margin-right: auto;">
            <label>Plec
                <select style="form-control">
                    <option value="chlop">Chlop</option>
                    <option value="chlop">Baba</option>
                    <option value="inny">Inna</option>
                </select>
            </label>

        </div>
        <div class="radio">
            <label style="display: block;">
                <input type="radio" name="optradio"> kradne</label>
            <label style="display: block;">
                <input type="radio" name="optradio"> nie kradne</label>
        </div>
        <button type="submit" class="btn btn-default" value="submit">Wyslij</button>

    </form>

So I added attribute action="mail.php" and method="post", so that the PHP start when I press submit. Can anybody tell why it doesn't work?

Munim Munna
  • 17,178
  • 6
  • 29
  • 58
Bruno
  • 92
  • 1
  • 8

2 Answers2

0

First of all add this attribute in form tag enctype="multipart/form-data"

then in php code try to send mail without header like this one

mail($to,$email_subject,$email_body);

if still you get error then try to print error like this one after mail function

print_r(error_get_last())

-1

Sending mail isn't rocket science. A simple example that Just Works -

<?php

    $to_address = "someone@example.com";
    $from_name = "From Name";
    $from_address =  "no.reply@example.com";
    $reply_to_name="Reply to name";
    $reply_to_address="reply-to@example.com";
    $subject = "Hello!";
    $headers = "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "From: $from_name <$from_address>\r\n";
    $headers .= "Date: " . date("Ymd H:i:s") . "\r\n";
    $headers .= "Reply-To: $reply_to_name <$reply_to_address>r\n";
    $headers .= "X-Priority: 1\r\n";
    $headers .= "X-MSMail-Priority: High\r\n";
    $headers .= "X-Mailer: Some PHP Script\r\n";
    $message_body = "Message goes here. Be polite and wrap it every 70 lines
    or so, otherwise some mail clients will display very long annoying
    lines of text.  There are functions that can do this automagically
    for you ... or write you own.";
    mail($to_address, $subject, $message_body, $headers);

?>

HOWEVER... the good old days of anyone being able to send mail anywhere from any machine running a script out there on the interwebz are over. Between not wanting to be seen as a source of spam, and not wanting to receive spam, dealing with SPF records and DKIM and ... it becomes a lot to deal with.

In order to really send mail and know it is your code that isn't working vs. the email system you need to have properly configured PHP and properly configured whatever mail server PHP is working with.

I see mail questions come up often enough, with no answers that have no concern for server configuration. I think I may set up a VM that has working mail to itself, post it somewhere, and post a self-answered question on setting up things for dev work when needing to work with mail()...

ivanivan
  • 2,155
  • 2
  • 10
  • 11