multiple website emails, one form for customers for all emai

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

multiple website emails, one form for customers for all emai

Post by bufhal »

Hello;
I hope someone can offer some help.
Our website has several email links to several departments. We are trying to get rid of them for spam purposes and offer a link to one form that the user will fill out (just subject and message) and upon submit, will go to prespective email in prespective department. We have 30-40 emails so a form for each one would not work.
Can someone offer an idea or premade code snippett that can solve our problem?
I appreciate any help.
Thank you in advance for your time.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: multiple website emails, one form for customers for all

Post by hawleyjr »

bufhal wrote:Hello;
I hope someone can offer some help.
Our website has several email links to several departments. We are trying to get rid of them for spam purposes and offer a link to one form that the user will fill out (just subject and message) and upon submit, will go to prespective email in prespective department. We have 30-40 emails so a form for each one would not work.
Can someone offer an idea or premade code snippett that can solve our problem?
I appreciate any help.
Thank you in advance for your time.
You have a few options. One option is to implement a CAPTCHA page.

You can also, do a drop down with a list of name (Don't list emails) and have the user select which person he/she is sending to.

The option you listed. "Have the form go to one email) is fairly simple.

Code: Select all

$send_to_email = 'info@example.com';
$send_from_email = $_POST['user_email'];//MAKE SURE YOU VALIDATE THIS EMAIL ADDRESS
$subject = $_POST['mssg_subj'];//MAKE SURE YOU CLEAN THIS INPUT
$body = $_POST['mssg_body'];//MAKE SURE YOU CLEAN THIS INPUT

//VALIDATE ALL FIELDS HERE

//SEND EMAIL
mail ( $send_to_email, $subject . ' From: ' . $send_from_email, $body  );
I want to point out that this is using the mail function in it's most simplistic form. There are other BETTER and MORE EFFECTIVE alternatives out there.
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

thank you

Post by bufhal »

I appreciate your reply. I think the best way to do this is a list the outside user can choose from. I am not sure if all this can be achieved in one form.
You can also, do a drop down with a list of name (Don't list emails) and have the user select which person he/she is sending to.

Is there a working example you know of or can you please show me how my existing HTML page (below) can "send" to the different emails?
We have around 20 email names.

Code: Select all

<html>
<head><title>Mail sender</title></head>
<center>
<body bgcolor="#d0d0d0">


<hr><br><h2>Please email us...</h2>
<br><br>

<form action="mail2.php" method="POST">

<p><b>Subject</b><br>
<input type="text" name="subject" size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>

</body></center>
</html> 
</code>


<?php

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}

/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail("gzakes@attotech.com",$subject,$message)) {
  echo "<h3>Thank you for sending email</h3>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Create an array of the email addresses, alas..

Code: Select all

<?php 

$emails = array(
   'Technical Support' => 'tech@domain.com',
   'Sales and Inquiry' => 'sales@domain.com'
);

//validation of your form takes place here
//add checks to make sure ALL the values are valid.. 
//I've only done for for the email_to drop down

if (!empty($_POST['email_to']) && in_array($_POST['email_to'], $emails)) {

   if (mail( ... )) {
      echo 'Email sent'; 
      exit(); //redirect maybe?
   }
}

?>

<html>
<head><title>Mail sender</title></head>
<center>
<body bgcolor="#d0d0d0">

<hr><br>
<h2>Please email us...</h2>
<br><br>

<form action="mail2.php" method="POST">
   <p><b>Subject</b><br>
   <input type="text" name="subject" size=40>
   <select name="email_to">

<?php
   foreach ($emails as $department => $email) { 
      echo '<option value="'. $email.'">'.$department.'</option>';
   }
?>

<p><b>Message</b><br>
<textarea cols=40 rows=10 name="message"></textarea>
<p><input type="submit" value=" Send ">
</form>

</body></center>
</html>
</code>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Printing the email address in the select box will not stop bots from grabbing the email address....

Code: Select all

$emails = array(
   'Technical Support' => 'tech@domain.com',
   'Sales and Inquiry' => 'sales@domain.com'
);

   foreach ($emails as $department => $email) {
      echo '<option value="'. $email.'">'.$department.'</option>';
   }
To...

Code: Select all

$emails = array(
   1 => 'tech@domain.com',
   2 => 'sales@domain.com'
);
$email_labels = array(
   1 => 'Technical Support',
   2 => 'Sales and Inquiry'
);

   foreach ($email_labels as $ak=> $label) {
      echo '<option value="'. $ak.'">'.$label.'</option>';
   }
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

Thank for your help.

Post by bufhal »

I will try to put this together and get it working. Thanks for your help and time.
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

Please take another look

Post by bufhal »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thank you for your help.
I am getting close, but this is not working.
I want the dropdown menu, when someone hits submit, to send the email(subject and message) to that person.
I appreciate your help.

Code: Select all

<?php 
$emails = array( 
   0 => 'et@email.com', 
   1 => 'jb@email.com' ,
   2 => 'rpec@email.com' ,
   3 => 'mkt@email.com' ,
  
); 
if (!empty($_POST['email_to']) && in_array($_POST['email_to'], $emails)) { 

   if (mail(  )) { 
      echo 'Email sent'; 
      exit(); 
   } 
} 
?><center> 
<body bgcolor="#d0d0d0"> 
<hr><br> 
<h2>Please email us...</h2> 
<br><br> 
<form action="mail2.php" method="POST"> 
   <p><b>Subject</b><br> 
   <input type="text" name="subject" size=50> 
   <br><br> 
   <select name="email_to"> 
	<option value="1">Ela Teirn -  Dir of Marketing</option> 
	<option value="2">Jen B -  Asia Sales</option> 
	<option value="3">Rick P -  Tech Support</option> 
	<option value="4">General Marketing Inquiry</option> 
	
	</select>
	<br><br>
<b>Message</b><br> 
<textarea cols=40 rows=10 name="message"></textarea>
<input type="submit" value=" Send "> 
</form>  </body></center> 
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

need the page link to fill in a email box on form

Post by bufhal »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


thank you for the info. Our company decided to switch tactics. Instead of a dropdown, they want this to work as follows. A person on a page of the site clicks on an email gif that contains the link to this form. The persons name will show in a box on this page (code below) and when the form is filled out and submitted it will go automatically to that email. I am somewhat stuck here. In addition to this code, the actual clickable gif needs to contain a variable to fill in the box on the form.
Please let me know how I can accomplsh this.
Thank you.

Code: Select all

<?php 
$emails = array( 
   0 => 'et@email.com', 
   1 => 'dd@email.com' ,
   2 => 'rpec@email.com' ,
   3 => 'mkt@email.com' ,
  
); 
if (!empty($_POST['email_to']) && in_array($_POST['email_to'], $emails)) { 

   if (mail(  )) { 
      echo 'Email sent'; 
      exit(); 
   } 
} 
?><center> 
<body> 

<h2>Please email us...</h2> 

<form action="mail2.php" method="POST"> 
   <p><b>Subject</b><br> 
   <input type="text" name="subject" size=50> 
   <br><br> 
   	<br><br>
<b>Message</b><br> 
<textarea cols=40 rows=10 name="message"></textarea>
<input type="submit" value=" Send "> 
</form></body></center> 
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

bufhal, please don't try to separate a single block of code into multiple highlighting blocks (especially if you're skipping segments).
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

ok, sorry about that

Post by bufhal »

ok, sorry about that
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$emails = array('et@email.com', 'dd@email.com' , 'rpec@email.com' , 'mkt@email.com');

if (!empty($_GET['email_to']) && in_array($_GET['email_to'], $emails)) {

   if (mail(  )) {
      echo 'Email sent';
      exit();
   }
}
Three things here

1. When assigning values to an array without keys, there is no need to automatically assign them. PHP will do that for you.
2. Since you want to append the email to a picture that you will click, wrap a link with ?email_to=email@goes.here around the appropriate image. This variable will be accessible through $_GET['email_to'] as shown in the code above
3. Make sure you will out the mail() function parameters. I just left it blank.
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

thank you

Post by bufhal »

Thank you very much, Jcart.

not sure what you mean here.
3. Make sure you will out the mail() function parameters. I just left it blank


So if I wrap my email gifs with

Code: Select all

?email_to=email@goes.here
And use your posted code with my form, this should work?

Thank you
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

php email image to populate email address on form

Post by bufhal »

Hi;
Still attempting to wrap some PHP around "images of the
email recipient's address" that, when clicked will
go to a form page. The form page has a subject and a
messagebox. When a visitor fills in those two boxes
and submits, the information will go to the email
address. I am stuck. Please foffer some assistance.
I appreciate any help.


Form Page:

Code: Select all

<?php
$emails = array('gz@email, 'dd@email.com' , 
'rpec@email.com' , 'mkt@email.com'); 

if (!empty($_GET['email_to']) && 

in_array($_GET['email_to'], $emails)) { 

   if (mail(  )) { 
      echo 'Email sent'; 
      exit(); 
   } 
}
?> 

<center> 
<body> 
 
<h2>Please email us...</h2> 


<form action="mail2.php" method="POST"> 
<p><b>Subject</b><br> 
<input type="text" name="subject" size=50> 
<br><br> 
<b>Message</b><br> 
<textarea cols=40 rows=10 

name="message"></textarea><br><br>
<input type="submit" value=" Send "> 
</form>
</body></center> 
</html>
Email link with image:
<map name="Map">
<area shape="rect" coords="0,-2,153,14"
href="mail4.html">
</map>
<a href="?email_to=" img src="images/Ed_email.gif"
width="152" height="14" border="0" usemap="#Map">
Last edited by bufhal on Fri Oct 27, 2006 9:20 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Anything look funny with our syntax highlightng to you? ;)
bufhal
Forum Newbie
Posts: 19
Joined: Sat Apr 17, 2004 11:39 am

Post by bufhal »

yeah it looks red...........................
Post Reply