Jump to content

Archived

This topic is now archived and is closed to further replies.

skter4938

contact form, multiple email

Recommended Posts

Hello there.

I have a contact form made, but I want the sender to choose to whome he wants to send it to, with a selection box. something like http://www.explosm.net/contact/

Here is the code in the contact.php part, where the form is processed.
[code]// get posted data into local variables
$EmailTo = "skter4938@gmail.com";
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$Name = Trim(stripslashes($_POST['Name']));
$Subject = Trim(stripslashes($_POST['Subject']));
$Message = Trim(stripslashes($_POST['Message'])); [/code]

And this is the accual form code
[code]<form method="POST" action="contact.php">

<select name="emailto">
    <option value="artem" selected>Artem Artemov</option>
      <option value="kris">Kris Mobayeni</option>
  </select>

<p><label for="Name">Name</label><br />
<input id="Name" name="Name" type="text" tabindex="1" size="30" /></p>

<p><label for="EmailFrom">Email</label><br />
<input id="EmailFrom" name="EmailFrom" type="text" tabindex="2" size="30" /></p>

<p><label for="Subject">Subject</label><br />
<input id="Subject" name="Subject" type="text" tabindex="3" size="30" /></p>

<p><label for="words">Message</label><br />
<textarea name="Message" tabindex="5" rows="10" cols="24" style="width:300px;"></textarea></p>

<input type="submit" name="post" id="post" value="&nbsp;Send&nbsp;" />&nbsp;&nbsp;<input type="reset" value="&nbsp;Reset&nbsp;" />
</form>[/code]

could you please help out?

Share this post


Link to post
Share on other sites
Use the switch construct:

[code]switch ($_POST['emailto'])
  {
  case "artem":
      $emailto = "artem@gmail.com";
      break;
  case "kris":
      $emailto = "kris@gmail.com";
      break;
  default:
      die("Missing or invalid recipient");
      break;
  }[/code]

Travis

Share this post


Link to post
Share on other sites
That [url=http://us2.php.net/manual/en/control-structures.switch.php]switch[/url] block will assign a certain value to $emailto depending on the value of $_POST['emailto']. Switch does basically the same thing as a series of If statements.

So, in the example above, if $_POST['emailto'] equals "artem", then $emailto gets set to "artem@gmail.com".

Share this post


Link to post
Share on other sites
It tells me that there is an error now.

Here is the full code to the contact.php. Can you look over it to see waht the error is?

[code]<?php

session_start();
//Email Validation
function checkEmail($email)
{
  if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email))
  {
      return FALSE;
  }

  list($Username, $Domain) = split("@",$email);

  if(@getmxrr($Domain, $MXHost))
  {
      return TRUE;
  }
  else
  {
      if(@fsockopen($Domain, 25, $errno, $errstr, 30))
      {
        return TRUE;
      }
      else
      {
        return FALSE;
      }
  }
}


// get posted data into local variables
switch ($_POST['EmailTo']){
  case "artem":
      $emailto = "skter4938@gmail.com";
      break;
  case "kris":
      $emailto = "skter4938@hotmail.com";
      break;
}
$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));
$Name = Trim(stripslashes($_POST['Name']));
$Subject = Trim(stripslashes($_POST['Subject']));
$Message = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
if(checkEmail($EmailFrom) == FALSE){
$error_msg[] = "Please enter in a valid email address.";
}
if (Trim($Name)==""){
$error_msg[] = "Please enter in a name.";
}
if (Trim($Subject)=="") {
$error_msg[] = "Please enter in a subject.";
}
if (Trim($Message)==""){
$error_msg[] = "Please enter in a message.";
}
//triggers error message
if ($error_msg) {
$_SESSION['error_msg'] = $error_msg;
header ('Location: index.php');
exit();
}
//sends email
else {
$Body = "You have a new email from $Name.\n\n Reply to $EmailFrom.\n\n $Name says,\n $Message";
$success = mail($EmailTo, $Subject, $Body);
}
//redirect to success page
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../index.php\">";
}
else{
  print "Email could not be sent due to errors. Please email the <a href='mailto:skter4938@gmail.com>webmaster</a>.";
}
?>[/code]

Share this post


Link to post
Share on other sites
In the switch block you're using [b]$emailto[/b], but in the mail function you have [b]$EmailTo[/b] instead. Variable names are case-sensitive. Also, it's unnecessary to trim those POST variables more than once.

Share this post


Link to post
Share on other sites

×

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.