Jump to content


Photo
- - - - -

Send mail


  • Please log in to reply
4 replies to this topic

#1 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 02 December 2005 - 04:13 AM

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.

<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)
<?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 = "[email protected]"; // 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:)

#2 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 26 December 2005 - 06:58 PM

No replies, no critique, no aplause, nothing?

#3 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 December 2005 - 12:29 AM

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 :blush:

Edited by rc69, 27 December 2005 - 12:30 AM.


#4 Ruben K

Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 27 December 2005 - 03:48 AM

Some things that in my opinion should be changed:

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

So it actually comes from the person who sent it.

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:

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!

:blush:

#5 _*Power_*

_*Power_*
  • Guests

Posted 10 January 2006 - 01:10 AM

Interesting... much different from the way I tried it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users