Help - Search - Members - Calendar
Full Version: Send mail
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Indigo
Ok, this one is quite simple, but IŽll explain so everybody hopefully can understand.
If there are any errors, please notice me, IŽve translated it from Norwegian, and some variables or other stuff might be wrong.

CODE

<form action="send.php" method="post" enctype="multipart/form-data">
Name:<br />
<input type="input" name="name"><br />
Mail: <br />
<input type="input" name="mail"><br />
Subject: <br />
<input type="input" name="subject"><br />
Message: <br />
<textarea name="message" rows="8" cols="55"></textarea><br />
<input name="submit" type=submit value="Send mail">
<input name="reset" type=reset value="Reset">
</form>


Ok, this is our form, just insert it wherever you want to on your page. It tells the user to insert name, mail, subject and message. Save the file as mail.php or whatever.

Now, on to send.php, the file that does it all (PS: If you choose not to call it send.php, you must change the form action)
CODE

<?php    
if ($_POST[name] && $_POST[mail] && $_POST[subject] && $_POST[message]) {
$name = stripslashes($_POST[name]);
$subject = stripslashes($_POST[subject]);
$mail = stripslashes($_POST[mail]);
$message = stripslashes($_POST[message]);

$date = date("d.m.Y"); // Sets dateformat. This one prints ex: 02.12.2005
$ip = $_SERVER['REMOTE_ADDR']; // Gets the users IP address

$headers = "From: $name";
$sendto = "mymail@mail.com"; // The mail will be sent to this adress, so if you want to receive the mail, you should change this to your own;)
$subject = "$subject";
$message = // The layout of our message
"------------------------------------------------------------------------------------------
Mail sent from (YOUR WEBPAGE)
------------------------------------------------------------------------------------------
Subject: $subject
Sent: $date
From: $name
$nameŽs mail: $mail
$nameŽs ip: $ip
------------------------------------------------------------------------------------------
Message:
$message
------------------------------------------------------------------------------------------
Autogenerated mail from (YOUR WEBPAGE)
Copyright Indigo 2005";

if (mail($sendto, $subject, $message, $headers)) {
echo "Your mail has been sent";}
else
echo "Raaah, something went wrong! CouldnŽt send mail";}            
else {
echo 'Raaah, something went wrong! Make sure you fill out all the fields. All fields are required.';
}
?>


For somebody, this is simple, for others this might be hard. IŽll try to explain some of it:
stripslashes - Makes sure the message, name or the other fields does not contain HTML-code, as this might crash our site or something (which is bad)

By the way: Sorry for language errors, my English really sucks
Also, I wasnŽt sure if this is good enough for Twodded or anything (actually I doubt it), but if you think, tell me:)
If you choose to use this script at your page, IŽd appreciated if you let me know:)
Indigo
No replies, no critique, no aplause, nothing?
rc69
The act of sending mail in PHP is rather simple, and because of that fact there are already hundreds of tutorials out there on how to do it.
But i'll aplaude you on the fact you translated it from Norwegian, and hope that it's here with the original authors permission. I'll also remind you not to double post bigwink.gif
Ruben K
Some things that in my opinion should be changed:

CODE
$headers = "From: $name";

Into
CODE
$header = "From: $name <$email>";


So it actually comes from the person who sent it.

CODE
if ($_POST[name] && $_POST[mail] && $_POST[subject] && $_POST[message]) {
$name = stripslashes($_POST[name]);
$subject = stripslashes($_POST[subject]);
$mail = stripslashes($_POST[mail]);
$message = stripslashes($_POST[message]);


Into:

CODE
if ($_POST['name'] && $_POST['mail'] && $_POST['subject'] && $_POST['message']) {
$name = stripslashes($_POST['name']);
$subject = stripslashes($_POST['subject']);
$mail = stripslashes($_POST['mail']);
$message = stripslashes($_POST['message']);


Proper use of arrays is preferred!

bigwink.gif
Power
Interesting... much different from the way I tried it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.