Page 1 of 1

trouble selecting email from a drop down menu

Posted: Sun Sep 26, 2010 7:51 pm
by anoveskey
Hello all. I am new here.

On my website I am attempting to create a form to send email. In the form I have a drop down menu with 4 different email addresses. When a user clicks 'Submit' a php form should retrieve the info from the form and fire it off to the email address chosen from the drop down menu. Problem is, its not working. I was wondering if anyone here could let me know what I'm doing wrong!

Here is the code from the form:

<form name="form1" method="post" action="http://www.mywebsite.com/contact/send_contact.php">
To:
<select name="to">
<option value="fanmail@mywebsite.com">Fan Mail</option>
<option value="hatemail@mywebsite.com">Hate Mail</option>
<option value="booking@mywebsite.com">Booking</option>
<option value="webmaster@mywebsite.com">Webmaster</option>
</select>
Subject:<input name="subject" type="text" id="subject" size="50">
Detail:<textarea name="detail" cols="50" rows="4" id="detail"></textarea>
Name:<input name="name" type="text" id="name" size="50">
Email:<input name="customer_mail" type="text" id="customer_mail" size="50">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>

and here is the php file that should be processing the form:

Code: Select all

<?php

$subject ="$subject";
$message="$detail";
$mail_from="$customer_mail";
$header="from: $name <$mail_from>";
$to ='$_REQUEST["to"]';
$send_contact=mail($to,$subject,$message,$header);

if($send_contact){
echo "Thank you! I hope to hear from you again soon!";
}
else {
echo "ERROR";
}
?>
ANY help would be appreciated!

Re: trouble selecting email from a drop down menu

Posted: Mon Sep 27, 2010 7:19 am
by robnet
Have you tried echoing out $to on the mail sending page?
I suspect it's treating $_REQUEST["to"] as a string because you've surrounded it in single quotes. Try taking away these and see what happens

From:

Code: Select all

$to ='$_REQUEST["to"]';
To:

Code: Select all

$to =$_REQUEST["to"];

Re: trouble selecting email from a drop down menu

Posted: Mon Sep 27, 2010 7:28 am
by robnet
Also, I strongly suggest you don't use this code in a live site. - You should be validating all user input on the server side before using it at all. It's trivial to insert a different email address into your form and send it from a browser and submit it to your server, which will currently blindly email anything to anyone. - Great for spammers :wink:

I suggest having an ID as the value in your select options and looking this up in send_contact.php. This has the added bonus of not publicising your email addresses for easy scraping by spam robots..

eg:

Code: Select all

<select name="to">
<option value="0">Address 1</option>
<option value="1">Address 2</option>
</select>

Code: Select all

$allowed_emails=array('emailaddress@domain.com','emailaddress2@domain.com');
if(isset($_POST['to']) && array_key_exists($_POST['to'],$allowed_emails)){
  $to=$allowed_emails[$_POST['to']];
}

Re: trouble selecting email from a drop down menu

Posted: Mon Sep 27, 2010 7:39 am
by DigitalMind

Code: Select all

$my_emails = array('fanmail@mywebsite.com', 'hatemail@mywebsite.com', 'booking@mywebsite.com', 'webmaster@mywebsite.com');
$to =$_POST["to"];
if (!in_array($to, $my_emails)) {
    die('Spam!');
}
$subject =$_POST["subject"];
$message=$_POST["detail"];
$mail_from=$_POST["customer_mail"];
$header="From: $name <$mail_from>\r\n";
$send_contact=mail($to,$subject,$message,$header);

if($send_contact){
echo "Thank you! I hope to hear from you again soon!";
}
else {
echo "ERROR";
}
?>

Re: trouble selecting email from a drop down menu

Posted: Tue Sep 28, 2010 12:06 am
by anoveskey
Thanks guys! I figured the possibility existed for spammers to abuse the form in some way, but I couldn't see how. I will rewrite the code to be more security minded and post again if it works... or if it doesn't. :wink:

Re: trouble selecting email from a drop down menu

Posted: Wed Sep 29, 2010 2:25 am
by anoveskey
hey guys! I reworked the code a little and it still doesn't seem to be working. I'm not getting any errors, the emails just aren't going to the designated locations. I used a little bit of both of your code examples and here is what I came up with.

here is the php code:

Code: Select all

<?php

$my_emails = array('fanmail@mywebsite.com', 'hatemail@mywebsite.com', 'booking@mywebsite.com', 'webmaster@mywebsite.com');
if(isset($_POST['to']) && array_key_exists($_POST['to'],$my_emails)){
  $to=$allowed_emails[$_POST['to']];
}
else { die('Nice try, Buddy.'); }

$subject =$_POST["subject"];
$message=$_POST["detail"];
$mail_from=$_POST["customer_mail"];
$header="From: $name <$mail_from>\r\n";
$send_contact=mail($to,$subject,$message,$header);

if($send_contact){
echo "Thank you! I hope to hear from you again soon!";
}
else {
echo "ERROR";
}
?>
and here is the html form:

Code: Select all

<form name="form1" method="post" action="http://www.mywebsite.com/cgi/send_contact.php">
	To:
	<select name="to">
		<option value="0">Fan Mail</option>
		<option value="1">Hate Mail</option>
		<option value="2">Booking</option>
		<option value="3">Webmaster</option>
	</select><br />
	Subject:<input name="subject" type="text" id="subject" size="50"><br />
	Detail:<textarea name="detail" cols="50" rows="4" id="detail"></textarea><br />
	Name:<input name="name" type="text" id="name" size="50"><br />
	Email:<input name="customer_mail" type="text" id="customer_mail" size="50"><br />
	<input type="submit" name="Submit" value="Submit"> 
	<input type="reset" name="Submit2" value="Reset">
</form>
once again, I want to thank you for your help. I'm still new to PHP so it is greatly appreciated!

Re: trouble selecting email from a drop down menu

Posted: Wed Sep 29, 2010 3:19 am
by robnet
First, make sure you have error reporting turned on. - I don't think this is the problem in this case, but it's always worth being sure.

If you're using a local server I wouldn't be surprised if email was being dropped because it's not a trusted mail server. In fact, even if you're using a server online it can cause a lot of problems if you try to send email if the server isn't trusted to send for the domain in your 'From:' field. Your best bet is to use a trusted SMTP server. Eg, if you use gmail as your normal mail provider, use their servers to send your email. Then there should be nothing (except potential dodgy content) different about an email your website sends compared to one you send yourself.

You'll need to use something more advanced than mail() to do this. Give phpmailer a look: http://sourceforge.net/projects/phpmailer/


Also, using the users address as 'From' can be a problem causing it to be flagged as spam. - It could be as simple as the user not using a real address, or that the users domain specifies where email is allowed to be sent from. (SPF records). *It's worth checking your users email address is correctly formed either way..

It's safe to have the users email address as the 'Reply-To' header. And even as a name in the From - eg:
You could use this, which can cause problems:
From: "Users name" <users_email@domain.com>
Or this:
From: "users_email@domain.com" <your_address@gmail.com>

Re: trouble selecting email from a drop down menu

Posted: Wed Sep 29, 2010 3:55 am
by DigitalMind
anoveskey wrote:

Code: Select all

$my_emails = array('fanmail@mywebsite.com', 'hatemail@mywebsite.com', 'booking@mywebsite.com', 'webmaster@mywebsite.com');
if(isset($_POST['to']) && array_key_exists($_POST['to'],$my_emails)){
  $to=$allowed_emails[$_POST['to']];
}
replace $allowed_emails with $my_emails.

Code: Select all

$to=$my_emails[$_POST['to']]

Re: trouble selecting email from a drop down menu

Posted: Wed Sep 29, 2010 10:29 pm
by anoveskey
Hey guys. I turned on error reporting (found out that the $name variable was not defined) and made a few changes to the code. I should have seen that $my_emails + $accepted_emails thing the first time. Anyway I am using GoDaddy as my webhost. I didn't see anything on their website regarding the problems I am having. :banghead:

Here is the updated code:

send_contact.php:

Code: Select all

<?php

error_reporting(E_ALL);

$my_emails = array('fanmail@mywebsite.com', 'hatemail@mywebsite.com', 'booking@mywebsite.com', 'webmaster@mywebsite.com');
if(isset($_POST['to']) && array_key_exists($_POST['to'],$my_emails)){
  $to=$my_emails[$_POST['to']];
}
else { die('Nice try, Buddy.'); }

$subject =$_POST["subject"];
$message=$_POST["detail"];
$mail_from=$_POST["customer_mail"];
$name=$_POST["name"];
$header="From: $name <$mail_from>\r\n";
$send_contact=mail($to,$subject,$message,$header);

if($send_contact){
echo "Thank you! I hope to hear from you again soon!";
}
else {
echo "ERROR";
}
?>
and the HTML form:

Code: Select all

<form name="form1" method="post" action="http://www.mywebsite.com/cgi/send_contact.php">
	To:
	<select name="to">
		<option value="0">Fan Mail</option>
		<option value="1">Hate Mail</option>
		<option value="2">Booking</option>
		<option value="3">Webmaster</option>
	</select><br />
	Subject:<input name="subject" type="text" id="subject" size="50"><br />
	Detail:<textarea name="detail" cols="50" rows="4" id="detail"></textarea><br />
	Name:<input name="name" type="text" id="name" size="50"><br />
	Email:<input name="customer_mail" type="text" id="customer_mail" size="50"><br />
	<input type="submit" name="Submit" value="Submit"> 
	<input type="reset" name="Submit2" value="Reset">
</form>
If I am relying too heavily on your assistance let me know. Thanks!

Re: trouble selecting email from a drop down menu

Posted: Thu Sep 30, 2010 2:10 am
by robnet
Hi Anoveskey
Is the problem still that the email isn't being received? Have you checked your spam folder?

I assume you're not getting the 'Nice try, Buddy.' response and that the line calling the mail function is actually running. So you're seing the output 'Thank you! I hope to hear from you again soon!' rather than 'ERROR'.

If you are, it looks like your script is working. Though I still strongly advise you to checkout phpmailer and send via SMTP.


What domain are you using for the From field? It might be that this domain limits where email can come from - the from address may not be allowed to send from GoDaddy servers. - have a look at my previous post in this thread where I linked to phpmailer.

Re: trouble selecting email from a drop down menu

Posted: Thu Sep 30, 2010 2:57 am
by anoveskey
robnet wrote:Hi Anoveskey
Is the problem still that the email isn't being received? Have you checked your spam folder?

I assume you're not getting the 'Nice try, Buddy.' response and that the line calling the mail function is actually running. So you're seing the output 'Thank you! I hope to hear from you again soon!' rather than 'ERROR'.

If you are, it looks like your script is working. Though I still strongly advise you to checkout phpmailer and send via SMTP.


What domain are you using for the From field? It might be that this domain limits where email can come from - the from address may not be allowed to send from GoDaddy servers. - have a look at my previous post in this thread where I linked to phpmailer.
I do not have a spam folder. No, I'm not getting the "Nice try" response. So I guess the script is working.

I'm a little confused by the whole domain thing. I have been using my yahoo email account in the from field just to test it out, but I have also sent email to one of the four accounts from my yahoo web-based email and it gets to my domain just fine. I may be totally misunderstanding your question.

If I am not, then I think my next step may be to contact GoDaddy's customer support.

Re: trouble selecting email from a drop down menu

Posted: Thu Sep 30, 2010 3:19 am
by robnet
No worries - I'll try to clarify.

You telling your server (via setting the 'From:' header) that the email is from [you]@yahoo.com isn't enough to establish trust - this is basically how early spammers did it. Whereas if you send from within yahoo.com's email system they will have all the systems in place to make sure your email gets to its destination. (well, as sure as possible).

This is an overly simplified description, but it makes the point:
A mail server receiving your email might look at the headers and check the originating IP address against known trusted yahoo.com mail server IP addresses. If it finds that your server's IP address isn't trusted (Which it will) it might decide that your email is spam (which effectively, it is) and drop it completely.

Have you tried it without setting the from header? - This will usually mean your server will fill this in itself - it might be more trusted this way. - At least then you can see whether it works in theory.

I still urge you to look into phpmailer. This will completely bypass GoDaddy and you shouldn't have any problems.

If you do talk to GoDaddy's customer support, please post the results here - I'm sure people will find the resolution valuable.

Re: trouble selecting email from a drop down menu

Posted: Fri Oct 01, 2010 9:25 pm
by anoveskey
Hey guys!

I'm not sure if this solved my problem or not because I had a friend use the form and put in his email address last night and I received it. Anyway, here is what I did and finally concluded that the form/script is working:
  • 1. Send emails from it several times throughout the night from my yahoo email and a few other email addresses. Eventually, they started getting through.

    2. I contacted GoDaddy user support and they had me launch my hosting control center from their website. From there, I clicked on the 'Content' tab and then clicked on the 'Form Mail' icon. On the next page there is a checkbox next to 'Reinstall Default Scripts Directory'. I checked the checkbox and then clicked 'Continue'. On the next page you are asked to verify. To do this, just click 'Update'.
I know it works now, but I don't know if that was the direct solution to the problem or not. Worst case scenario, give it a try.

Thanks for all your help over the past week!