Solved

PHP Mail code problem and other help needed...

Posted on 2006-06-20
15
210 Views
Last Modified: 2013-12-12
Below is code from past EE question, just modified to my desires.  There seems to be a problem when I send, specifically around the subject, I believe that is why I am getting the "could not send mail, an error occured" message.  As soon as I modified (figures) it failed.  It worked fine before I messed with it.  I believe it is around the "subject", as evenutally I want it to be "Message from Tom Jones", where Tom Jones is "fromname" from user input.

Plan is to use this as a simple "tell a friend" function and also need to have loop for up to 5 friends, also if Friend2 "name" is there and not Friend2 "email", it should flag as such.  Lastly, how can I prevent spammers, maybe time function?  

This code is part of Diablo84

<HTML>
<HEAD>
<TITLE>formpage</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<?php
if (isset($_POST['submit'])) {
 if (empty($_POST['toname']) || empty($_POST['tomail']) || empty($_POST['fromname']) || empty($_POST['frommail']) || empty($_POST['message'])) {
  echo "all fields must be completed<br>\n";
 }
 else {
  $to  = $_POST['tomail'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];
  $headers = "To: ".$_POST['toname']." <".$_POST['tomail'].">\r\n";
  $headers .= "From: ".$_POST['fromname']." <".$_POST['frommail'].">\r\n";
  mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
  echo "mail sent<br>\n";
 }
}
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="mail">
<input type="HIDDEN" name="subject" value="ID HERE">
      <div align="center">
  <table border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="FFFFFF" style="border: 1pt solid #001b6e; width: 525px;" nof="LY">
    <tr valign="TOP" align="LEFT">
      <td width="100%">
<br><b><font size="-2" face="Verdana">&nbsp;&nbsp;*Note: </font></b><font size="-2" face="Verdana">
&nbsp;&nbsp;Tell Your Friends.<br>
                  </font>

<div align="center">
<table width="520" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<table border="0" cellpadding="0" cellspacing="0" width="97%">
  <tr>
      <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Name:&nbsp;</font>
      <input type="TEXT" name="fromname" value size="24" maxlength="30" value="<?php if(isset($_POST['fromname'])) { echo $_POST['fromname']; } ?>"></font></td></tr>
  <tr>
      <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Email:&nbsp;</font>
      <input type="TEXT" name="frommail" value size="24" maxlength="50" value="<?php if(isset($_POST['frommail'])) { echo $_POST['frommail']; } ?>"></font></td>
  </tr>
</table></td>
      <td><font face="Arial">
      <textarea wrap="PHYSICAL" id="textarea" name="message" rows="4" cols="27" value=""><?php if(isset($_POST['message'])) { echo $_POST['message']; } ?></textarea></font>
</td></tr></table>
</div>
<div align="left">
<table id="Table1" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="#111111">
   <tr>
      <td width="117"> <p>&nbsp;</p></td>
      <td width="158"> <p><font face="Arial" size="-1"><b>Name</b></font></p></td>
      <td width="228"> <p><font face="Arial" size="-1"><b>Email</b></font></p></td></tr>
   <tr>
      <td width="117" align="right"> <p><font face="Arial" size="2">Friend 1&nbsp;</font></p></td>
      <td width="158"> <p><font face="Arial">
      <input type="TEXT" name="toname" value size="20" maxlength="50" value="<?php if(isset($_POST['toname'])) { echo $_POST['toname']; } ?>"></font></td>
      <td width="228"> <p><font face="Arial">
      <input type="TEXT" name="tomail" value size="20" maxlength="50" value="<?php if(isset($_POST['tomail'])) { echo $_POST['tomail']; } ?>"></font></td></tr>
   <tr>
      <td width="117" align="right"> <p><font face="Arial" size="2">Friend 2&nbsp;</font></p></td>
      <td width="158"> <p><font face="Arial">
      <input type="TEXT" name="name2" value size="20" maxlength="50"></font></td>
      <td width="228"> <p><font face="Arial">
      <input type="TEXT" name="email2" value size="20" maxlength="50"></font></td></tr>
   <tr>
      <td width="117" align="right"> <p><font face="Arial" size="2">Friend 3&nbsp;</font></p></td>
      <td width="158"> <p><font face="Arial">
      <input type="TEXT" name="name3" value size="20" maxlength="50"></font></td>
      <td width="228"> <p><font face="Arial">
      <input type="TEXT" name="email3" value size="20" maxlength="50"></font></td></tr>
   <tr>
      <td width="117" align="right"> <p><font face="Arial" size="2">Friend 4&nbsp;</font></p></td>
      <td width="158"> <p><font face="Arial">
      <input type="TEXT" name="name4" value size="20" maxlength="50"></font></td>
      <td width="228"> <p><font face="Arial">
      <input type="TEXT" name="email4" value size="20" maxlength="50"></font></td></tr>
</table>
</div>
         
<table width="100%" border="0" cellspacing="0" cellpadding="0" nof="TE" height="4">
   <tr>
      <td align="center" height="4"><br><font face="Arial">
      <input name="submit" type="submit" value="Send to Friends!"></font>
</td></tr></table>
</td></tr></table>
</div>
</form>




</BODY>
</HTML>
0
Comment
Question by:precision
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 

Author Comment

by:precision
ID: 16942665
Ok, figured out part 1 with "could not send mail, an error occured", the from HAS to be domain of server, otherwise it fails, make sense?  I wound up doing that and it worked....any ideas on my other issues?
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16943324
I think you can't send it because your localhost doesn't have mailserver. I upload your code to my host and it works.
Bonmat86.
0
 

Author Comment

by:precision
ID: 16950389
I solved part 1, I found out it needed actual domain of the server to send the mail, which is just fine....so....part 2...

How can I create a simple loop so I can have 3 inputs as follows:
toname
tomail

toname1
tomail1

toname2
tomail2

I want to have it so if someone puts in toname1 without tomail1, it will echo error message.  Also, how can I loop it if it needs to send 3 mails instead of just 1?  I am try a few things with no success...thanks in advance.
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16951740
<HTML>
<HEAD>
<TITLE>formpage</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<?php
if (isset($_POST['submit'])) {
      if (empty($_POST['fromname']) || empty($_POST['frommail']) || empty($_POST['message'])) {
            echo "all fields must be completed<br>\n";
      }
      else
      {
            for ($i = 1; $i <= 4; $i++)
            {
                  if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
                  {
                        $to[$i]  = $_POST['tomail' . $i];
                        $subject[$i] = $_POST['subject' . $i];
                        $message[$i] = $_POST['message' . $i];
                        $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                        $headers[$i] .= "From: " . $_POST['fromname' . $i]." <" . $_POST['frommail' . $i] . ">\r\n";
                        //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                        //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                  }
                  else
                  {
                        echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
                  }
            }

            /*for ($i = 1; $i <= 4; $i++)
            {
                  mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                  echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
            }*/
      }
}
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="mail">
<input type="HIDDEN" name="subject" value="ID HERE">
     <div align="center">
  <table border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="FFFFFF" style="border: 1pt solid #001b6e; width: 525px;" nof="LY">
    <tr valign="TOP" align="LEFT">
      <td width="100%">
<br><b><font size="-2" face="Verdana">&nbsp;&nbsp;*Note: </font></b><font size="-2" face="Verdana">
&nbsp;&nbsp;Tell Your Friends.<br>
                  </font>

<div align="center">
<table width="520" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<table border="0" cellpadding="0" cellspacing="0" width="97%">
  <tr>
     <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Name:&nbsp;</font>
     <input type="TEXT" name="fromname" size="24" maxlength="30" value="<?php echo $_POST['fromname'];?>"></font></td></tr>
  <tr>
     <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Email:&nbsp;</font>
     <input type="TEXT" name="frommail" size="24" maxlength="50" value="<?php echo $_POST['frommail'];?>"></font></td>
  </tr>
</table></td>
     <td><font face="Arial">
     <textarea wrap="PHYSICAL" id="textarea" name="message" rows="4" cols="27" value=""><?php if(isset($_POST['message'])) { echo $_POST['message']; } ?></textarea></font>
</td></tr></table>
</div>
<div align="left">
<table id="Table1" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="#111111">
   <tr>
     <td width="117"> <p>&nbsp;</p></td>
     <td width="158"> <p><font face="Arial" size="-1"><b>Name</b></font></p></td>
     <td width="228"> <p><font face="Arial" size="-1"><b>Email</b></font></p></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 1&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="toname1" value="<?php echo $_POST['tomail1'];?>" size="20" maxlength="50" value="<?php if(isset($_POST['toname'])) { echo $_POST['toname']; } ?>"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="tomail1" value="<?php echo $_POST['toname1'];?>" size="20" maxlength="50" value="<?php if(isset($_POST['tomail'])) { echo $_POST['tomail']; } ?>"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 2&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name2" value="<?php echo $_POST['tomail2'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email2" value="<?php echo $_POST['toname2'];?>" size="20" maxlength="50"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 3&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name3" value="<?php echo $_POST['tomail3'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email3" value="<?php echo $_POST['toname3'];?>" size="20" maxlength="50"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 4&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name4" value="<?php echo $_POST['tomail4'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email4" value="<?php echo $_POST['toname4'];?>" size="20" maxlength="50"></font></td></tr>
</table>
</div>
         
<table width="100%" border="0" cellspacing="0" cellpadding="0" nof="TE" height="4">
   <tr>
     <td align="center" height="4"><br><font face="Arial">
     <input name="submit" type="submit" value="Send to Friends!"></font>
</td></tr></table>
</td></tr></table>
</div>
</form>




</BODY>
</HTML>

If you still want sent email if user don't input all of field, just remove comment :
            /*for ($i = 1; $i <= 4; $i++)
            {
                  mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                  echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
            }*/

Bonmat86.
0
 

Author Comment

by:precision
ID: 16952688
ok, getting close...here are some issues I see....right now

1) If I fill in just a few fields, error message flag comes up just find, but my typed in form data dissapears...

2) I want it so someone can just refer 1 friend but "has to have name/email filled in", or they can refer 2 or even 3 friends...
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16952769
<HTML>
<HEAD>
<TITLE>formpage</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">

</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>

<?php
if (isset($_POST['submit'])) {
     if (empty($_POST['fromname']) || empty($_POST['frommail']) || empty($_POST['message'])) {
          echo "all fields must be completed<br>\n";
     }
     else
     {
          for ($i = 1; $i <= 4; $i++)
          {
               if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
               {
                    $to[$i]  = $_POST['tomail' . $i];
                    $subject[$i] = $_POST['subject' . $i];
                    $message[$i] = $_POST['message' . $i];
                    $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                    $headers[$i] .= "From: " . $_POST['fromname' . $i]." <" . $_POST['frommail' . $i] . ">\r\n";
                    //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                    //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
               }
               /*else
               {
                    echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
               }*/
          }

          for ($i = 1; $i <= 4; $i++)
          {
                     if (!empty($_POST['toname' . $i]))
                     {
                              mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                              echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                     }
          }
     }
}
?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="mail">
<input type="HIDDEN" name="subject" value="ID HERE">
     <div align="center">
  <table border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="FFFFFF" style="border: 1pt solid #001b6e; width: 525px;" nof="LY">
    <tr valign="TOP" align="LEFT">
      <td width="100%">
<br><b><font size="-2" face="Verdana">&nbsp;&nbsp;*Note: </font></b><font size="-2" face="Verdana">
&nbsp;&nbsp;Tell Your Friends.<br>
                  </font>

<div align="center">
<table width="520" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<table border="0" cellpadding="0" cellspacing="0" width="97%">
  <tr>
     <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Name:&nbsp;</font>
     <input type="TEXT" name="fromname" size="24" maxlength="30" value="<?php echo $_POST['fromname'];?>"></font></td></tr>
  <tr>
     <td width="100%"><font face="Arial"><font size="-1">&nbsp;&nbsp;Your Email:&nbsp;</font>
     <input type="TEXT" name="frommail" size="24" maxlength="50" value="<?php echo $_POST['frommail'];?>"></font></td>
  </tr>
</table></td>
     <td><font face="Arial">
     <textarea wrap="PHYSICAL" id="textarea" name="message" rows="4" cols="27" value=""><?php if(isset($_POST['message'])) { echo $_POST['message']; } ?></textarea></font>
</td></tr></table>
</div>
<div align="left">
<table id="Table1" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="#111111">
   <tr>
     <td width="117"> <p>&nbsp;</p></td>
     <td width="158"> <p><font face="Arial" size="-1"><b>Name</b></font></p></td>
     <td width="228"> <p><font face="Arial" size="-1"><b>Email</b></font></p></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 1&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="toname1" value="<?php echo $_POST['toname1'];?>" size="20" maxlength="50" value="<?php if(isset($_POST['toname'])) { echo $_POST['toname']; } ?>"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="tomail1" value="<?php echo $_POST['tomail1'];?>" size="20" maxlength="50" value="<?php if(isset($_POST['tomail'])) { echo $_POST['tomail']; } ?>"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 2&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name2" value="<?php echo $_POST['tomail2'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email2" value="<?php echo $_POST['toname2'];?>" size="20" maxlength="50"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 3&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name3" value="<?php echo $_POST['tomail3'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email3" value="<?php echo $_POST['toname3'];?>" size="20" maxlength="50"></font></td></tr>
   <tr>
     <td width="117" align="right"> <p><font face="Arial" size="2">Friend 4&nbsp;</font></p></td>
     <td width="158"> <p><font face="Arial">
     <input type="TEXT" name="name4" value="<?php echo $_POST['tomail4'];?>" size="20" maxlength="50"></font></td>
     <td width="228"> <p><font face="Arial">
     <input type="TEXT" name="email4" value="<?php echo $_POST['toname4'];?>" size="20" maxlength="50"></font></td></tr>
</table>
</div>
         
<table width="100%" border="0" cellspacing="0" cellpadding="0" nof="TE" height="4">
   <tr>
     <td align="center" height="4"><br><font face="Arial">
     <input name="submit" type="submit" value="Send to Friends!"></font>
</td></tr></table>
</td></tr></table>
</div>
</form>




</BODY>
</HTML>


Please copy all of my code and try it.
Bonmat86.
0
 

Author Comment

by:precision
ID: 16952975
ok, your good!  I put in the exact code above, your latest post...

When I type in "Your Name", flag comes up fine regarding "all fields must be completed"

Then when I type in "Your Email", flag comes up fine regarding "all fields must be completed"

Then when I type in the message, flags go away completely (maybe should say "enter a friend")

Then when I type in just "name1" it states "mail sent to blah"

It is getting close!  Thanks in advance....soon we can close and bump to more points with your effort...
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16953093
<?php
if (isset($_POST['submit'])) {
     if (empty($_POST['fromname']))
       {
          echo "You must iput From Name field !<br>\n";
     }
       if (empty($_POST['frommail']))
       {
          echo "You must iput From Mail field !<br>\n";
     }
       if (empty($_POST['message']))
       {
          echo "You must iput Message field !<br>\n";
     }
     else
     {
          for ($i = 1; $i <= 4; $i++)
          {
               if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
               {
                    $to[$i]  = $_POST['tomail' . $i];
                    $subject[$i] = $_POST['subject' . $i];
                    $message[$i] = $_POST['message' . $i];
                    $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                    $headers[$i] .= "From: " . $_POST['fromname' . $i]." <" . $_POST['frommail' . $i] . ">\r\n";
                    //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                    //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                              $count++;
               }
               /*else
               {
                    echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
               }*/
          }

              if ($count == '') echo "You must enter a friend.";

          for ($i = 1; $i <= 4; $i++)
          {
                  if (!empty($_POST['toname' . $i]))
                  {
                         mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                         echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                  }
          }
     }
}
?>

As you like.
Good luck.
Bonmat86.
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16953106
<?php
if (isset($_POST['submit'])) {
     if (empty($_POST['fromname']))
       {
          echo "You must input From Name field !<br>\n";
     }
       if (empty($_POST['frommail']))
       {
          echo "You must input From Mail field !<br>\n";
     }
       if (empty($_POST['message']))
       {
          echo "You must input Message field !<br>\n";
     }
     else
     {
          for ($i = 1; $i <= 4; $i++)
          {
               if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
               {
                    $to[$i]  = $_POST['tomail' . $i];
                    $subject[$i] = $_POST['subject' . $i];
                    $message[$i] = $_POST['message' . $i];
                    $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                    $headers[$i] .= "From: " . $_POST['fromname' . $i]." <" . $_POST['frommail' . $i] . ">\r\n";
                    //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                    //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                              $count++;
               }
               /*else
               {
                    echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
               }*/
          }

          for ($i = 1; $i <= 4; $i++)
          {
                  if (!empty($_POST['toname' . $i]))
                  {
                         mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                         echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                  }
          }
     }
       if ($count == '') echo "You must enter a friend.";
}
?>

This one is better.
Bonmat86.
0
 

Author Comment

by:precision
ID: 16953193
ok, much better but when I have entered "Your Name" "Your Email" & "Message"....Then when I enter "toname1" and NOT "tomail1" I get this....

mail sent to johnboy <>
You must enter a friend.
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16953271
<?php
if (isset($_POST['submit'])) {
     if (empty($_POST['fromname']))
       {
          echo "You must input From Name field !<br>\n";
     }
       if (empty($_POST['frommail']))
       {
          echo "You must input From Mail field !<br>\n";
     }
       if (empty($_POST['message']))
       {
          echo "You must input Message field !<br>\n";
     }
     else
     {
             $count = 0;
          for ($i = 1; $i <= 4; $i++)
          {
               if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
               {
                    $to[$i]  = $_POST['tomail' . $i];
                    $subject[$i] = $_POST['subject' . $i];
                    $message[$i] = $_POST['message' . $i];
                    $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                    $headers[$i] .= "From: " . $_POST['fromname' . $i]." <" . $_POST['frommail' . $i] . ">\r\n";
                    //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                    //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                              $count++;
               }
               /*else
               {
                    echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
               }*/
          }

          for ($i = 1; $i <= 4; $i++)
          {
                  if (!empty($_POST['toname' . $i]) && $count != 0)
                  {
                         mail($to[$i], $subject[$i], $message[$i], $headers[$i]) or die("could not send mail, an error occured");
                         echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                  }
          }
     }
       if ($count == 0) echo "You must enter a friend.";
}
?>

Bonmat86.
0
 

Author Comment

by:precision
ID: 16953348
Ahhhhh...Sorry for the extra effort, like I said we will move to new post soon...

Much better, now when I send to friends, it states:

"could not send mail, an error occured"
and form goes away, just left with error message.
0
 
LVL 10

Accepted Solution

by:
Khanh Doan earned 500 total points
ID: 16953493
<?php
if (isset($_POST['submit'])) {
     if (empty($_POST['fromname']))
       {
          echo "You must input From Name field !<br>\n";
     }
       if (empty($_POST['frommail']))
       {
          echo "You must input From Mail field !<br>\n";
     }
       if (empty($_POST['message']))
       {
          echo "You must input Message field !<br>\n";
     }
     else
     {
             $count = 0;
          for ($i = 1; $i <= 4; $i++)
          {
               if (!empty($_POST['toname' . $i]) && !empty($_POST['tomail' . $i]))
               {
                    $to[$i]  = $_POST['tomail' . $i];
                    $subject = $_POST['subject'];
                    $message = $_POST['message'];
                    $headers[$i] = "To: " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . ">\r\n";
                    $headers[$i] .= "From: " . $_POST['fromname']." <" . $_POST['frommail'] . ">\r\n";
                    //mail($to, $subject, $message, $headers) or die("could not send mail, an error occured");
                    //echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                              $count++;
               }
               /*else
               {
                    echo "You must input toname" . $i . "or tomail" . $i . " !<br>";
               }*/
          }

          for ($i = 1; $i <= 4; $i++)
          {
                  if (!empty($_POST['toname' . $i]) && $count != 0)
                  {
                         mail($to[$i], $subject, $message, $headers[$i]) or die("could not send mail, an error occured");
                         echo "mail sent to " . $_POST['toname' . $i] . " <" . $_POST['tomail' . $i] . "><br>\n";
                  }
          }
     }
       if ($count == 0) echo "You must enter a friend.";
}
?>

I uploaded it to my host and it works now.
Bonmat86.
0
 

Author Comment

by:precision
ID: 16953557
Nice...I have a few tweaks, moving to new question with your code, about 3-5 minutes....
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question