Forum Moderators: coopster
Correctly working script
<?php
$from = "From: <sales@mystartup.com> ";
$to = "personalemail@n.com";
$subject = "Hi ";
$body = "TEST";
if (mail($to,$subject,$body,$from)) {
echo "MAIL 001 - OK ";
} else {
echo "MAIL FAILED";
}
?>
When I try and run this from a textbook it sends a true message, but the email never arrives in my inbox:
<?php
$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">
<table>
<tr>
<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.
Lots of issues right here:
$headers = "\"" . $_POST['name'] . "\"" . $_POST['email'];
if(mail($to,$subject,$comments,$headers)) {
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.
<?php
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.
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.