[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP email form

Posted on 2011-03-08
13
Medium Priority
?
255 Views
Last Modified: 2012-05-11
I have this little email script and I am not sure why its not working, I dont get an error on my pages after I submit the form. but the email is just not showing up in my email box I do use google apps if that matters at all. Hopefully someone could help me out:

this is the php on the action page.
<?php

	if(isset($_POST['submit'])) {
		
		$to = "contact@anathallo.net";
		$subject = "A message from the website";
		$name = 'Name:' . $_POST['name'] . "\n";
		$company = 'Company:' . $_POST['company'] . "\n";
		$email = $_POST["email"] . "\n";
		$message = 'Message:' . $_POST['message'] . "\n";
		$headers = "FROM: {$email}\n";
		$headers .= "Reply-To: {$email}\n";
			
		$body = $name . $company . $email  . $phone . $message;
			
		if (mail($to, $subject, $body, $headers)) {
			$response = "Your message has been sent!";			
		} else {
			$response = "Ooops! something happened, please try again.";
		}

	}
	
?>

Open in new window


this is the form on the contact page:
        <form name="contactform" method="post" action="mailer.php">
        <table class="forms">
            <tr>
                <td id="input"><input name="name" type="text" id="name" size="55" value="your name"/></td>
            </tr>
                <td id="input"><input name="company" type="text" id="company" size="55" value="your company"/></td>
            </tr>
            <tr>
                <td id="input"><input name="email" type="text" id="email" size="55" value="your email" /></td>
            </tr>
            <tr>
                <td id="input"><textarea name="message" cols="45" rows="12" id="message">your message...</textarea></td>
            </tr>
            <tr>
                <td align="right" class="submit_btn"><input name="submit" type="image" src="images/btn_send.png" /></td>
            </tr>
        </table>
        </form>    

Open in new window

0
Comment
Question by:RNB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 6

Expert Comment

by:nasirbest
ID: 35073960
use value attribute in submit element like
<input name="submit" value="1" type="image" src="images/btn_send.png" />

Open in new window

0
 
LVL 6

Expert Comment

by:nasirbest
ID: 35073967
also check you spam folder
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35073974
First thing I notice is that "$phone" is not defined in your code.  That may be causing an error that stops the script.  To see errors, include this at the top of your script:

error_reporting(E_ALL);
ini_set('display_errors','On');

I have attached my PHP email demo code.  It is self contained.  Save it as "Email.php" and put your own email info in there and see if it works on your server or if it gives you errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

# some settings of POST vars
if (!isset($_POST['send']))  $send = ''; else $send = $_POST['send'];
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];
if (!isset($_POST['ccText'])) $ccText = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['subjectText'])) $subjectText = ''; else $subjectText = $_POST['subjectText'];
if (!isset($_POST['msgText'])) $msgText = ''; else $msgText = $_POST['msgText'];
if (!isset($_POST['ccText'])) $ccTexth = ''; else $ccText = $_POST['ccText'];
if (!isset($_POST['bccText'])) $bccText = ''; else $bccText = $_POST['bccText'];
if (!isset($_POST['nameText'])) $nameText = ''; else $nameText = $_POST['nameText'];
if (!isset($_POST['fromText'])) $fromText = ''; else $fromText = $_POST['fromText'];

if ($send == "") {
    $title="Test Email Page";
    $announce="---";
}
else {
	if($fromText === "") die("No name!");
  $toText="youremail@yourdomain.com";
	$title="Test Email Page";
  $announce="Your Message has been Sent!";
	$header = "From: ".$fromText."\r\n";
//	$header .= "Cc: ".$ccText."\n";
	$header .= "Reply-To : ".$fromText."\r\n";
	$header .= "Return-Path : ".$fromText."\r\n";
	$header .= "X-Mailer: PHP\r\n";
	$header .= "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
//	ini_set(sendmail_from,$fromText);  
	mail($toText, $subjectText, $msgText, $header, '-f'.$fromText);
//	ini_restore(sendmail_from);
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title><?php echo($title)?></title>
<style type="text/css">
<!-- 
A:link { color: #999999; }
A:visited { color: #999999; }
A:hover {color: #0099ff;}
-->
</style>
<script type="text/javascript">
<!--
function check()
{
var at=document.getElementById("fromText").value.indexOf("@");
var eml=document.getElementById("fromText").value;
var nam=document.getElementById("nameText").value;
var alerttxt="";
var submitOK="true";

if (eml.length < 5 || at == -1)
    {
    alerttxt=alerttxt+"Please enter a valid e-mail address!\r\n";
    submitOK="false"
    //return false;
    }
if (nam.length < 3)
    {
    alerttxt=alerttxt+"Please enter your name.\r\n";
    submitOK="false"
    //return false;
    }
if (submitOK=="false")
    {
    alert(alerttxt);
    return false;
    }

}

// -->
</script>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
if ($send != "") {
   	echo ("To: ".$toText."<br>\r\nSubject: ".$subjectText."<br>\r\n".$msgText."<br>\r\n".$header);
		}
?>

<p><b><font color="#000000" size="5">Test Email</font></b></p>
<font size="4" color="#000000">

<!--   <form method="POST" action="<?php echo($PHP_SELF)?>"> -->
  <form method="POST" action="Email.php" onsubmit="return check();">
    <p><font size="3"><b>Name: <input type="text" name="nameText" id="nameText" size="46"></b></font></p>
    <p><font size="3"><b>Email: <input type="text" name="fromText" id="fromText" size="46"></b></font></p>
    <input type="hidden" name="subjectText" value="Web Mail">
    <p><font face="Arial" size="3"><b>Message Text:</b></font></p>
    <p><font face="Arial" size="3"><b><textarea rows="6" name="msgText" cols="60"></textarea></b></font></p>
    <p><font size="3"><b><input type="submit" value="Send" name="send" style="font-family: Arial; font-size: 12pt; font-weight: bold"></b></font></p>
    <input type="hidden" name="state" value="1">
  </form>
  <b><font face="Arial" size="4" color="#e00000"><?php echo($announce)?></font></b><br><br>

</font>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
nasirbest earned 1000 total points
ID: 35074019
it is better to delete $phone variable. as it is not being used at all

@davebaldwin
As per $phone variable, by default PHP will generate a warning about uninitialized variable and then use null value as $phone variable. which will not create any problem for mail.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35074755
@nasirbest, you may be right.  Or his PHP may be setup differently.  I prefer to eliminate things that may be a problem.  I think my demo code is error free and I know it works on all the hosting that I know about, both Linux and Windows.

At this point we're short on information because the author has not replied to anything yet.
0
 

Author Comment

by:RNB
ID: 35084817
@DaveBaldwin, I tried your email script and it said my email was sent and worked without any erros. However, the email did not show up in my email box.
0
 

Author Comment

by:RNB
ID: 35084924
I also took out the $phone variable and still the form runs and does not send the email to my inbox.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35085454
Ok, who is hosting your web site and what kind of web server are you running on?
0
 

Author Comment

by:RNB
ID: 35086037
I have a (ve) server at mediatemple running linux with centOS and apache
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35086202
Looks like do-it-yourself hosting.  Do you have a SMTP mail server installed?  Sendmail or Postfix or ...?  Without a SMTP server, nothing gets sent.
0
 

Author Comment

by:RNB
ID: 35089556
Do you recommend a way to check to see if i have it installed or how to get it installed?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35089629
I think you need to "Request Attention" and get some others to help with this from the Linux and CentOS zones.  I would have to fire up my server and look it and I don't have time right now.  Off to fix broken computers in a minute.
0
 
LVL 6

Expert Comment

by:nasirbest
ID: 35089636
You have to check [mail function] settings from your php configuration file  /etc/php.ini and make sure thaat sendmail_path is valid

for example in my php.ini file I have

sendmail_path = /usr/sbin/sendmail -t -i

and can confirm this by issuing same command at shell like

/usr/sbin/sendmail emailToAddress@host.com [ENTER]
some text message [ENTER]
. [DOT][ENTER]

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question