Send email using php and html forms

Go To StackoverFlow.com

1

I have a form that I want to use to collect user information. I have a php that gets the information and stores in the variable. When user clicks submit button the form gest submitted and is successfully redirected to the thank you page. Bu for some reason I am not getting the email as expected. Can someone explain where I am doing things wrong.

Below are my HTML and PHP file. I will add javascript validation later. At this moment I just want to make the form submission working.

PHP

<?php

//This page should not be accessed directly. Need to submit the form.
if(!isset($_POST['submit'])){
echo "ERROR. This page cannot be accessed directly";
}

//Variables from the form
$name               = $_POST['fullname'];
$ageGroup           = $_POST['ageGroup'];
$gender             = $_POST['gender'];
$visit              = $_POST['visit'];
$purposeOfVisit     = $_POST['purposeOfVisit'];
$otherReferrer      = $_POST['otherReferrer'];
$member             = $_POST['member'];
$socialMedia        = $_POST['socialMedia'];
$difficulties       = $_POST['difficulties'];
$easeOfUse          = $_POST['easeOfUse'];
$whatDifficulties   = $_POST['whatDifficulties'];
$specialCondition   = $_POST['specialCondition'];
$browser            = $_POST['browser'];
$preferredFormat    = $_POST['preferredFormat'];
$contentsForWebsite = $_POST['contentsForWebsite'];
$oldContents        = $_POST['oldContents'];
$expectations       = $_POST['expectations'];

$message = "Fullname:"; // will compose later

// Compose email
$email_from = "subash.adikari@gmail.com";
$email_subject = "Infinity User Questionnaire";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".

$to = "subash.adhikari@gmail.com";

$headers = "From: $email_from \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);

//done. redirect to thank-you page.
header('Location: thankyou.html');


if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

// Function to validate against any email injection attempts
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;
  }
}
?>     

you can download index.php here if you want.

Thanks a lot.

2012-04-05 23:26
by Subash
is the site on a live website or on your localhost - Kristian 2012-04-05 23:29
You don't prevent the code from loading after you determined there is nothing in $_POST. You only add a warning message. ALWAYS enable error_reporting - PeeHaa 2012-04-05 23:35
its on my local host - Subash 2012-04-05 23:36
Can you define not as expected - Starx 2012-04-05 23:41
@Starx I was expecting the email to be sent. But I am not getting any email while I submit the form - Subash 2012-04-05 23:42
Have you configured the mail function in your php.ini correctly? Refer to my answer for more information - Xenon 2012-04-05 23:53
First make sure there is a mail server running on your host, or php.ini is pointing to one. Second, maybe you will want to take a look at PEAR's Mail class, which can handle SMTP relaying and other nice features: http://pear.php.net/package/Mai - Victor Nițu 2012-04-06 00:13
Please note that on a Windows host, PHP's built-in mail() function will most likely fail, as I experienced for various reasons on WS hosts... Yet another bunch of MS bugs - Victor Nițu 2012-04-06 00:15


0

The problem was with my hosting service. I had to give them a call and they fixed it for me.

2012-04-23 09:27
by Subash
Please mark this answer as the accepted answer so that the question does not show up as 'unanswered' - Xenon 2012-04-25 10:24


3

Since this is on your local machine, you need to first configure mail() in php.ini. For example, you might configure it to use your ISP's SMTP server:

[mail function]
SMTP = smtp.isp.net
smtp_port = 25
sendmail_from = me@isp.net

For more information, refer to the PHP Documentation.

EDIT: I just tested your code on one of my servers, and the email sent as expected. This further leads me to believe there's something wrong with how you've configured the mail() function.

2012-04-05 23:45
by Xenon


0

I suggest for you use PHPMailer from http://phpmailer.worxware.com/ to send mails. With PHPMailer you can track the errors, send mails in HTML and TEXT, change encoding and manage the word warpers.

Also, as someone said, your script doesn't prevent it from being executed directly and i think that headers must be finished with just new line character "\n" and not return carriage "\r".

2012-04-05 23:42
by Alexandre Leites
While PHPMailer is good, it doesn't answer the original question. Besides, PHPMailer still uses mail().. - Xenon 2012-04-05 23:52
Ads