Jump to content


Photo

multiple website emails, one form for customers for all emails?


  • This topic is locked This topic is locked
42 replies to this topic

#1 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 25 October 2006 - 02:42 PM

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 point me to a premade code snippett that can solve our problem?
I appreciate any help.
Thank you in advance for your time.


#2 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 25 October 2006 - 04:24 PM

Similar topic here - http://www.phpfreaks....html#msg456389

#3 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 25 October 2006 - 08:02 PM

Thank you for the link. 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.


<?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>



#4 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 25 October 2006 - 08:37 PM

mail2.php

<?php

$recipient = $_POST['email_to']; //Retrieve POST value from dropdown in previous page containing the form

switch ($recipient) {

	default:
		//send email to webmaster
		$toaddress = "webmaster@mysite.com";
	break;

	case "1":
		//send email to person1@somewhere.com
		$toaddress = "et@email.com";
	break;
	
	case "2":
		//send email to person2@somewhereelse.com
		$toaddress = "jb@email.com";
	break;

}

//Call the function that sends the email
functionToMail($toaddress, $sub, $emailbody, $fullname, $fromaddress);




function functionToMail($to, $subject, $body, $name, $email_address) {

	// Let's mail the user!
		$headers = "From: \"".$name."\" <".$email_address.">";

		mail($to, $subject, $body, $headers);

}

?>


You would still need to assign the POST values from the form to the relevant variables like $emailbody, $name, $email_address, etc. but this should be enough to get you going again. Also, you HAVE to sanitize the POST values. Validate each field.

The thread I pointed you to earlier contained a nice snippet where an array was used rather than a switch, but I must admit that I am not too sure how that works yet, I will stick to my trusty switch until I can work it out! ;)

#5 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 26 October 2006 - 06:01 PM

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.

<?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>
<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> 
   	<br><br>
<b>Message</b><br> 
<textarea cols=40 rows=10 name="message"></textarea>
<input type="submit" value=" Send "> 
</form>
</body></center>
</html>



#6 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 01:51 PM

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:

<?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>
<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> 
<b>Message</b><br> 
<textarea cols=40 rows=10 

name="message"></textarea><br><br>
<input type="submit" value=" Send "> 
</form>
<br><br>
<hr>
</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">

</a>




#7 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 27 October 2006 - 02:49 PM

Apart from the fact that images should not be used where text could be used (due to accessibility issues) the concept is still the same, and my code should do the trick. Also, by using GET, you open the possibility of your site to injection attacks unless you sanitize the input very well.

Is there something specific that is not working? Is the code that you have supplied in your example complete, or have you erased certain things for this post? I don't quite understand which part of your scenario is failing...

#8 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 03:02 PM

I think the image link syntax is wrong, . My last post contains all the code, the first for the form page and the second for the webpage email image link.

#9 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 03:11 PM

I have a question you may think is dumb, I am trying to understand the array. When I click on the image, it brings the form up and when I fill out and submit, it goes to the first entry(recipient) in the array, gz@email.com. Since there are 15-20 emails I need to enter, how can the image links know which email in the array to "choose" when it is clicked?
I am learning this on the fly and appreciate your patience and hanging in there with me.


#10 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 27 October 2006 - 03:31 PM

Well if you had seperate images for each email address (and you really do need to consider whether this is the best way to do it) then you would just need to define the GET variable...

<a href="?email_to=gz" img src="images/Ed_email.gif" width="152" height="14" border="0" >

Then in your array code, you would need to define keys that correspond to the GET values, which have the actual email addresses as their values.

<?php

$emails = array(

'gz' => 'gz@email',
'dd' => 'dd@email.com',
'rpec' => 'rpec@email.com',
'mkt' => 'mkt@email.com'

);

?>

I am not too sure about your image map though...

#11 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 03:42 PM

there are multiple email links, some people/departments have several emails within the site but one image per should d the trick. Your code makes sense, the email must be stored within the php array so it cannot be found/viewed..
I will try this and again thank you. I will let you know..

#12 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 04:16 PM

trying to find what is causing Parse error

<?php
$emails = array(

'gz' => 'gz@attotech.com',
'dd' => 'dd@email.com',
'rpec' => 'rpec@email.com',
'mkt' => 'mkt@email.com'
)
if (!empty($_GET['email_to']) && in_array($_GET['email_to'], $emails)) {

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

#13 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 27 October 2006 - 07:06 PM

You are missing the semicolon ; after the closing bracket of the array )

#14 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 27 October 2006 - 07:47 PM

Thank you for ALL your help, gmwebs.
All set. I appreciate it...

#15 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 27 October 2006 - 09:24 PM

No problem, glad you got it sorted in the end. Might want to mark this as solved in the subject line...

#16 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 30 October 2006 - 03:23 PM

I am sorry, upon further testing I was wrong. This is not working correctly. Can you please take a look and see why the "thank you" page code will not grab the array email specified on the first page. Thank you again in advance.
The page with the email gif link is first:
E-mail: <a href="mail4.php?email_to=mkt"><img src="images/Ed_email.gif" width="152" height="14" border="0"></a>

That email link should grab the correct email address from the array on "mail4.php":
<?php
$emails = array(
'mkt' => 'mkt@tech.com',
'dd' => 'dd@email.com',
'rpec' => 'gz@tech.com',
'gz' => 'mkt22@tech.com'
);
if (!empty($_GET['email_to']) && in_array($_GET['email_to'], $emails)) {

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


The "thank you" page, "mail2.php" that actually sends the email is not grabbing the email address from the array. I left the comments in

<?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($emails,$subject,$message)) {

echo "<h3>Thank you for sending email</h3>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>


#17 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 30 October 2006 - 05:24 PM

Ok... I think I see it. You click on a graphic which links through to mail4.php. It correctly finds the email address there. But from there, it is not submitting the email because the $email variable is empty in mail2.php?

Also, in mail2.php on the elseif line, should it not be $email and not $emails?

What makes the script go to mail2.php? Is there a form that is included in mail4.php? If there is, you would need to include the value of the email address which is derived from the array in mail4.php as a hidden value, or you would need to use session variables.

Why are there so many scripts involved?

#18 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 30 October 2006 - 06:25 PM

Thanks for the reply.
This is the form on the mail4.php page:

<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>


#19 bufhal

bufhal
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 30 October 2006 - 06:41 PM

Also, in mail2.php on the elseif line, should it not be $email and not $emails?


I switched to $email, still no dice.

Is there a form that is included in mail4.php? If there is, you would need to include the value of the email address which is derived from the array in mail4.php as a hidden value, or you would need to use session variables.


Sorry, gmwebs, not quite sure how to do this.

#20 gmwebs

gmwebs
  • Members
  • PipPipPip
  • Advanced Member
  • 174 posts
  • LocationLondon

Posted 30 October 2006 - 10:35 PM

Could you perhaps detail each of your scripts, as well as the files that contain the form that your users will use to submit their contact request, and tell me what each one does, and the process that should be followed? I hope you don't mind going through the troubleshooting with me, but I hope you will learn something this way rather than me just providing you with a copy and paste script?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users