Jump to content

Recommended Posts

I have this mail code, and i was wondering if anyone could help make it a bit shorter. I believe there is a easier to way to display the errors?

<?php
session_start();
include("phpmailer/class.phpmailer.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" media="screen" href="style.css">
<title>Mail App</title>
<style type="text/css">
body
{
visibility:hidden;
background-color:#000000;
}

.test
{
visibility:visible;
margin-top:25%;
}

</style>
<script type="text/javascript" src="main.js"></script>
</head>
<body onLoad="loading()">
<div id="loader" align="center" class="test">
<h1>Loading</h1> 
<br /><img src="loading.gif" alt="loading" align="absmiddle"></div>
<div align="center" id="main">
<?php

//set some variables for the form
$to = "Enter An Email";
$subject = "Enter A Subject";
$from = "Enter Your Email";
$message = "Enter Your message here";


//first off check if they have posted
if(isset($_POST['send']))
{
$to = htmlspecialchars($_POST['to']);
$to = explode(",",$to);
$count = count($to);
if($count <=1)
{
$go = $to[0];
}
if($count == 2)
{
$go = $to[0].",";
$go .= $to[1];
}
if($count == 3)
{
$go = $to[0].",";
$go .= $to[1].",";
$go .= $to[2];
}
if($count == 4)
{
$go = $to[0].",";
$go .= $to[1].",";
$go .= $to[2].",";
$go .= $to[3];
}
if($count == 4)
{
$go = $to[0].",";
$go .= $to[1].",";
$go .= $to[2].",";
$go .= $to[3].",";
$go .= $to[4];
}
echo $go;
$subject = htmlspecialchars($_POST['subject']);
$from = htmlspecialchars($_POST['from']);
$message = htmlspecialchars($_POST['message']);
$error = 0;

if(strlen($go[0]) < 1)
{
echo "<h5>Please Enter A Recipient</h5>";
$error = 1;
}
elseif (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $go)) {
echo "<h5>Invalid Recipient Email Address</h5>";
$error = 1;
} 

if(strlen($from) < 1)
{
echo "<h5>Please Enter Your Email</h5>";
$error = 1;
}
elseif (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) {
  echo "<h5>Your Email Address Appears To Be Invalid</h5>";
  $error = 1;
}

if(strlen($subject) < 1)
{
echo "<h5>Please Enter A Subject</h5>";
$error = 1;
}

if(strlen($message) < 1)
{
echo "<h5>Please Enter A Message</h5>";
$error = 1;
}

if($error == 0)
{
$mail = new PHPMailer();
$mail->IsSMTP();                            // set mailer to use SMTP
$mail->Host = "random-game-design.awardspace.com";        // specify main and backup server
$mail->SMTPAuth = true;                        // turn on SMTP authentication
$mail->Username = "admin@random-game-design.awardspace.com";    // SMTP username
$mail->Password = "Natasha";            // SMTP password

$mail->From = $from;    //do NOT fake header.
$mail->FromName = $from;
$mail->AddAddress($go);
$mail->AddReplyTo($from, "Support and Help"); //optional

$mail->Subject = $subject;
$mail->Body    = $message;

if(!$mail->Send())
{
   echo $mail->ErrorInfo;
}else{
   echo "email was sent";
}

}

?>
<form action="mailapp.php" method="post" >
<?php if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $go)) { ?>
<font color="#FF0000">
To :<input type="text" value="<?php echo $go; ?>" name="to" size="30" style="border:#FF0000 thin solid;" title="Enter The Recipients Email Address">
</font> 
<?php } else { ?> 
To :<input type="text" value="<?php echo $go; ?>" name="to" size="30" title="Enter The Recipients Email Address"> 
<?php }
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) { ?>
<font color="#FF0000">
From :<input type="text" value="<?php echo $from; ?>" name="from" size="30" style="border:#FF0000 thin solid;" title="Enter Your Email">
</font> 
<?php } else { ?> 
From :<input type="text" value="<?php echo $from; ?>" name="from" size="30" title="Enter Your Email">
<?php }
if(strlen($subject) == "") { ?>
<font color="#FF0000">
Subject :<input type="text" value="<?php echo $subject; ?>"  style=" border:#FF0000 thin solid;" name="subject" size="30" title="Enter The Email Subject">
</font>
<?php } else { ?>
Subject :<input type="text" value="<?php echo $subject; ?>"  name="subject" size="30" title="Enter The Email Subject">
<?php } 
if(strlen($message) == "") { ?>
<font color="#FF0000">
Message:<textarea title="Enter Your Message" name="message" rows="10" cols="100" style="border:#FF0000 thin solid;"><?php echo $message; ?></textarea>
</font>
<?php }else { ?>
Message:
<textarea title="Enter Your Message" name="message" rows="10" cols="100"><?php echo $message; ?></textarea>
<?php } ?>
<input type="submit" name="send" value="Send" title="Send The Email" size="30">
</form>
</div>
<?php		
}
else
{
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" >
To :<input type="text" value="<?php echo $go; ?>" name="to" size="30" onClick="this.value=''" title="Enter The Recipients Email Address">

From :<input type="text" value="<?php echo $from; ?>" name="from" size="30" onClick="this.value=''" title="Enter Your Email">

Subject : <input type="text" value="<?php echo $subject; ?>"  name="subject" size="30" onClick="this.value=''" title="Enter The Email Subject">

Message:
<textarea title="Enter Your Message" name="message" onClick="this.value=''" rows="10" cols="100"><?php echo $message; ?></textarea>
<input type="submit" name="send" value="Send" title="Send The Email" size="30">
</form>
<?php
}
?>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/112385-help-to-minmize-code/
Share on other sites

Well, I'm not going to rewrite your code for you, but I'll point some stuff out:

 

- Why are you exploding $to, only to turn around and rebuild it into $go (nevermind the fact that you could have just made that into a loop)?

 

- Checking if a variable is empty by using strlen is flawed in that it counts whitespace as a character.  Instead, do something like

if (isset($go) && (trim($go) != '')) { ... }

 

- On that note, you are error checking each posted var individually.  How about instead you put all that in a loop from the getgo, sanitizing and checking if empty etc.. all in one loop.

 

- On top of all that, you do the same exact error checking for your form... there's really no reason why you need to be doing all this individually.

Link to comment
https://forums.phpfreaks.com/topic/112385-help-to-minmize-code/#findComment-577001
Share on other sites

Ok, thanks.

 

An anwser to no.1 is because i am eventually going to be putting all the emails($to) in the database. but i still don't know now why i am exploding as i can enter them as one.

 

And can you explain how i would do the while loop for the error checking , and the posted vars.

Link to comment
https://forums.phpfreaks.com/topic/112385-help-to-minmize-code/#findComment-577002
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.