homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

$ POST variable causing email not to send
my php script will email me, but not if I include $_

 6:15 pm on Jun 19, 2009 (gmt 0)

I am running on an aphache server and have SMPT on and cannot get information posted in a form to attach to my emails. The server is fine because the tech. ran this script and it works:

Correctly working script
$from = "From: <sales@mystartup.com> ";
$to = "personalemail@n.com";
$subject = "Hi ";
$body = "TEST";

if (mail($to,$subject,$body,$from)) {
echo "MAIL 001 - OK ";
} else {

When I try and run this from a textbook it sends a true message, but the email never arrives in my inbox:

$to = "sales@mystartup.com";
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$headers = "\"" . $_POST['name'] . "\"" . $_POST['email'];
if(mail($to,$subject,$comments,$headers)) {
echo "<h3>Your Message</h3>";
echo "<p><strong>From:</strong>" . $headers . "</p>";
echo "<p> <strong>Subject:</strong>" . $subject . "</p>";
echo "<p> <strong>Comments:</strong>" . $comments . "</p>";
echo "<p> We will contact you as soon as we can.</p>";
} else {
echo "<p>Email failed.</p>";
} ?>

I am trying to gather the variables posted from another page that target this php code. The format is this:

<form action="/target.php" method="post" enctype="application/x-www-form-urlencoded" name="contactus" id="contactus">
<th>Your Name:</th>
<td><input name="name" type="text" id="name" size="40" maxlength="60" /></td>
</tr> ...etc.

I don't code php. Just HTML, CSS and working on Actionscript. Little help here please.



 1:19 pm on Jun 20, 2009 (gmt 0)

Welcome to WebmasterWorld ,bradyhall007.

Lots of issues right here:

$headers = "\"" . $_POST['name'] . "\"" . $_POST['email']; 
if(mail($to,$subject,$comments,$headers)) {

You are using user-supplied input without scrubbing the input first or even validating anything. You are opening the door for spammers to use your script for malicious purposes, and likely getting your server blacklisted.

I highly recommend that you read up on mail [php.net] first before proceeding so you can understand what the mail headers do and how they should be used.


 2:12 pm on Jun 20, 2009 (gmt 0)

Format for that header is:
From: "Your Name" <your.email@example.com>


 10:13 pm on Jun 20, 2009 (gmt 0)

Yes, vincevincevince is correct. Most of the servers, donot allow sending emails from email ids of those external domains.


 1:22 pm on Jun 22, 2009 (gmt 0)

Man, Iím not ready to learn how to configure milters, learn M4 macros to work with a binary program and to code PHP. I am trying to learn Actionscript 3.0 and donít want to chase this rabbit trail to collect customer info. without looking cheesy by having them copy and past my email address to their own email program just to leave a comment or phone number. Is there an easier way to collect customer data on a contact page?


 1:49 pm on Jun 22, 2009 (gmt 0)

easier way? probably not
different way? absolutely

give them a form on the contact us page and drop it into a database. Then you would need to build an interface for people to see these entries in the database.


 8:35 pm on Jun 22, 2009 (gmt 0)

o.k, still trying the php thing. I went to the PHP email forum and have modified the code this far and it still doesní t deliver in my inbox, even though it says sent successfully. I think Iím making this harder than it has to be. As far as security goes my host offers filters that require an alias which will be my biz name and a header_to name that wonít be visible to the world, if these are missing it will automatically delete, but none of that is set yet because I just want to see it work first. Any ideas:

ini_set("SMTP","smtp.mbox.100ws.com" );
ini_set('sendmail_from', 'sales@mybiz.com');
$from = "From: <sales@mybiz.com> ";
$to = "<personal@email.com>"; //optional headerfields
$name = $_POST['name']; //senders name
$yoohoo = $_POST['yoohoo']; //senders e-mail adress
$comments = $_POST['comments']; //mail body
$subject = $_POST['subject']; //subject
mail($to, $subject, $comments, $name, $yahoo); //mail command :)
if(mail($recipient,$subject,$comments,$header)) {
echo "<h3>Your Message</h3>";
echo "<p><strong>From:</strong>" . $headers . "</p>";
echo "<p> <strong>Subject:</strong>" . $subject . "</p>";
echo "<p> <strong>Comments:</strong>" . $comments . "</p>";
echo "<p> We will contact you as soon as we can.</p>";
} else {
echo "<p>Email failed.</p>";
} ?>

P.S. My tech support wont troubleshoot my code, but he has proven with his own code (that doesnít contain variables) that the server email is configured and does work.


 12:18 pm on Jun 23, 2009 (gmt 0)

You have $yoohoo defined, but use $yahoo in your mail command, and $yahoo isn't defined anywhere.

You have the wrong number of parameters in your first mail command (unless you intend to use $yahoo as a means to pass additional flags as command line options, which I don't think is the case).

You have $to in your first mail command, and $recipient in your second -- but recipient isn't defined anywhere.

You use both $header and $headers but don't define them anywhere.

You need to examine both your form input names and your php variables to make sure you're using variables that are actually defined and used.

Then you can do some basic troubleshooting to determine where this is failing, by taking one simple step at a time. Start with using no variables at all:

$to = "youremail@example.com"; //recipient
$subject = 'My test email'; //subject
$message = 'My test message'; //mail body
mail($to, $subject, $message);

Does that work? Okay, now try this:

$to = "youremail@example.com"; //recipient
$subject = 'My test email'; //subject --
$message = $_POST['comments']; //mail body
mail($to, $subject, $message);

Does that work?

No? Echo out $_POST['comments'] to see what you're actually getting for that variable:
echo $_POST['comments'];

Yes, it does work? Now take another baby step, adding in the "extra headers" parameter:

$to = "youremail@example.com"; //recipient
$subject = 'My test email'; //subject --
$message = $_POST['comments']; //mail body
$sendersaddress = $_POST['yahoo'];
$headers = 'From: '.$sendersaddress;
mail($to, $subject, $message, $headers);

And so forth, testing at each step of the way, until you end up with a functioning form.

Important: Don't overlook what coopster said above about scrubbing the POSTed input before using it. But we don't need to get into that now. Get your basics working first.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved