Merging 2 Php Scripts Together


Recommended Posts

I am using the following script to send emails from a submission form on my website:

<?php

$to = "[email protected]";

$name = $_POST['name'];

$email = $_POST['email'];

$subject = $_POST['subject'];

$msg = $_POST['msg'];

$d = date('l dS \of F Y h:i:s A');

$sub = "Contact Page";

$headers = "From: $name <$email>\n";

$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";

$mes = "Subject: ".$subject."\n";

$mes .= "Message: ".$msg."\n";

$mes .= "Name: ".$name."\n";

$mes .= 'Email: '.$email."\n";

$mes .= 'Date & Time: '.$d;

if (empty($name) || empty($email) || empty($subject) || empty($msg))

{

echo " <h3>Sorry all fields are required.</h3>";

}

elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {

print " <h3>Sorry the email address you entered looks like it's invalid.</h3>";

}

else

{

mail($to, $sub, $mes, $headers);

print " <h3><center>Thank you ".$name." for contacting me.<br>I will get back to you as soon as possible. <br><br> You will be redirected to the home page in 3 seconds...</center></h3>";

}

?>

I am very happy with this script and it is working well...I just liked certain aspects of my old script better. This is what it looks like:

<?php

$option = $_GET['option'];

$webmastersemail = "[email protected]";

$bannedips = "";

$footer = "<center><p><a href=''></a></p></center>";

if($webmastersemail == null)

{

echo("<p align='center'>The webmaster must specify his / her email, to which these messages will be sent.</p>");

}

if($option == null)

{

$option = "viewform";

}

if($option == "viewform")

{

echo("<center>");

echo("<table border='0' cellpadding='5' cellspacing='0' style='font-size: 90%;'>");

echo("<form action='?option=sendemail' method='POST'>");

echo("<tr>");

echo("<td align='right'>Your name: </td>");

echo("<td><input style='font-size: 70%' type='text' name='yourname' /></td>");

echo("</tr>");

echo("<tr>");

echo("<td align='right'>Your email: </td>");

echo("<td><input style='font-size: 70%' type='text' name='youremail' /></td>");

echo("</tr>");

echo("<tr>");

echo("<td align='right'>Your subject: </td>");

echo("<td><input style='font-size: 70%' type='text' name='yoursubject' /></td>");

echo("</tr>");

echo("<tr>");

echo("<td valign='top' align='right'>Your message: </td>");

echo("<td><textarea cols='30' rows='7' name='yourmessage'></textarea></td>");

echo("</tr>");

echo("<tr>");

echo("<td></td>");

echo("<td><input style='font-size: 70%' type='submit' value='Send' /></td>");

echo("</tr>");

echo("</form>");

echo("</table>");

echo("</center>");

echo($footer);

}

if($option == "sendemail")

{

$ip = $_SERVER["REMOTE_ADDR"];

$pass = "yes";

$exbannedips = explode(",", $bannedips);

for($i=0; $i<count($exbannedips); $i++)

{

if($exbannedips[$i] == $ip)

{

echo("<p align='center'>You are banned from this Contact Form if you feel there has been a mistake contact the website administrator, click <a href='?option=viewform'>here</a> to return.</p>");

$pass = "no";

}

}

if($pass != "no")

{

$yourname = $_POST['yourname'];

$youremail = $_POST['youremail'];

$yoursubject = $_POST['yoursubject'];

$yourmessage = $_POST['yourmessage'];

$yourmessage = nl2br($yourmessage);

$yourmessage = str_ireplace("<br />", "\n\r", $yourmessage);

mail("$webmastersemail", "$yoursubject", "This persons email is: $youremail\n\rThis persons IP address is: $ip\n\r\n\rThe message is: \n\r\n\r$yourmessage");

echo("<p align='center'>Your message has been sent.</p>");

}

}

?>

The second script has the ability to ban IPs and it also sends the IP along with the message. I want the IP banning/sending abilities from the second script, placed into my first script. I tried tinkering with it and just when I thought it would work, it didn't (I suck at php).

I also wanted to reformat the order of the fields that it sends in the email (i.e. name, subject, ect.). I want it to go Name, Email, Subject, Message, Date & Time (in that order). I tried simply reorganizing the "$mes" variables, but that just screwed it up further...

Edited by DOSError
Link to post
Share on other sites

I will say that there is a second half to that 1st script. It is a simple html submission form:

<form method="post" action="contact/send.php">

Name:<br><input name="name" type="text" size="30"><br>

Email:<br><input name="email" type="text" size="30"><br>

Subject:<br><input name="subject" type="text" size="30"><br><br>

Message:<br><textarea name="msg" cols="50" rows="12"></textarea><br><br>

<input type="submit" value="Send">  <input type="reset" value="Reset">

If that makes any difference...

Link to post
Share on other sites

For the rearranging of items in the message and to grab the IP, add the following to the first part of your code:

$ip = $_SERVER["REMOTE_ADDR"];
$mes = "Name: ".$name."\n";
$mes .= 'Email: '.$email."\n";
$mes .= "Subject: ".$subject."\n";
$mes .= "Message: ".$msg."\n";
$mes .= "IP: ".$ip."\n";
$mes .= 'Date & Time: '.$d;

Someone more knowledgeable with PHP will have to help with the bannedIP option.

Edited by jsbowen
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...