PDA

View Full Version : php mail script



cheatpark
January 25th, 2002, 16:21
Can someone give me a url of a php script that sends an e-mail by filling out form details and then clicking on submit. I've tried loads and for some reason I can't get any to work. I am also looking for a mailing list script too. Thanks in advance.

megapuzik
January 25th, 2002, 16:27
WHY to download a script ??? dont you want to learn to do it by your-self ?? (its really easy) .....
I dont mind to go with you step by step....:)

cheatpark
January 25th, 2002, 16:29
I like to understand it myself so if you don't mind please explain it step-by-step.

megapuzik
January 25th, 2002, 16:34
Ok, we will do a simple script that will take the following info in the form :

full name
email adrress
message
<->
To do that, the first thing that you need to do is to make an HTML form with these 3 objects...
I asume that you know html so it wont be any problem (just a simple html form with 3 field, name , smail , msg)

write here the code.

keith
January 25th, 2002, 16:56
private message or email me. i have a custom script written [see it in action here (http://www.weezerfans.com/rock/contact.shtml)]. you can pass it a url to forward to once it's sent the message [similar to matt wright's formmail]

cheatpark
January 25th, 2002, 16:58
<form name="form1" method="post" action="">
<p>Full name:
<input type="text" name="name">
</p>
<p>E-mail:
<input type="text" name="email">
</p>
<p>Message
<textarea name="message"></textarea>
</p>
</form>

megapuzik
January 25th, 2002, 17:28
OK....now, before the coding, you should know that the action in the form, is the link to the php script..


<form name="form1" method="post" action="mail.php">

Ok the PHP :
Well, before I will explain how the script work, I just want to say that the name of the form element, for example "name" will be pased to the script like a variable : $name...

the script..
like i allready said, every form element is parsed like a var name, so in the first lines, we will do a simple checl if al the variables are set (mean, if the user fill the forms)


<?php
if(!$name || !$email || !$message)
{
print " One of the field is missing ";
}


we did a simple thing :
if the forms element are NOT set (there is a ! operator to check if its set or not)
then this will print an error message

ok ??

now we will will need to send the mail right ?? well, its simple.....we just need to use the mail() finc.
mail("your@email.com", "Subject", "message");

this is the syntex.
To use with our variables, we can do something like this :


$mail_msg = "from ::: $name
E-mail ::: $email
message ::: $message";

we should place this code at the top of the script.
this code putting al our variables (that parsed from the form) into 1 variable that named $mail_msg, and this var will contain all our info...
supose the we typed these following data in the form :
name = megapuzik
email = dima@signup.co.il
message = hi...

that we will get these email :
from ::: megapuzik
E-mail ::: dima@signup.co.il
message ::: hi...
OK ???

now, all we need to do, is to add the "else" to the if statment to send the mail (else mean that if the variables ARE set [the user typed some info to it])
this will look like this :


else
{
mail("your@email.com","here is the subject", $mail_msg);
print "the email sent.";
}


we used the var mail_msg to contain all the info and send it...
well, thats it, here is the complete code :


<?php
$mail_msg = "from ::: $name
E-mail ::: $email
message ::: $message";
if(!$name || !$email || !$message)
{
print " One of the field is missing ";
}
else
{
mail("your@email.com","here is the subject", $mail_msg);
print "the email sent.";
}
?>

save this script to a file, and call it mail.php
and dont forget in the html form, to set the action to mail.php

thats it, we done a simple php send mail script
:)

cheatpark
January 25th, 2002, 17:47
It said failed to connect.

cheatpark
January 31st, 2002, 11:35
It now sends me an e-mail but It sends me an e-mail like this:

from: me@localhost.com
subject: subject
to: webmaster@cheatpark.net

Then there is a blank message. Why is this? By the way my site is on a server with more than one user so this may cause the problem. My host says that I should find a way or overriding it in my scripts.

hipcat
January 31st, 2002, 13:21
Are you sure that the variable $mail_msg actually contains data?

Add the line:

print $mail_msg;

underneath the other print statement. This will print the body of the email to the screen once you've sent it. If this doesn't print anything, then for whatever reason, the $mail_msg variable is empty.

Should help you to narrow down the problem

Hope this helps

cheatpark
January 31st, 2002, 13:33
Warning: Undefined variable: mail_msg in D:\hosting\usrv4283j\cheatpark.net\cgi-bin\mail\mail.php on line 8
the email sent.
Warning: Undefined variable: mail_msg in D:\hosting\usrv4283j\cheatpark.net\cgi-bin\mail\mail.php on line 10

cheatpark
January 31st, 2002, 13:42
Its ok it works now. For those who were interested I added the code:

$mail_msg="$name,$email,$message";

under the lines

else
{

keith
January 31st, 2002, 19:45
you ever get the one i emailed you? just curious as to whether it went through or not...

i'm also working on a stand-alone mailer at http://amail.slowjim.com

it won't be an anonymous mailer though, and i'm going to change the name as to not reflect anonymous mailing :cool:

just something to pass the time...

cheatpark
January 31st, 2002, 19:56
Sorry, my outlook express messed up and the e-mail (attatchment) got deleted. If you want to you can send it again but I don't mind because I got one working now anyway. Also making your own script is better because you gain a lot of php knowledge like I did tonight.

keith
January 31st, 2002, 19:57
true...

well, if you still want to take a look at it, it's at http://www.weezerfans.com/phpform for the time being

that's kind of simple though... this other script i'm writing is much more advanced... checks the validity of emails, makes sure all the fields are filled out properly, let's sender review before submitting, prevents other from stealing your form for use on other sites... etc... etc...

custom scripting is definitely the best way to learn... but i might as well say the sun is the center of our solar system, duh.

cheatpark
January 31st, 2002, 20:03
Thanks. I shall analyse this code.

cheatpark
January 31st, 2002, 20:04
Also mine is in action at http://www.cheatpark.net/advertising/signup.htm