Hi everybody,
I set up my contact form just fine. I am now working on the php part of it. I do not know php so am learning it little by little as I do this. It doesn't seem to work. This is my code, I was hoping you could look at it and tell me what is wrong. When a user fills out the form, the thank you page will come but but no information from that form will be sent to my email. I only get the test message from the server that I was told I needed to put in.
I am sure something is incorrect but I am so new to php.
When I post the code, I masked some information I didn't want to bring to the forum. For example, recipient, host, username, password, and mail are all fake ones. My correct information is def there in my Dreamweaver program though.
<html>
<head><title>Test email via php</title></head>
<body>
<?
require_once "Mail.php";
$from = "Sender <postmaster@HostingAccountDomain.com>";
$to = "Recipient <myemail@mydomain.com>";
$subject = "This is a test email sent via php";
$body = "This is a test email";
$host = "mail.domain.com";
$username = "username@domain.com";
$password = "password";
$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);
?>
<?php
// This function checks for email injection. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}
// Load form field data into variables.
$first_name= $_REQUEST['first_name'] ;
$last_name = $_REQUEST['last_name'] ;
$email = $_REQUEST['email'] ;
$comments = $_REQUEST['comments'] ;
// If the user tries to access this script directly, redirect them to feedback form,
if (!isset($_REQUEST['email'])) {
header( "Location: contact.html" );
}
// If the form fields are empty, redirect to the error page.
elseif (empty($email) || empty($comments)) {
header( "Location: error_message.html" );
}
// If email injection is detected, redirect to the error page.
elseif ( isInjected($email) ) {
header( "Location: error_message.html" );
}
// If we passed all previous tests, send the email!
else {
mail( "myemail@domain.com, "Email from website",
$comments, "From: $email" );
header( "Location: thank_you.html" );
}
?>
</body>
</html>