Help with PHP form to email

0 Debra A · July 6, 2015
Hi all! I was wondering if I could have some help with my php form. I have the form set up but am having issues. With WinHost, I have to have a authentication. I was told it has to be in one file. I call it process.php. But, when I put that in with my php to extract the fields, it doesn't work. When a user fills out the form, it takes them to the thank you page but I get no email. Here is my code. I changed personal information like my domain, email and password. Any idea what is wrong? Thanks!

 
  <div class="container">
<div class="row">
      <div class="col-xs offset-center col-md-offset-0 col-md-12"><form action="process.php" id="form" class="form" name="form" method="get" enctype="text/plain" accept-charset="UTF-8">
    <h1>Contact Form</h1>
    <div class="content">
        <div class="intro"></div>
        <div id="section0" >
            <div class="field"><label for="Name">Name:</label><input id="Name" name="Name" required type="text"></div>
            <div class="field"><label for="Email">Email:</label><input id="Email" name="Email" required type="email"></div>
            <div class="field"><div class="edit-options"><div class="edit"></div><div class="delete"></div></div><label for="Comments">Comments:</label><textarea id="Comments" name="Comments" wrap="hard"></textarea></div>
            <div class="field"><input id="Submit" name="Submit" type="submit"></div>
        </div>
    </div>
</form>
 
 

process.php
<html>
<head><title>Test email via php</title></head>
<body>
<?php
require_once "Mail.php";
$from = "Sender <email@domain.com>"; 
$to = "Recipient <email@domain.com>"; 
$subject = "This is a test email sent via php"; 
$body = "This is a test email";
$host = "mail.domain.com";
$username = "email@domain.com"; 
$password = "mypassword";
$headers = array ('From' => $from,
  'To' => $to, 
  'Subject' => $subject); 
$smtp = Mail::factory('smtp',
  array ('host' => $host, 
  'auth' => true, 
  'username' => $username, 
  'password' => $password)); 
$mail = $smtp->send($to, $headers, $body);
echo "mail sent";
?>

<?php
$to = 'myemail@mydomain.com';
$subject = 'Comments from website';
$Name = trim(($_POST['Name']));

$Email = trim(($_POST['Email']));

$Comments = trim(stripslashes(htmlspecialchars($_POST['Comments'])));
// build the mail message
$message = "Name: $Name\n\n";
$message .= "Email Address: $Email\n\n";
$message .= "Comments: $Comments\n\n";
$headers = "From: $Name <$Email>";
mail($to, $subject, $message, $headers);
header('Location: thank_you.html');
?>

Post a Reply

Replies

Oldest  Newest  Rating
0 Sachin Kumar · July 8, 2015
Your form has action set to GET and you are calling their values from POST method.
$Name = trim(($_POST['Name']));
>$Name = trim(($_GET['Name']));


$Email = trim(($_POST['Email']));
>$Email = trim(($_GET['Email']));
0 Debra A · July 11, 2015
Hi and thanks so much for getting back to me. I just fixed that and made everything as POST. I am still having issues though. When a user tried to send me a message, they are taken to the thank you page with no issue but I only receive the subject of "comments from my website" and also "comments from my website" in the body. I don't know php very well and am really hoping to get this form up and running but I can't seem to get it to work. Can you see if my php code is correct at all? This is the guide I have to follow for my server with authentication. https://support.winhost.com/kb/a826/how-to-send-email-from-a-php-application.aspx

Here is my code again

my form
 <div class="container">
<div class="row">
      <div class="col-xs offset-center col-md-offset-0 col-md-12"><form action="process.php" id="form" class="form" name="form" method="get" enctype="text/plain" accept-charset="UTF-8">
    <h1>Contact Form</h1>
    <div class="content">
        <div class="intro"></div>
        <div id="section0" >
            <div class="field"><label for="Name">Name:</label><input id="Name" name="Name" required type="text"></div>
            <div class="field"><label for="Email">Email:</label><input id="Email" name="Email" required type="email"></div>
            <div class="field"><div class="edit-options"><div class="edit"></div><div class="delete"></div></div><label for="Comments">Comments:</label><textarea id="Comments" name="Comments" wrap="hard"></textarea></div>
            <div class="field"><input id="Submit" name="Submit" type="submit"></div>
        </div>
    </div>
</form></div>
    </div>
</div>


process.php
<html>
<head><title>Test email via php</title></head>
<body>
<?php
require_once "Mail.php";
$from = "Sender <email@domain.com>";
$to = "Recipient <email@domain.com>";
$subject = "Comments from my website";
$body = "This is a test email";
$host = "mail.domain.com";
$username = "email@domain.com";
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
echo "mail sent";

$to ='myemail@mydomain.com';
$subject = 'Comments from website';
$Name = trim(($_POST['Name']));
$Email = trim(($_POST['Email']));
$Comments = trim(stripslashes(htmlspecialchars($_POST['Comments'])));

// build the mail message
$message = "Name: $Name\n\n";
$message .= "Email Address: $Email\n\n";
$message .= "Comments: $Comments\n\n";
$headers = "From: $Name <$Email>";
mail($to, $subject, $message, $headers);
header('Location: thank_you.html');
?>
0 Sachin Kumar · July 11, 2015
If you are just following the tutorial you gave then I assume you dont have mail.php?
m i ryt?
0 Debra A · July 15, 2015
Hi, thanks for getting back to me. I really don't know PHP too well and I just know that I have to follow that authentication. When I do, I only get a test email and nothing from my form. mail.php doesn't show anything, it just pops up as a tab on DWCC 2015. I think it is on the server.
  • 1

PHP

107,228 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators