0

Below is the code for the basic email function.script. It actually uses a form on a website to set the variables in the script above to send an email. But its not working perfectly, please help me to fix it!

 <?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {

  //Email information
  $admin_email = "someone@example.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];

  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);

  //Email response
  echo "Thank you for contacting us!";
  }

  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>

<?php
  }
?>
Kh.Taheri
  • 946
  • 1
  • 10
  • 25
Wraith King
  • 192
  • 2
  • 19

4 Answers4

3

Try this - Add header in your code.

//if "email" variable is filled out, send email
if (isset($_REQUEST['email']))  {

 //Email information
 $admin_email = "someone@example.com";
 $email = $_REQUEST['email'];
 $subject = $_REQUEST['subject'];
 $comment = trim($_REQUEST['comment']);

 $headers  = 'From:' .$email. "\r\n" .
                                    'Reply-To: info@domain.com' . "\r\n" .
                                    'MIME-Version: 1.0' . "\r\n" .
                                    'Content-type: text/html; charset=iso-   8859-1' . "\r\n" .
                                    'X-Mailer: PHP/' . phpversion();

  //send email
  $result = mail($admin_email, $subject, $comment, $headers);

  //Email response
   if($result)
    echo "Thank you for contacting us!";
   else
     echo "Something went wrong.";
   }
Kavita
  • 77
  • 7
0

Try this

mail($admin_email, $subject, $comment, "From:" . $email);

Keval Rathi
  • 978
  • 6
  • 21
  • How would this make it work? All you've done is remove the unnecessary but insignificant double quotes. – Mike Sep 07 '15 at 05:26
0

Which server/environment that you are running on?

If it's Linux/Ubuntu. It might because there is a missing library. Try to install it using the following command.

apt-get install sendmail

But some log/output would be nice. Does the program throw any error/exception?

Toby D
  • 1,465
  • 1
  • 19
  • 31
0

Try it Like this:

//send email

  mail($admin_email, $subject, $comment, "From:" . $email);

and if not sending mail again then you must debug your code step by step and check all values comes or not in variables.

user1990
  • 1,007
  • 1
  • 10
  • 17