View Full Version : php 'mail' function problems

May 20, 2008, 01:18 AM
ok so i've setup a form to collect a users email, name, phone number, and any comments they may have. What I'd like to do is have a confirmation email sent to them and one sent to me. The problem I'm having is getting the confirmation sent to them. I get all the notifications that someone has submitted information but they dont ever get the confirmation email. If i run the form through localhost and use my email, I can get both the confirmation and the "notification" that someone has submitted information. Oh and as I'm running this kind of as a "demo", I'm running the webserver off of my home computer and os 10.5. I had a friend fill out the form at his house and I did recieve the "notification" but he did not recieve the confirmation. Heh, sorry for the essay but I'm just trying to figure it out. Keep in mind that I am totally new to php so I appreciate any advice that can be givin. Heres the php code:


/* Subject and email veriables */

$emailSubject = 'Thank You!';
$masterSubject = 'Information Request';
$webMaster = 'myemail@email.com' ;

/* Gathering data variables */

$emailField = $_POST ['email'];
$nameField = $_POST ['name'];
$phoneField = $_POST ['phone'];
$commentsField = $_POST ['comments'];

$body = <<<EOD
A request for additional information has been recieved:

Email: $emailField

Name: $nameField

Phone Number: $phoneField

Comments: $commentsField

$body2 = <<<EOD
This email is to confirm that we have recieved the following information:

Email: $emailField

Name: $nameField

Phone Number: $phoneField

Comments: $commentsField

Please do not reply to this email.

$headers = "From: $emailField\r\n" ;
/* $headers .= "Content-type: text/html\r\n" ; */
$success = mail ($webMaster, $masterSubject, $body, $headers) ;
$success2 = mail ($emailField, $emailSubject, $body2, $headers) ;

/* Results rendered as html */

$theResults = <<<EOD
<title>Form Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-color: #f1f1f1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #666666;
text-decoration: none;

<div align="left">Thank you for your interest! Your email will be answered very soon!</div><br>
<div aligh="left">If you are not redirected back within a few seconds, click <a href="index.html">here</a>.</div>
<meta http-equiv="refresh" content="5;url=index.html">

echo "$theResults" ;

May 20, 2008, 05:11 AM
Doesn't seem to be an php issue, but SMTP.
Your machine might not allow outgoing emails.

May 20, 2008, 07:03 AM
Agreed, SMTP does tend to be a potential issue here. In your code you should also check if the mail was 'successfully' sent. Your $success and $success2 variables will become true or false after the mail calls. I quoted successfully before because if the variable is true, that doesn't necessarily mean the email was sent to its destination. There could be a SMTP issue and the mail could return true.

Your code also doesn't seems to do any type of validation or scrubbing. Someone could easily spam this type of form. You should really read up on PHP security to protect these types of forms, especially since it will be sending emails to other people than yourself. For instance, this form could be turned into a way to mass mail people and you'd be at fault. Check this site, they have a PDF that talks about this exact problem and have some solutions. (http://phpsec.org/projects/)

May 20, 2008, 02:40 PM
thanks for the replies. its wierd because if i use my gmail account as the "webMaster" I get both the request for info and the confirmation but no other email works.

as for the validation, i used javascript and put it on the form page. it checks it before it gets sent. sound right? thanks for the link. im gonna read up on it.

and finally, any tips on how to get smtp work on my mbp? i'm trying to host everything here just on my mbp so i can make sure it all works before i pay for hosting. thanks again for the help.

May 20, 2008, 04:22 PM
as for the validation, i used javascript and put it on the form page. it checks it before it gets sent. sound right? thanks for the link. im gonna read up on it.

No, JavaScript isn't anywhere near enough. JavaScript is only good for catching legitimate visitors from filling out the form incorrectly. All spambots that attack forms do so through other means (telnet, perl, etc) than from your site and thus never get exposed to your JavaScript. JavaScript doesn't do anything to stop spambots. Form validation must be done server side. That article linked will describe more details once you get to it so it should become more clear then.

If it's a SMTP issue, it may become resolved if you move it to some host, but that said you should still be able to get it to work from your machine. For myself I have to use my ISP's SMTP server to send emails out. You can contact you ISP to find out if they have such restrictions, though not sure why it was working from your gmail account. There's also other mail functions to use with PHP such as PHP::Pear. You can Google for that and find various tutorials on how to use it. It's not much different from the mail function you're currently using.