Weborum Webmaster Forum > [Beginner] Web Mailer
Help - Search - Members - Calendar
Full Version: [Beginner] Web Mailer
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
Timo
You don't need MySQL for this. :-) and I'd like to officially say I hate javascript with a passion and I never want to touch regular expressions again unless I have to. :-)

Rather small and easy to understand script...

CODE

<script language="JavaScript" type="text/javascript">
function check(form){

 if (form.name.value == "") {
   alert("Please enter your name.");
   form.name.focus();
   return false;
 }
 else if (form.email.value == "") {
   alert("Please enter your email.");
   form.email.focus();
   return false;
 }
 else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
   alert("Please enter a valid email address.");
   form.email.focus();
   return false;
   }
 else if (form.comment.value == "") {
   alert("Please enter a comment/question.");
   form.comment.focus();
   return false;
 }
else{
return true;
}
}

</script>

This can go anywhere above the PHP code, what it does is when the user clicks submit it checks to see if all the fields have data and if so allows it to submit...

CODE

<?php

function mailcheck($emailadr){
return eregi("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$", $emailadr);
}

if (!isset($_POST['submit']))
#if submit doesn't have a value
{
?>
<h3>Feel free to contact me.</h3>
<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);">
<p>Name : <input type="text" name="name" /></p>
<p>Email : <input type="text" name="email" /></p><br />
<p>Comments/Questions : <br /><textarea name="comment" rows="10" cols="30"></textarea></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>

<?php
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$comment = stripslashes($_POST['comment']);
$to  = "Your Name<YourEmail@YourDomain.com> ";

if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
$subject = "Mail from site.";

# message
$message = '
<html>
<head>
 <title>Mail from site</title>
</head>
<body>
<p>The email contains user comments.</p>
<br />
Name of person : '.$name.'<br /> #I have it displaying the name
Email of person : '.$email.'<br />#here the email
Comment/Question left:<br />
<p>'.$comment.'</p>#here the comment
<br />
               #it uses html so feel free to format to your pleasure
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $email>\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $subject, $message, $headers);
echo 'Thank you for sending mail.';
}
else {
echo 'Sorry, one of more or your fields is invalid.';}
}

?>
Josh
OK, lets say I want the user to specify the subject. Would I just add this stuff:
CODE

if (form.subject.value == "") {
  alert("Please enter a subject.");
  form.name.focus();
  return false;

would be added to the javascript
this would be added to the form html:
CODE

<p>Email : <input type="text" name="subject" /></p><br />

and the php would look like this:
CODE

<?php
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
$to  = "Your Name<YourEmail@YourDomain.com> ";

if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
#$subject = "mail from site" deleted

# message
$message = '
<html>
<head>
<title>$subject</title>
</head>
<body>
<p>The email contains user comments.</p>
<br />
Name of person : '.$subject.'<br />
Name of person : '.$name.'<br /> #I have it displaying the name
Email of person : '.$email.'<br />#here the email
Comment/Question left:<br />
<p>'.$comment.'</p>#here the comment
<br />
              #it uses html so feel free to format to your pleasure
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $email>\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $subject, $message, $headers);
echo 'Thank you for sending mail.';
}
else {
echo 'Sorry, one of more of your fields is invalid.';}
}

?>


does this look OK timo?
Timo
QUOTE (Josh @ Mar 20 2004, 02:34 AM)
OK, lets say I want the user to specify the subject. Would I just add this stuff:
CODE

if (form.subject.value == "") {
  alert("Please enter a subject.");
  form.name.focus();
  return false;

would be added to the javascript
this would be added to the form html:
CODE

<p>Email : <input type="text" name="subject" /></p><br />

and the php would look like this:
CODE

<?php
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
$to  = "Your Name<YourEmail@YourDomain.com> ";

if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
#$subject = "mail from site" deleted

# message
$message = '
<html>
<head>
<title>$subject</title>
</head>
<body>
<p>The email contains user comments.</p>
<br />
Name of person : '.$subject.'<br />
Name of person : '.$name.'<br /> #I have it displaying the name
Email of person : '.$email.'<br />#here the email
Comment/Question left:<br />
<p>'.$comment.'</p>#here the comment
<br />
              #it uses html so feel free to format to your pleasure
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $email>\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $subject, $message, $headers);
echo 'Thank you for sending mail.';
}
else {
echo 'Sorry, one of more of your fields is invalid.';}
}

?>


does this look OK timo?

I don't see anything wrong with it besides bad labeling

<p>Email : <input type="text" name="subject" /></p><br />

haha just messing with you, it looks good besides that. It looks like you've covered every base... Try testing it out, tell me if it gives an error, I doubt it will though.
Josh
K, great tutorial timo, i'll deffinitely be using this. Right now i'm gonna watch some ncaa tournament smile.gif
Josh
Well i can't edit the html so it appears different in my email. I tried this:
Subject : <span style=/"font-size:21pt; font-weight:bold;/">'.$subject.'</span><br />
Name of person:<span style=/"color:red;/"> '.$name.'</span><br />
but to no avail

EDIT: well i figured out i didn't need the slashes and they were going the wrong way. so it works now thumbsupsmileyanim.gif
Timo
haha yep, darn I sent the PM a min too late. :-)
Joe
great tutorial Timo biggrin.gif

works great and easy to customize too

1 spelling mistake in your code though

CODE
echo 'Sorry, one o[b]f[/b] more of your fields is invalid.';}
Timo
QUOTE (joe2kiss @ Mar 20 2004, 02:31 PM)
great tutorial Timo biggrin.gif

works great and easy to customize too

1 spelling mistake in your code though

CODE
echo 'Sorry, one o[b]f[/b] more of your fields is invalid.';}

fixed, thanks
Josh
I got it up and working here:
www.joshmccrain.com/contact.php
Timo
haha works well :-D
Joe
got mine working too biggrin.gif

*URI removed*
Josh
Timo, when you type something like this into your message:
This isn't mine so I can't claim it

it will come out like this
This isn\'t mine so I can\'t claim it

how would i fix that?
Timo
$comment = $_POST['comment'];

to

$comment = stripslashes ($_POST['comment']);

Try that.
Josh
ah that did it, thanks timo. I thought that was the function but I wasn't sure where to put it.
Josh
Here's a script that uses Timo's that allows you to send an attatchment with the email.

Special thanks to scoutt at HTMLforums for setting this up.

CODE

<?php
//change/add to this line depending on what format of image you want.

$mime_type = array(1=>"image/jpeg", 2=>"image/gif", 3=>"image/jpg", 4=>"image/pjpeg"); //this part decides what type of mime type to put after the type of image is determined



function mailcheck($emailadr){
return eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$", $emailadr);
}

if (!isset($_POST['submit'])){
#if submit doesn't have a value

?>
   <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);" enctype="multipart/form-data">
   <p>Name: <input type="text" name="name" style="background:#ccc;" /></p>
   <p>Subject: <input type="text" name="subject" style="background:#ccc;" /></p>
   <p>Email: <input type="text" name="email" style="background:#ccc;" /></p>
   Send this file: <input name="userfile" type="file" />
   <p>Comments/Questions: <br />
   <textarea name="comment" rows="10" cols="30" style="background:#ccc;"></textarea>
   </p>
   <p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
   </form>
<?php
} else {
   if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
       // you can change this if you move the uploaded file from the tmp folder.
       $uploaddir = '/the/directory/here/uploads/'; // change this
       $uploadfile = $uploaddir . $_FILES['userfile']['name'];
   if(array_search($_FILES['userfile']['type'],$mime_type)){
       if ($_FILES['userfile']['size'] > 100000) {
               echo 'file size too large';
               exit;
           }
     
           if (!copy($_FILES['userfile']['tmp_name'], $uploadfile)) {
               echo "Possible file upload attack! Here's some debugging info:\n";
               print_r($_FILES);
               exit;
           }
           $new_mimetype = $_FILES['userfile']['type'];
       } else {
           echo "Wrong file type uploaded.";
           exit;
       }
   } else {
       echo "No file Uploaded, or file was empty";
       exit;
   }

   $name = $_POST['name'];
   $email = $_POST['email'];
   $subject = $_POST['subject'];
   $comment = stripslashes($_POST['comment']);
   $to = "bob@example.com";

   if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL)){
       #check if a real email address, if it returns true it sends the email
       #also checks to make sure the comment and name fields aren't empty

       # subject
       #$subject = "mail from site" deleted

       # message
       $message = '
       <html>
       <head>
       <title>$subject</title>
       <style type="text/css">
       body { font-family: arial; background: #ccc; }
       </style>
       </head>
       <body>

       <span style="font-size:12pt;font-weight:bold;color:blue;">Subject:</span> '.$subject.'<br /><br />
       <span style="font-size:12pt;font-weight:bold;color:blue;">Name of person:</span> '.$name.'<br /><br />
       <span style="font-size:12pt;font-weight:bold;color:blue;">Email of person:</span> '.$email.'<br /><br />
       <span style="font-size:12pt;font-weight:bold;color:blue;">Comment/Question:</span><br />
       <div style="font-size:10pt;">'.$comment.'</div>
       <br />

       </body>
       </html>';

       // create a unique boundry, can basically be anything.
       // generally people use time()
       $mail_boundary = md5(uniqid(time()));
       // create header
       $mail_headers = "From: $email\r\n"; #The sender's email
       $mail_headers .= "MIME-Version: 1.0\r\n";
       $mail_headers .= "Content-type: multipart/mixed; boundary=\"$mail_boundary\"\r\n\r\n";
       if (file_exists($uploadfile)) {
           // must open the file in readonly and binary format
           $fp = fopen($uploadfile, "rb");
           $file = fread($fp, filesize($uploadfile));
           // also you have to base64 it so the email can see it.
           $file = chunk_split(base64_encode($file));
           // must get the name of the file
           $filename = basename($uploadfile);
       }        
       // sets the email up so we can send the text and the attachment at the same time
       $new_mail_body .= "--{$mail_boundary}\r\n";
       $new_mail_body .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
       $new_mail_body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
       $new_mail_body .= "{$message}\r\n";

       $new_mail_body .= "--{$mail_boundary}\r\n";
       $new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n";
       $new_mail_body .= "Content-Transfer-Encoding:base64\r\n ";
       $new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
       $new_mail_body .= $file . "\r\n";
       $new_mail_body .= "--{$mail_boundary}--\r\n";

       // now the good stuff as we actually send the message and the attachment.
       mail($to,$subject,$new_mail_body,$mail_headers);

       echo 'Thank you for sending mail.';
   } else {
       echo 'Sorry, one of more of your fields is invalid.';
   }
}

?>
Joe
nice Josh, just one question though, if i wanted to add that, where exactly would it go?
Josh
In the body of your html. Notice there's a form in the middle of the script.
Josh
How would I get it to send the chosen option from a dropdown list?
Timo
You'd do it like you would a normal dropdown.

<SELECT NAME="dropdownname">
<OPTION VALUE="value1">first</option>
<OPTION VALUE="value2">second</option>
<OPTION VALUE="value3">third</option>
</SELECT>

When you want to get what's passed you type
$variablename = $_POST['dropdownname'];

It will contain whicher option is selected. So if you selected first it would contain value1; second would contain value2 and third would contain value3.
Josh
CODE

<?php

if (!isset($_POST['submit']))
#if submit doesn't have a value
{
?>
<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);">
 <p>Game Name:
   <input type="text" name="gamename" style="background:#ccc;" /></p>
 <p>Real Name(first name is OK):
   <input name="realname" type="text" style="background:#ccc;" value="">
 </p>
 <p title="It is OK to enter N/A if you don't want this to be public">Email:
   <input type="text" name="email" style="background:#ccc;" />
 </p>
 <p>AIM Name:
   <input name="aimname" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Location(State):
   <input name="state" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Favorite TDM Map:
   <input name="tdm" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Favorite Obj. Map:
   <input name="obj" type="text" style="background:#ccc;" value="">
 </p>
 <p>Favorite Weapon:
   <input name="favweapon" type="text" style="background:#ccc;" value="" />
 </p>
 <p>2nd Favorite Weapon:
   <input name="ndfavweapon" type="text" style="background:#ccc;" value="">
 </p>
 <p>Hobbies:
   <textarea name="hobbies" style="background:#ccc;"></textarea>
 </p>
 <p>
   Position in Clan: <select name="position">
<option value="leader">Clan Leader/Co-Leader</option>
<option value="captain">Ladder Team Capatain/Co-Captain</option>
<option value="rcon">Rcon Member</option>
<option value="member">Member</option>
   </select>
 </p>
 <p title="For example, Call of Duty, Generals">Other Games Played:
   <textarea name="othergames" style="background:#ccc;"></textarea>
 </p>
       
<p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
</form>
<?php
}
else
{
$gamename = stripslashes($_POST['gamename']);
$realname = $_POST['realname'];
$email = $_POST['email'];
$aimname = $_POST['aimname'];
$state = stripslashes($_POST['state']);
$tdm = $_POST['tdm'];
$obj = $_POST['obj'];
$favweapon = $_POST['favweapon'];
$ndfavweapon = $_POST['ndfavweapon'];
$hobbies = stripslashes($_POST['hobbies']);
$position = $_POST['position'];
$othergames = stripslashes($_POST['othergames']);
$to  = "person<email@place.com> ";

if (($gamename!=NULL)||($realname!=NULL)||($email!=NULL)||($aimname!=NULL)||($state!=NULL)||($tdm!=NULL)||
($obj!=NULL)||($favweapon!=NULL)||($ndfavweapon!=NULL)||($hobbies!=NULL)||($position!=NULL)||($othergames!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
#$subject = "mail from site" deleted

# message
$message = '
<html>
<head>
<title>$gamename</title>
<style type="text/css">
body { font-family: arial; background: #ccc; }
.section { font-size:12pt;font-weight:bold;color:blue; }
</style>
</head>
<body>

<span class="section">Subject:</span> '.$gamename.'<br /><br />
<span class="section">Name of person:</span> '.$realname.'<br /><br />
<span class="section">Email of person:</span> '.$email.'<br /><br />
<span class="section">Subject:</span> '.$aimname.'<br /><br />
<span class="section">Name of person:</span> '.$state.'<br /><br />
<span class="section">Email of person:</span> '.$tdm.'<br /><br />
<span class="section">Subject:</span> '.$obj.'<br /><br />
<span class="section">Name of person:</span> '.$favweapon.'<br /><br />
<span class="section">Email of person:</span> '.$ndfavweapon.'<br /><br />
<span class="section">Subject:</span> '.$hobbies.'<br /><br />
<span class="section">Name of person:</span> '.$position.'<br /><br />
<span class="section">Email of person:</span> '.$othergames.'<br /><br />
             
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $gamename\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $gamename, $realname, $email, $aimname, $state, $tdm, $obj, $favweapon, $ndfavweapon, $hobbies, $position, $othergames);
echo '<p>Your email was sent.</p>';
}
else {
echo 'Sorry, one of more of your fields is invalid.';}
}

?>

Timo
CODE

<?php

if (!isset($_POST['submit']))
#if submit doesn't have a value
{
?>
<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);">
 <p>Game Name:
   <input type="text" name="gamename" style="background:#ccc;" /></p>
 <p>Real Name(first name is OK):
   <input name="realname" type="text" style="background:#ccc;" value="">
 </p>
 <p title="It is OK to enter N/A if you don't want this to be public">Email:
   <input type="text" name="email" style="background:#ccc;" />
 </p>
 <p>AIM Name:
   <input name="aimname" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Location(State):
   <input name="state" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Favorite TDM Map:
   <input name="tdm" type="text" style="background:#ccc;" value="" />
 </p>
 <p>Favorite Obj. Map:
   <input name="obj" type="text" style="background:#ccc;" value="">
 </p>
 <p>Favorite Weapon:
   <input name="favweapon" type="text" style="background:#ccc;" value="" />
 </p>
 <p>2nd Favorite Weapon:
   <input name="ndfavweapon" type="text" style="background:#ccc;" value="">
 </p>
 <p>Hobbies:
   <textarea name="hobbies" style="background:#ccc;"></textarea>
 </p>
 <p>
   Position in Clan: <select name="position">
<option value="leader">Clan Leader/Co-Leader</option>
<option value="captain">Ladder Team Capatain/Co-Captain</option>
<option value="rcon">Rcon Member</option>
<option value="member">Member</option>
   </select>
 </p>
 <p title="For example, Call of Duty, Generals">Other Games Played:
   <textarea name="othergames" style="background:#ccc;"></textarea>
 </p>
       
<p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
</form>
<?php
}
else
{
$gamename = stripslashes($_POST['gamename']);
$realname = $_POST['realname'];
$email = $_POST['email'];
$aimname = $_POST['aimname'];
$state = stripslashes($_POST['state']);
$tdm = $_POST['tdm'];
$obj = $_POST['obj'];
$favweapon = $_POST['favweapon'];
$ndfavweapon = $_POST['ndfavweapon'];
$hobbies = stripslashes($_POST['hobbies']);
$position = $_POST['position'];
$othergames = stripslashes($_POST['othergames']);
$to  = "person<email@place.com> ";

if (($gamename!=NULL)||($realname!=NULL)||($email!=NULL)||($aimname!=NULL)||($state!=NULL)||($tdm!=NULL)||
($obj!=NULL)||($favweapon!=NULL)||($ndfavweapon!=NULL)||($hobbies!=NULL)||($position!=NULL)||($othergames!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
#$subject = "mail from site" deleted

# message
$message = '
<html>
<head>
<title>$gamename</title>
<style type="text/css">
body { font-family: arial; background: #ccc; }
.section { font-size:12pt;font-weight:bold;color:blue; }
</style>
</head>
<body>

<span class="section">Subject:</span> '.$gamename.'<br /><br />
<span class="section">Name of person:</span> '.$realname.'<br /><br />
<span class="section">Email of person:</span> '.$email.'<br /><br />
<span class="section">Subject:</span> '.$aimname.'<br /><br />
<span class="section">Name of person:</span> '.$state.'<br /><br />
<span class="section">Email of person:</span> '.$tdm.'<br /><br />
<span class="section">Subject:</span> '.$obj.'<br /><br />
<span class="section">Name of person:</span> '.$favweapon.'<br /><br />
<span class="section">Email of person:</span> '.$ndfavweapon.'<br /><br />
<span class="section">Subject:</span> '.$hobbies.'<br /><br />
<span class="section">Name of person:</span> '.$position.'<br /><br />
<span class="section">Email of person:</span> '.$othergames.'<br /><br />
             
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $gamename\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $subject, $message, $headers);
echo '<p>Your email was sent.</p>';
}
else {
echo 'Sorry, one of more of your fields is invalid.';}
}

?>


haha Josh, you changed the mail function. wink.gif You don't need to do that. Just add to the body like you did, I changed it back to mail($to, $subject, $message, $headers);
Josh
d'oh!

thanks smile.gif
fmx
CODE
<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return


what does <?php echo $_SERVER['SCRIPT_NAME']?> echo?

something like a thankyou.html?
Joe
$_SERVER['SCRIPT_NAME']

contains the current script's path. this is useful for pages which need to point to themselves
ven7
I tried testing your mailer.php script on our server, but i still did not get the data in my email address. I simply modified the ff. :
$to = "Ven<ven@quazardesigns.net> "; (my own email add)


$name = $_POST['name'];
$email = $_POST['email'];
$comment = stripslashes($_POST['comment']);
$to = "Ven<ven@quazardesigns.net> ";

Joe
is that all your modified?

your changes look find so i dont think thats the problem
Joe
did you remember to change

action="<?php echo $_SERVER['mailer.php']?>

to

action="<?php echo $_SERVER['SCRIPT_NAME']?>"

like i said on htmlforums?
ven7
Yes i did change that back, but still it did not work. No data in my email. blink.gif
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-2010 Invision Power Services, Inc.