-2

So I'm trying to create an email form for my Contact Us Page. my problem is i dont have any idea on how does this email form i have this code for my Form As Shown Below:

<form method = "post" action = "contactus.php" enctype="multipart/form-data">
<div class = "emailform">
    <input type = "text" placeholder = "Name" name = "name" style="width:450px; padding:5px;" required><br>
    <input type = "text" placeholder = "Email" name = "email" style="width:450px; padding:5px;" required><br>
    <textarea name = "message" placeholder = "Message" style = "width:450px; padding: 5px; height: 155px;" required></textarea><br>
    <button type = "submit" name = "send">Send</button>
</div>
</form> 

it will be really helpful if someone answer by the way I'm only using notepad++ for editing my codes. and im also a newbie :)

Thank you in advance

2

2 Answers 2

0

try this in your contactus.php page.

<?php


$to = "[email protected]";
$subject = "Your subject";
$txt = $_POST['message'];
$headers = "From: ".$_POST['email'];

mail($to,$subject,$txt,$headers);
?> 
5
  • So i did exactly what you said but i got some php errors like unidentified index: message and unidentified index: email. and also this error: Oct 19, 2015 at 9:26
  • Warning: mail(): Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\wamp\www\Villa d El-lita\contactus.php on line 69 Oct 19, 2015 at 9:26
  • this mail function will work only on live servers and not in localhost. Oct 19, 2015 at 9:27
  • submit the form with the values from your above form. you will not recieve undefined index errors Oct 19, 2015 at 9:28
  • Oh i see thanks for the help :) Oct 19, 2015 at 9:31
0

Add below code in your "contactus.php" file.And replace "[email protected]" with your email address.Please put "contactus.php" in the same directory where your form file is placed.

<?php
function clean($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if(isset($_POST['name'],$_POST['email'],$_POST['message']) && $_POST['name'] != '' && $_POST['email'] != '' && $_POST['message'] != '') {
    foreach($_POST as $key = > $value)
        $_POST[$key] = clean($value);

    $to = '[email protected]';
    $from = $_POST['email'];
    $subject = 'Customer support needed';
    $message = 'Name='.$_POST['name'].'  Message='.$_POST['message']; 


    // Sending email
    if(mail($to, $subject, $message)){
        echo 'Your mail has been sent successfully.';
    } else{
        echo 'Unable to send email. Please try again.';
    }
}
?>
3
  • Warning: mail(): Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\wamp\www\Villa d El-lita\contactus.php on line 83 Oct 19, 2015 at 10:12
  • i think the code works i jst need to figure out how to fix this error Oct 19, 2015 at 10:13
  • Yes,This is working code.You just need to look at your server configuration.And if done, please accept this answer. Oct 20, 2015 at 9:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.