Jump to content

Email - drop down list


Juanita
 Share

Recommended Posts

Hi guys,

 

I'm trying to work this out on my own, but I'm running out of time to figure this out and my PHP skills are newbie level.

 

Here's the deal, I'm creating a contact form that has a drop down box with multiple contacts. I tested to make sure that I can send an actual email to my email address, so that's no problem. It worked just fine after going through hoops of getting that to work. The issue is that it was only 1 email address. I need this to work for multiple email addresses.

 

This is the test code I was using:

 

<?php

$msg="";
if(isset($_POST['submit']))
{
   

	$from_add = "default@123.com"; 

	$to_add = "default@456.com"; 
	$subject = "Test Subject";
	$message = "Test Message";
	
	$headers = "From: $from_add \r\n";
	$headers .= "Reply-To: $from_add \r\n";
	$headers .= "Return-Path: $from_add\r\n";
	$headers .= "X-Mailer: PHP \r\n";
	
 /*?>	[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =<?php */
	
	if(mail($to_add,$subject,$message,$headers)) 
	{
		$msg = "Mail sent OK";
	} 
	else 
	{
 	   $msg = "Error sending email!";
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>Test form to email</title>
</head>

<body>
<?php echo $msg ?>
<p>
<form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>

</form>
</p>


</body>
</html>

Now I was thinking that I would just be able to add the email array to $to_add using something like this, but clearly I'm messing up because it keeps displaying the error before I even hit the submit button:

 

<?php

$msg="";
if(isset($_POST['submit']))
{
  
	$from_add ='default@123.com' 

	$to_add = array('choice 1' => 'default@1234.com', 'choice 2' => default2@1234.com');

	$subject = "Test Subject";
	$message = "Test Message";
	
	$headers = "From: $from_add \r\n";
	$headers .= "Reply-To: $from_add \r\n";
	$headers .= "Return-Path: $from_add\r\n";
	$headers .= "X-Mailer: PHP \r\n";
	
 /*?>	[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =<?php */
	
	if(mail($to_add,$subject,$message,$headers)) 
	{
		 if(array_key_exists($_POST['dropdown']))  

 {  
		$msg = "Mail sent OK";
	} 
	}
	else 
	{
 	   $msg = "Error sending email!";
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>Test form to email</title>
</head>

<body>
<?php echo $msg ?>
<p>
<form action='<?php echo htmlentities($_SERVER['PHP_SELF']); ?>' method='post'>
<select name='dropdown' method="post">  

   <option value=''>Please Choose Dept.</option>  

   <option value='choice 1'>Boating</option>  

   <option value='choice 2'>Camping</option>  

 </select>
<input type='submit' name='submit' value='Submit'>
</form>
</p>


</body>
</html>

 

Can someone help me out with this please? :happy-04:

Edited by Juanita
Link to comment
Share on other sites

*what* error?

 

Is that your actual code? You're missing a quote. Look at the colors in the syntax highlighting.

 

However, the mail function does not accept an array for the $to param. You will either need to loop through the array, implode the array, or use something more advanced like PHP Mailer.

Edited by Jessica
Link to comment
Share on other sites

So because I'm a newbie with this can anyone actually guide me with code. You don't have to give it away because I know coding is a big secret of sorts, but just guide me in the right direction with code. That would be wonderful.

Link to comment
Share on other sites

Coding is not a big secret...

 

PHP.net has pretty much everything you need to know about PHP. I'd say PHP has one of the best manuals / documentation out there. (I'm tempted to say it is the best.)

 

As Jessica stated, you can search PHP.net for the mail function.

 

Jessica, I really like this site you used a while ago. :) http://lmgtfy.com/?q=PHP+mail+function&l=1

 

Also, as kind of a survey for me, where have you "seen folks use $emailAddresses" or learned that coding is a big secret? Not being rude, I just want to learn what your process is. This could help me understand how to help you guys/girls better.

Link to comment
Share on other sites

I don't think she's trying to send a list of emails. I think she is trying to send one email but to a different address depending on the option selected in the form.

 

So something like this would set the $to_add variable to the required email address:

switch( $_POST['dropdown'] ) {
    case "choice 1":
        $to_add = "email@email.com";
        break;
    case "choice two":
        $to_add = "email2@email.com";
        break;
}

This:

case "choice 1":

...is used when this option in the form is selected:

<option value='choice 1'>Boating</option> 

...Notice the option value and the value of $_POST['dropdown']

 

 

Btw. just dropping this code into your script won't work, there are one or two semi-obvious things to change.

 

The two posters in here can post links to the PHP mail function all day long but it isn't going to help with this problem.

Edited by johnsmith153
Link to comment
Share on other sites

I don't think she's trying to send a list of emails. I think she is trying to send one email but to a different address depending on the option selected in the form.

 

I'd say she want's to send one email to multiple people as per her original question.

 

if(isset($_POST['submit']))
{
  
	$from_add ='default@123.com' 

	$to_add = array('choice 1' => 'default@1234.com', 'choice 2' => default2@1234.com');

	$subject = "Test Subject";
	$message = "Test Message";
	
	$headers = "From: $from_add \r\n";
	$headers .= "Reply-To: $from_add \r\n";
	$headers .= "Return-Path: $from_add\r\n";
	$headers .= "X-Mailer: PHP \r\n";
	
 /*?>	[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25

; For Win32 only.
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =<?php */
	
	if(mail($to_add,$subject,$message,$headers)) 
	{
		 if(array_key_exists($_POST['dropdown']))  

 {  
		$msg = "Mail sent OK";
	} 
	}
	else 
	{
 	   $msg = "Error sending email!";
	}
}

 

 

 

 

The two posters in here can post links to the PHP mail function all day long but it isn't going to help with this problem.

 

The PHP mail function link tells you exactly how to do it.

 

 

to

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User <user@example.com>
  • User <user@example.com>, Another User <anotheruser@example.com>

 

As for the "error". We still don't know what error message she is referring to since she hasn't posted it.

Link to comment
Share on other sites

I'm pretty sure she wants to send one email, not a list of email addresses. I think if you read it again and see the form at the very, very end of her post (and code) you'll see what I mean. The form asks to select an option in a dropdown and those options relate to her array of email addresses.

 

The error is no doubt as Jessica pointed out in post #2, which I'm guessing she would have solved. There may be more errors of course.

Link to comment
Share on other sites

This is exactly what I'm trying to figure out. Thank you for the help thus far.

 

I don't think she's trying to send a list of emails. I think she is trying to send one email but to a different address depending on the option selected in the form.

 

So something like this would set the $to_add variable to the required email address:

switch( $_POST['dropdown'] ) {
    case "choice 1":
        $to_add = "email@email.com";
        break;
    case "choice two":
        $to_add = "email2@email.com";
        break;
}

This:

case "choice 1":

...is used when this option in the form is selected:

<option value='choice 1'>Boating</option> 

...Notice the option value and the value of $_POST['dropdown']

 

 

Btw. just dropping this code into your script won't work, there are one or two semi-obvious things to change.

 

The two posters in here can post links to the PHP mail function all day long but it isn't going to help with this problem.

Link to comment
Share on other sites

if(mail($to_add,$subject,$message,$headers)) { if(array_key_exists($_POST['dropdown'])) { $msg = "Mail sent OK"; } } else { $msg = "Error sending email!"; }

 

As soon as I ran the script in a browser it said 'Error sending email.'

 

I'd say she want's to send one email to multiple people as per her original question.


 

 

 

 

 

The PHP mail function link tells you exactly how to do it.

 

 

As for the "error". We still don't know what error message she is referring to since she hasn't posted it.

Edited by Juanita
Link to comment
Share on other sites

It helps to be as detailed as possible when describing the problem.

 

You still don't need a switch statement, just an array. Use the array to populate the dropdown, then select that key from the array to send the email.

 

IE:

<?php
$emails = array(
   1=>array('name'=>'Boating', 'email'=>'boating@email.com'),
   2=>array('name'=>'Camping', 'email'=>'camping@email.com')
);

if(isset($_POST['send_to']) && isset($emails[$_POST['send_to']])){
   $to_email = $emails[$_POST['send_to']]['email'];
}

echo '<select name="send_to">';

foreach($emails AS $id=>$info){
   echo '<option value="'.$id.'">'.$info['name'].'</option>';
}
echo '</select>';
Edited by Jessica
Link to comment
Share on other sites

If I had the time to go through the manuels then I definitely would. I really want to learn PHP, but right now it's crunch time at work and so I just need to code this last bit for this site that I'm trying to hurry up and get out there on the web. That's why I'm just wanting to speak code right now and not read through everything. I've been searching for bits and pieces of code online and I've been trying to figure the rest out on my own. The only reason why I said coding seems like some big secret is because wants you start dealing with server side coding everyone wants to be a professor. That's all well and good if I wasn't running out of time. Right now I just need to get straight to the point. Because I don't know PHP I've been searching for some weeks now, on top of trying to get emails to even send to the work email from a contact form through our hosting server.

 

I appreciate all the help. :-)

Coding is not a big secret...

 

PHP.net has pretty much everything you need to know about PHP. I'd say PHP has one of the best manuals / documentation out there. (I'm tempted to say it is the best.)

 

As Jessica stated, you can search PHP.net for the mail function.

 

Jessica, I really like this site you used a while ago. :) http://lmgtfy.com/?q=PHP+mail+function&l=1

 

Also, as kind of a survey for me, where have you "seen folks use $emailAddresses" or learned that coding is a big secret? Not being rude, I just want to learn what your process is. This could help me understand how to help you guys/girls better.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

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