Jump to content

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


bufhal

Recommended Posts

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 [b]one form [/b] 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.
Link to comment
Share on other sites

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.


[code]<?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();
   }
}
?> [/code] <center>
<body bgcolor="#d0d0d0">
<hr><br>
<h2>Please email us...</h2>
<br><br>
[code]<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> [/code] </body></center>
</html>

Link to comment
Share on other sites

mail2.php

[code]
<?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);

}

?>

[/code]

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! ;)
Link to comment
Share on other sites

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]<?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();
   }
}
?> [/code] <center>
<body>
<hr><br>
<h2>Please email us...</h2>
<br><br>
[code]<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> [/code] </body></center>
</html>

Link to comment
Share on other sites

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.


[b]Form Page:[/b]

[code]<?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();
  }
}
?> [/code]

<center>
<body>
<hr><br>
<h2>Please email us...</h2>
<br><br>
[code]<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>[/code]<br><br>
<hr>
</body></center>
</html>


[b]Email link with image:[/b]
<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>


Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

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

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.

[code]
<?php

$emails = array(

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

);

?>
[/code]

I am not too sure about your image map though...
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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();
   }
}
?>
Link to comment
Share on other sites

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.
[b]The page with the email gif link is first:[/b]
E-mail: <a href="mail4.php?email_to=mkt"><img src="images/Ed_email.gif" width="152" height="14" border="0"></a>

[b]That email link should grab the correct email address from the array on "mail4.php":[/b]
[code]<?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();
  }
}
?> [/code]

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

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

echo "<h3>Thank you for sending email</h3>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>[/code]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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

[code]<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>[/code]
Link to comment
Share on other sites

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

I switched to $email, still no dice.

[quote]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.[/quote]

Sorry, gmwebs, not quite sure how to do this.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

absolutely, I appreciate your help.
Throughout our website there soon to be multiple email gif links that, when clicked, will bring up the form page mail4.php. The link goes to the mail4.php page but thats all it does. It should recognise a variable in the array. As of now, when someone clicks the form submit, it is going to the mail2.php elseif, which I thought I correctly fixed(It had my email in it and I would recieve the form results, however, it should not reach the elseif statement if working correctly and someone fills out both form fields.
I know this is overly complicated but the whole idea is to have the 50-70 emails on our site replaced with gif's which will offer the form.
Thank you, I hope this clarifies somewhat. I am learning on the fly mand appreciate your help and patience.
Link to comment
Share on other sites

Ok, so in mail4.php we need to retrieve the value from the gif and assign it to a hidden value in the form.

[code]
<?php

$email = $_GET['email_to']; //Get the value of the email_to variable in the gif. Might need to use $HTTP_GET_VARS instead of $_GET but try this first

?>
<form action="mail2.php" method="POST">
   <input type="hidden" name="email" value="<?php echo $email; ?>">
<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>
[/code]

And I am pretty sure that you need to move the email array in this file to mail2.php. The idea is that the hidden value is sent to mail2.php, and then it finds the email address based on what this value is.
Link to comment
Share on other sites

Thank you for hanging in there with me.  Still not sending email. Here is what I have for the three files.
have this for link:

E-mail: [code]<a href="mail4.php?email_to=mkt"><img src="images/Ed_email.gif" width="152" height="14" border="0"></a>[/code]
mail4.php:
[code]<?php
$email = $HTTP_GET_VARS['email_to'];
//Get the value of the email_to variable in the gif. Might need to use $HTTP_GET_VARS instead of $_GET but try this first
?>
<form action="mail2.php" method="POST">
   <input type="hidden" name="email" value="<?php echo $email; ?>">
<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> [/code]

mail2.php:

[code]<?php
$emails = array(
'mkt' => 'mkt@tech.com',
'dd' => 'dd@email.com',
'rpec' => 'gz@tech.com',
'gz' => 'mkt@tech.com'
);
if (!empty($_GET['email_to']) && in_array($_GET['email_to'], $emails)) {

   if (mail(  )) {
      echo 'Email sent';
      exit();
   }
}
?>
<?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($email,$subject,$message)) {
echo "<h3>Thank you for sending email</h3>";
} else {
  echo "<h4>Can't send email to $email</h4>";
}
?>  [/code]
Link to comment
Share on other sites

[code]
<?php
$emails = array(
'mkt' => 'mkt@attotech.com',
'dd' => 'dd@email.com',
'rpec' => 'gzakes@attotech.com',
'gz' => 'mkt@attotech.com'
);

$email = $HTTP_POST_VARS['email'];

echo $email; //let's see what the value of $email is

if (!empty($email) && in_array($email, $emails)) {

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

$echo $email_addr; //let's see what the value of $email_addr is
exit(); //end the script to see the output

/* 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($email_addr,$subject,$message)) {
echo "<h3>Thank you for sending email</h3>";
} else {
  echo "<h4>Can't send email to $email</h4>";


}
?>
[/code]
Link to comment
Share on other sites

getting a parse error:
Parse error: parse error, unexpected T_VARIABLE in /home/attonet/www/atto.net/mail2.php on line 311

307  /* All form fields are automatically passed to the PHP script through the array
308  $HTTP_POST_VARS. */
309  $email_addr = $emails['email'];
310  $subject = $HTTP_POST_VARS['subject'];
311  $message = $HTTP_POST_VARS['message'];

$echo $email_addr; //let's see what the value of $email_addr is
exit(); //end the script to see the output
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.