0

I don't have any skills in PHP, but I want to create the contact form in my HTML file and I'm creating this codes with web tutorial:

Html:

<form method="post" action="mail.php">
  <label for="name">name</label>
  <input type="text" id="fname" name="name">
  <label for="email">Email</label>
  <input type="email" id="lname" name="email">
  <label for="subject">message</label>
  <textarea id="subject" name="message" style="height:200px"></textarea>
  <input type="submit" value="submit">
</form>

mail.php :

<?php include 'database.php';?>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MyWeb'; 
$to = 'MyEmail'; 
$subject = 'Hello';


$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
}
}
?>

I create a new database and edit this file: database.php:

<?php

function OpenCon()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "myDatabaseName";
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: 
%s\n". $conn -> error);
return $conn;
}

function CloseCon($conn)
{
$conn -> close();
}

?>

and then upload this files into htaccesss directory. but now the form isn't working. what should I do?

Amin
  • 1
  • 3
  • Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Rotimi Mar 17 '18 at 21:46
  • 1
    What does `isn't working` mean? Any PHP error? – pavel Mar 17 '18 at 21:50
  • What's the name of the file in which the html code resides? – dakis Mar 17 '18 at 21:58
  • @panther does not show PHP error – Amin Mar 18 '18 at 17:26
  • @dakis index.php – Amin Mar 18 '18 at 17:27
  • @dakis I'm sorry! – Amin Mar 18 '18 at 17:27
  • No problem. For a start, in order to be able to see on screen which errors are raised, put this code lines as the first ones in your `database.php` page: line 1) `error_reporting(E_ALL);`, line 2) `ini_set('display_errors', 1);`, line 3) `$mysqliDriver = new mysqli_driver();`, line 4) `$mysqliDriver->report_mode = (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. Note that the value in the second line must be set to `0` when your website goes live. – dakis Mar 18 '18 at 19:14
  • I set this codes, but show  when submitted. – Amin Mar 18 '18 at 22:46

2 Answers2

0

Why not modify the mail.php file?

Rather than:

<?php include 'database.php'; ?>
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MyWeb';
$to = 'MyEmail';
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
    if (mail($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

try:

<?php include 'database.php'; ?>
<?php

if ($_POST['submit']) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: MyWeb';
    $to = 'MyEmail';
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if (@mail($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something went wrong, go back and try again!</p>';
    }
}
?>

Although I do not understand what error you are getting, it would be helpful if you can echo the errors and post it. cheers

C Williams
  • 684
  • 9
  • 14
  • thank you... I enter this codes but showed error in line 4. – Amin Mar 18 '18 at 17:30
  • I think I have found the error, run the edited code again and tell us if it worked. cheers – C Williams Mar 18 '18 at 18:43
  • I run the edited code, now showing something went wrong. – Amin Mar 18 '18 at 22:30
  • that is because you have set up the PHP mailer properly, You have to set up PHP mailer and configuring your SMTP server to be able to send emails. Check out how to configure SMTP here on StackOverflow. – C Williams Mar 18 '18 at 23:34
  • Joke: if anyone could just send a mail without providing login credentials using the mail function, then I could just send fake test scores to my parent using my school email address. :), hope you understand what I mean? – C Williams Mar 18 '18 at 23:38
0

Until now it's not clear if your problems reside in the settings for the php mailer, in the php code of your pages, in the encoding of your pages, or somewhere else. So, in order to find out the source of your problems, let's simplify/change the strategy and start with this:

  • Create two new php pages (handlers.php and testmail.php) and make sure that they are "UTF-8" encoded.
  • Copy the following codes into them.
  • Replace the custom email addresses with your own, e.g. find "@todo" in the codes and act accordingly.
  • Let testmail.php run.

Give us a feedback, providing the complete error messages, if any.

handlers.php

<?php

function errorHandler($errno, $errstr, $errfile, $errline) {
    echo '<pre>' . print_r('Error ' . $errno . ' - ' . $errstr . ' in file ' . $errfile . ' on line ' . $errline, TRUE) . '</pre>';    
    exit();
}

function exceptionHandler($exception) {
    echo '<pre>' . print_r('Exception ' . $exception->getCode() . ' - ' . $exception->getMessage() . ' in file ' . $exception->getFile() . ' on line ' . $exception->getLine(), TRUE) . '</pre>';
    exit();
}

set_error_handler('errorHandler');
set_exception_handler('exceptionHandler');

testmail.php

<?php

require 'handlers.php';

error_reporting(E_ALL);
ini_set('display_errors', 1);

$to = 'your-email-here'; /* @todo Replace with a corresponding email address. */
$subject = 'Test email';
$body = 'Hello. This is a test email.';
$headers = 'From: an-email-here'; /* @todo Replace with a corresponding email address. */

$sent = mail($to, $subject, $body, $headers);

if ($sent) {
    echo '<pre>' . print_r('The email was successfully sent.', TRUE) . '</pre>';
} else {
    echo '<pre>' . print_r('An error occurred during your request. Please try again.', TRUE) . '</pre>';
}
dakis
  • 225
  • 1
  • 6
  • 32
  • @Amin You are very welcome. Yep, I thought so myself in the beginning, too. But then I read your comment about "_show  when submitted_". That made me think about some other types of problem. Important is, that you found your solution! Good luck. – dakis Mar 19 '18 at 16:24