Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help me with PHP mail form please!
Old 08-28-2007, 07:51 PM Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
I am very new to PHP and am writing a simple mail form script.
I need help with certain things that I am unsure of.
Here is the code:
Code:
$EmailFrom = trim($_POST["EmailFrom"]);
$EmailTo = "owen.timothy@gmail.com";
$Name = trim($_POST["Name"]);
$QuestionorComment = trim($_POST["QuestionorComment"]);

mail($EmailTo, "Email from website", $QuestionorComment) {
    if(!preg_match("@.");
        return false;
    elseif(return false);
    
    else;
}
As you can see there are parts left out due to the fact that I just don't know what to put in there.
I am pretty sure I am using preg_match wrong.
I am trying to make it so that if no @ or . are detected in the email field then it is valid and sends you to to the "form has been sent" page.
And if there are no @ or . detected it returns false and you are sent to the error page.

Would I use something like this?
Code:
if ($EmailFrom != preg_match("@.");
    blank??("error.html");
I am taking wild guesses at this point. I am a complete novice.
Please help!
Thanks a lot.

Last edited by soap; 08-28-2007 at 07:59 PM..
soap is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-28-2007, 08:06 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Code:
$EmailFrom = trim($_POST["EmailFrom"]);
$EmailTo = "owen.timothy@gmail.com";
$Name = trim($_POST["Name"]);
$QuestionorComment = trim($_POST["QuestionorComment"]);
 
mail($EmailTo, "Email from website", $QuestionorComment) {
    if(!preg_match("@.");
        return false;
    elseif(return false);
 
    else;
}
Okay that whole preg_match thing as far as i know completely wrong..

You should do ALL checking and processing of fields BEFORE the mail() function

then if theres no errors in the formatting and hat and all your checks validate, then run mail()

else show error.html

something like this should do what you need:

PHP Code:
<?
function validate_email($email)
{
$email trim($email);
if(
(
strpos($email"@") === FALSE) ||
(
strpos($email".") === FALSE) ||
(
strpos($email" ") != FALSE) ||
(
strpos($email"@") > strrpos($email"."))) 
{
$valid_email 0;
}  
else {
$valid_email $email// If email IS valid the email becomes $valid_email if it isnt it become 0 (false)
}
}
function 
clean_input($input)
{
if(empty(
$input))
{
$input_error++; //will add 1 to $_input_error later on we will check if $_input_error is > 0 if so dont send
}
else {
$input trim($input);
$input htmlentities($input); // Removes any HTML tags etc within the input
}
}        
 
 
$to 'owen.timothy@gmail.com';
$from 'owen.timothy@gmail.com'// I would advise you chnage this to a email one your server messages will be sent from this address
$email validate_email($_POST['email']);
 
$Name clean_input($_POST['Name']);
$message clean_input($_POST['QuestionorComment']); //You should really think about using short names like message. :D
 
$subject $Name.' Has contacted you via your online form.'// this is the subject of the email
# The following makes the BODY of your email. 
$body $Name.' Sent you a email!';
$body.= '<br /> The message they left: <br />';
$body.= $message;
$body.= '<br />'$Name.'\'s Email is: '.$email.'<br />';
$body.= 'This email was generated automatically via a PHP emailer created by <a href="http://dansgalaxy.co.uk">Dansgalaxy</a> :D';
$body.= '<br /><br /><br />';
// Feel Free to remove the above line :) all it will do is send "This email was automatically via a PHP emailer created by Dansgalaxy"
#Now we are constructing out headers for the email
$nl "\r\n" //new line and return
$headers '';
$headers.= "From: ".$Name"<".$from.">" .$nl;
$headers.= "Reply-To: "$Name"<".$email.">" .$nl
//this should should me when you reply to a message it will send it to the users and NOT your email which is was sent from.
### This should try amd help the emails not get sent to junk. ###
$headers.= "Message-ID: <".time(). "-" .$from.">".$nl;
$headers.= "X-Mailer: PHP v".phpversion(). $nl;
$headers.= "Content-Type: text/html; charset=iso-8859-1".$nl;
 
if(
$valid_email == OR $input_error 0)  //if email returned false for validity or there was a input error dont send show error message.
{
echo 
'ERROR!! Email <strong>NOT</strong> sent!';
}
else {
mail($to$subject$body$headers);
echo 
'Email successfully Sent!';
}
?>
ok i havent tested this script but i just knocked it up and i have to say im quite pleased with it

this is much more than the basic, and u can easily edit it and add extre fields and stuff which is why i made the clean_input and validate_email functions os you can use them again and again.

please test this and if theres any problems feel free to contact me!

Talkupation will be MUCH apprieciated... as just got ear full for being one PC at 2PM :P

Thanks,
Dan

Last edited by dansgalaxy; 08-28-2007 at 09:56 PM.. Reason: Fixed Error
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-28-2007, 08:13 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
I can't wait!
soap is offline
Reply With Quote
View Public Profile
 
Old 08-28-2007, 09:05 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
I edited it So look above!

Talkupation apprieciated - and if you feel to enclined feel free to offer a doantion to myself (paypal email webmaster@dansgalaxy.co.uk ) hehe

(you think im despurate?... no :P)

Dan

Also you can check out http://uk2.php.net/mail will give you a some info on the mail() function
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 08-28-2007 at 09:07 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-28-2007, 09:10 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
Thanks so much.
soap is offline
Reply With Quote
View Public Profile
 
Old 08-28-2007, 09:18 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Happy to help
and thanks for the TP

Does it all work as it should? can i see it live lol i think gonna end up using that myself Lol better than what im using
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-28-2007, 09:25 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
Working on it right now. It doesn't work immediately. It only gives me a blank /contact.php page. Trying to debug.

Here is where it is hosted:
http://www.loveliessleeping.net/contact.html
soap is offline
Reply With Quote
View Public Profile
 
Old 08-28-2007, 09:54 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
shoudl be quite simple,

bunk all my code above into:

[php]
<?php

if($_POST['submit'])
{
ALL MY CODING
}

else {
echo 'form coding';
}
?>

you have to add name="submit" to the submit button on the form.

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-28-2007, 09:55 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
sorry just spotted one of my errors

$body.= '<br /><br /><br />;
should be
$body.= '<br /><br /><br />';
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-28-2007, 10:03 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
Hmm, I'm still getting a blank /contact.php page.
soap is offline
Reply With Quote
View Public Profile
 
Old 08-28-2007, 10:11 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
Something confusing me. In: (strpos($email, "@") === FALSE),
what does === stand for?
I thought it was ==
soap is offline
Reply With Quote
View Public Profile
 
Old 08-29-2007, 07:57 AM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
well they all mean differant single = is assigning

== is equal to
and === i think basically means its the same type

so i think like if you have

$var = "puppy";
$varibles['var'] = "puppy";

i think so if you had

$var == $variables['var']
i think it would return true becasue like the contents are the same.

but if u had

$var === $varaibles['var']

it would return false because the second is a array.

i probally havent explained it propperly.

i just had a google and cant find anything to explain it its in a book i have soemwhere so when i find it ill post a fuller explination.


to be honest that little snippet for email validation i got out of a PHP book i borrowed ages ago and have used it for email validation ever since.

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-29-2007, 02:19 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
I see. Still having trouble making this work.
Http://www.loveliessleeping.net/contact.html
Can you figure it out?
soap is offline
Reply With Quote
View Public Profile
 
Old 08-29-2007, 03:02 PM Re: Help me with PHP mail form please!
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hi, okay
Do you have MSN i can get this working faster that way.

ill have a look and try and figure out whats wrong.

EDIT:

mines Dansgalaxy@hotmail.co.uk is mine
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 08-29-2007 at 03:07 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-29-2007, 04:14 PM Re: Help me with PHP mail form please!
Super Talker

Posts: 131
Trades: 0
Hey so I got most of it debugged, was just some syntax errors.
Code:
//validation
if($_POST['submit']) {
    function validate_email($email) {

$email = trim($email);
if(
    (strpos($email, "@") === FALSE) ||
    (strpos($email, ".") === FALSE) ||
    (strpos($email, " ") != FALSE) ||
    (strpos($email, "@") > strrpos($email, "."))) 
        $valid_email = 0;
  
else
    $valid_email = $email; // If email IS valid the email becomes $valid_email if it isnt it become 0 (false)
}
}

function clean_input($input)
{
    if(empty($input))
        $input_error++; //will add 1 to $_input_error later on we will check if $_input_error is > 0 if so dont send
    else {
        $input = trim($input);
        $input = htmlentities($input); // Removes any HTML tags etc within the input
}
}        

$to = 'owen.timothy@gmail.com';
$from = 'owen.timothy@gmail.com'; // I would advise you chnage this to a email one your server messages will be sent from this address
$email = validate_email($_POST['email']);

$Name = clean_input($_POST['Name']);
$message = clean_input($_POST['message']); //You should really think about using short names like message. :D

$subject = $Name.' Has contacted you via your online form.'; // this is the subject of the email
# The following makes the BODY of your email. 
$body = $Name.' Sent you an email!';
$body.= '<br /> The message they left: <br />';
$body.= $message;
$body.= '<br />'. $Name.'\'s Email is: '.$email.'<br />';
$body.= '<br /><br /><br />';

#Now we are constructing out headers for the email
$nl = "\r\n"; //new line and return
$headers = '';
$headers.= "From: ".$Name. "<".$from.">" .$nl;
$headers.= "Reply-To: ".$Name. "<".$email.">" .$nl;
//this should should me when you reply to a message it will send it to the users and NOT your email which is was sent from.
### This should try amd help the emails not get sent to junk. ###
$headers.= "Message-ID: <".time(). "-" .$from.">".$nl;
$headers.= "X-Mailer: PHP v".phpversion(). $nl;
$headers.= "Content-Type: text/html; charset=iso-8859-1".$nl;

if($valid_email == 0 OR $input_error > 0)  //if email returned false for validity or there was a input error dont send show error message.
    echo 'Form NOT sent';
    
    else {
    mail($to, $subject, $body, $headers);
    echo 'Form successfully Sent!';
    }
Problem I'm having now is no matter the input, i get the echo 'Form NOT sent'

yes, i have msn messenger

Last edited by soap; 08-29-2007 at 04:22 PM..
soap is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help me with PHP mail form please!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47995 seconds with 12 queries