Click to See Complete Forum and Search --> : help me - mail form processing
Hey! i'm a beginner with php. and i'm a bit lost.
i made this really simple php mail form.
i have a problem with if/else stuff. i want to check is the header or message empty, and if it is then not send the e-mail. and if everything is ok send the e-mail and in other cases echo an error. i hope you understand what i'm trying to say. just don't know how to build the if/else stuff correctly.
here is the code:
$header = "From: $email";
$subject = "E-mail from your site";
$to = "pixu@hellokitty.com";
$message = $_POST['message'];
if (empty($header) or empty($message)) {
echo 'Your e-mail or message was empty, the message was not sent.';
}
if (mail($to, $subject, $header, $message)) {
echo 'Your message have been sent.';
}
else {
echo 'Error. Your message was not sent.';
}
Hey! i'm a beginner with php. and i'm a bit lost.
i made this really simple php mail form.
i have a problem with if/else stuff. i want to check is the header or message empty, and if it is then not send the e-mail. and if everything is ok send the e-mail and in other cases echo an error. i hope you understand what i'm trying to say. just don't know how to build the if/else stuff correctly.
here is the code:
$header = "From: $email";
$subject = "E-mail from your site";
$to = "pixu@hellokitty.com";
$message = $_POST['message'];
if (empty($header) or empty($message)) {
echo 'Your e-mail or message was empty, the message was not sent.';
}
if (mail($to, $subject, $header, $message)) {
echo 'Your message have been sent.';
}
else {
echo 'Error. Your message was not sent.';
}
I think you mean this piece of code:
$header = "From: $email";
$subject = "E-mail from your site";
$to = "pixu@hellokitty.com";
$message = $_POST['message'];
if (empty($header) or empty($message))
{
echo 'Your e-mail or message was empty, the message was not sent.';
}
else if (mail($to, $subject, $header, $message))
{
echo 'Your message have been sent.';
}
else
{
echo 'Error. Your message was not sent.';
}
(btw, all { and } tags may be removed here)
And here is the metacode (the english translation). I've discovered that may be helpfull in understanding.
If the header and/or the message is empty, then generate an error message.
Else, send the mail and check if it worked. If it worked, give the message that the message was sent, if it didn't work, give the message that it was not sent.
I hope this helps
Hey! thank you so much! that helped me alot! one thing didn't work though. when i left the e-mail empty it still sent it.
but i changed the code a bit, and i think it works now. does it seem correct to you?
$from = $_POST['email']; //NEW STUFF HERE!
$header = "From: $from"; //AND HERE
$subject = "E-mail from your site";
$to = "pixu@hellokitty.com";
$message = $_POST['message'];
if (empty($from) or empty($message))
{
echo 'Your e-mail or message was empty, the message was not sent.';
}
else if (mail($to, $subject, $header, $message))
{
echo 'Your message have been sent.';
}
else
{
echo 'Error. Your message was not sent.';
}
well i guess my change didn't work after all. it says that it sent the message. but when i checked my e-mail it was empty.
what's wrong?
Yes, you're right :rolleyes: . In my example, the header is never empty because it always contains the "From: "-string.
What you have should work.
NogDog
09-20-2005, 05:21 AM
Two things I see: "elseif" is one word, and the order of the args for mail() was wrong, should be:
elseif (mail($to, $subject, $message, $header))
{
echo 'Your message have been sent.';
}
Thank you everyone for the help!
So what should i do to the "from email"? how can i check if someone leaves in blank?
felgall
09-20-2005, 03:49 PM
Compare it with 'From: '
i still have problems with the code! please help.
here's the code
<?php
$to = "pixu@hellokitty.com";
$header = "From: $email";
$subject = "E-mail from your site";
$message = $_POST['message'];
if (empty($from) or empty($message))
{
echo 'Your e-mail or message was empty, the message was not sent.';
}
elseif (mail($to, $subject, $message, $header))
{
echo 'Your message have been sent.';
}
else
{
echo 'Error. Your message was not sent.';
}
?>
problems: it doesn't send it, and when i submit it says 'Your e-mail or message was empty, the message was not sent', even though the fields weren't empty.
NogDog
09-24-2005, 03:29 AM
if (empty($from) or empty($message))
I don't see where you set $from. If it's not set, you'll get that error message. Did you mean for it to be...
if (empty($header) or empty($message))
...?
if (empty($from) or empty($message))
I don't see where you set $from. If it's not set, you'll get that error message. Did you mean for it to be...
if (empty($header) or empty($message))
...?
ah thank you! i forgot the "from" from a previous code.
now it says "your message have been sent"
but when i check my e-mails it hasn't really sent it. what's wrong?