-4

I am about to write a little html/php script. But I am not able to send data from the HTML form input to any email address or into any text file. I was already searching for possible solutions but nothing worked for me. The Browser replayed the php script. But no mail has been send. Any help will be appreciated. Thank you.

Razia sultana
  • 2,168
  • 3
  • 15
  • 20
Simon l.
  • 451
  • 1
  • 4
  • 4

3 Answers3

0

I recommend you use PHPMailer his use is very easy

CrsCaballero
  • 2,124
  • 1
  • 24
  • 31
0

I think your issue is how to handle form data with some code, so you may be able to send an email or write form data to a file. This is where you see the difference between client-side and server-side. HTML is a language to describe documents, here your form: the text input name is going to describe a name, the form will send its data within the POST method, and so on. The file describing HTML is processed in your browser. And your browser don't send an email or write data... That's why you should use a server-side language such as PHP to get things done. PHP is great to help you process data and behave on different event... In your case, great for receiving data, analyze data and then send data through mails or save data into a file.

So now you may want to understand how to do so... Mail are a little tricky since you may need to configure things such as mail server, authentification and so one. Maybe a good solution is to try mails with a Google account or something like that... When it will be done, you may simply send an email like so:

<?php

$to      = 'your@email.here';
$subject = 'Mail test';
$data    = $_POST['name']; // if a `name` field exist and your form send its data through POST method
mail($to, $subject, $data);

Writing things to a file is simpler, it only request permissions to read and/or write a file.

<?php

$file = 'path/to/file';
file_put_contents($file, $_POST['name'] . ' for example');

So this is globally everything:

index.html HTML file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Form</title>
    </head>
    <body>
        <form action="process.php" method="post">
            <input name="name" type="text" placeholder="Name" />
            <input type="submit" value="Process">
        </form>
    </body>
</html>

and process.php PHP file

<?php

/**
 * Testing data
 */

if (!isset($_POST['name'])) {
    die('No value `name` found');
}

/**
 * Configuring process
 */

$to      = 'your@email.here';
$subject = 'Mail test';
$data    = $_POST['name'];

/**
 * Saving data
 */

$res = file_put_contents(
    'data.txt',
    $data."\r\n"
);

if ($res !== false) {
    echo 'data saved'.PHP_EOL;
} else {
    echo 'error while saving data'.PHP_EOL;
}

/**
 * Sending email
 */

$res = mail(
    $to,
    $subject,
    $data
);

if ($res === true) {
    echo 'mail sent';
} else {
    echo 'error while sending mail'.PHP_EOL;
}

I suggest you to read mail() and file_put_contents() documentations to understand their behavior in case there are errors... :)

Alexandre
  • 474
  • 2
  • 14
0

Do not forget the action and method attributes in the <form> tag.

contents of html file

<form action="send.php" method="POST">
    <input type="text" name="name" placeholder="Typ your name..." />
    <input type="email" name="from" placeholder="Typ your e-mailaddress..." />
    <textarea name="message" placeholder="Typ your message..."></textarea>
    <button type="submit">Send E-mail</button>
</form>

contents of send.php

<?
if(isset($_POST)) {
    $name = $_POST['name'];
    $message = $_POST['message'];
    $from = $_POSST['from'];

    if(!empty($name) && !empty($message) {
        $subject = 'message from '.$name;

        $headers = "From: " . strip_tags($from) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($from) . "\r\n";
        //$headers .= "CC: susan@example.com\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        // @SEE: http://php.net/manual/en/function.mail.php
        if(mail('[YOUR-ADDRESS]', $subject, $message, $headers)) {
            echo 'Thx 4 msg!';
        }
        else {
            echo 'Oh nooos, The msg was not send.';
        }
    }
    else {
        echo 'You should provide the fields with some data..';
    }
}
?>

One should first sanitize the user input obviously.

Brainfeeder
  • 2,604
  • 2
  • 19
  • 37