Log in

View Full Version : email encrypted in form



stardom
12-10-2007, 12:36 AM
hello guys, I'm not a programmer but I need some help with a form.

1) imagine to have a few names, like Jack, Frank, Mary. To each name an email address is assigned (but it's not visible)

2) next to the list of names there's a form with fields "send to", "your email" and "your message"

3) when the user clicks on a name, its corresponding email address appears in the "send to" field, and once submit button is pressed the message is sent to that person.

Hope someone can spare a couple of minute and write this little script for me... Thank you. Criss

djr33
12-10-2007, 01:02 AM
A much better idea would be to just have a reference to their name, then on the server side script which interprets it send to the corresponding email. "Jack" becomes "Jack@your.com", and the visitor never knows the real address, nor do spambots.

stardom
12-10-2007, 04:59 PM
yes, a reference like "manager, supervisor," etc.. and this title could appear in the "send to" box, instead of an email....

but my problem is that I have no idea how to write such a script anyway. Can you help writing a few lines for me?

boogyman
12-10-2007, 06:31 PM
if you only have a couple of people you could use radio buttons



<form name="frmEmail" action="sendEmail.php" method="post">
<label>
<span>Receipient:</span>
<input type="radio" name="ureceipient" value="jack"> Jack
<input type="radio" name="ureceipient" value="frank"> Frank
<input type="radio" name="ureceipient" value="mary"> Mary
</label>
<label>
<span>Message:</span>
<textarea cols="10" rows="5" name="ucomment"></textarea>
</label>
<input type="submit" name="sumbit" value="Send Email">
</fieldset>
</form>


if you would prefer a drop down you can do


<form name="frmEmail" action="sendEmail.php" method="post">
<label>
<span>Receipient:</span>
<select name="ureceipient">
<option value="jack">Jack</option>
<option value="frank">Frank</option>
<option value="mary">Mary</option>
</select>
</label>
<label>
<span>Message:</span>
<textarea cols="10" rows="5" name="ucomment"></textarea>
</label>
<input type="submit" name="sumbit" value="Send Email">
</fieldset>
</form>


and sendEmail.php would look like


<?php
// Define the email addresses for the specific names
$to = array(
"jack" => "jack@domain.com",
"frank" => "frank@domain.com",
"mary" => "mary@domain.com"
);
// Populate the email "to" field
$receipient = $to[$_POST['ureceipient']];

// Send Email
mail($receipient,'Subject','Message');
?>


if you wanted to send to multiple people at the same time, then that is doable too, just let us know and a script will be given. if you have any questions just ask, sorry for the very rough example

stardom
12-11-2007, 12:11 AM
thanks a lot for taking your time in helping me...
I actually don't need radio buttons or drop menu either. Have a look at www.arsenalemusica.eu/staff.php and I think you'll understand what I need..i think it''s an easy one, but I have no idea how to build it

Criss

Master_script_maker
12-11-2007, 12:47 AM
edited from boogyman's post


<form name="frmEmail" action="sendEmail.php" method="post">
<span>Receipient:</span>
<input type="text" name="ureceipient" value="Enter Name" />
<span>Message:</span>
<textarea cols="10" rows="5" name="ucomment">Enter Message</textarea>
</label>
<input type="submit" name="submit" value="Send Email">
</fieldset>
</form>
sendEmail.php:

<?php
// Define the email addresses for the specific names
$to = array(
"jack" => "jack@domain.com",
"frank" => "frank@domain.com",
"mary" => "mary@domain.com"
);
// Populate the email "to" field
$receipient = $to[$_POST['ureceipient']];
if ($receipient != null) {
// Send Email
mail($receipient,'Subject','Message');
} else {
echo "Not a valid name. Please go back and try again";
}
?>

tech_support
12-11-2007, 05:43 AM
Or with links:



<a href="sendEmail.php?contact=jac">Jack</a>
<a href="sendEmail.php?contact=mary">Mary</a>
<a href="sendEmail.php?contact=frank">Frank</a>


sendEmail.php:


<?php
// Define the email addresses for the specific names
$to = array(
"jack" => "jack@domain.com",
"frank" => "frank@domain.com",
"mary" => "mary@domain.com"
);
// Populate the email "to" field and hide the notice error when using E_ALL as the error reporting
$receipient = @$to[$_GET['contact']];
if ($receipient != null) {
// Send Email
mail($receipient,'Subject','Message');
} else {
echo "Not a valid name. Please go back and try again";
}
?>

boogyman
12-11-2007, 01:40 PM
Its generally not advisable to have them insert a name, without giving them options because they could misspell it, they could try to send to a name not in the directory, or other errors could crop up...
The only way to get around something like this would be to use a switch loop and assign a default email address incase one of those or another error did occur.



$to = strtolower($_POST['ureceipient']);
$to = switch($to)
{
case jack:
'jack@domain.com'
break;
case frank:
'frank@domain.com'
break;
case mary:
default:
'mary@domain.com'
break;
}


Using links would be a fine, but you would need server-side support to capture the rest of the values inputted before the link is clicked, thus not really making it not the best idea in my mind

stardom
12-11-2007, 05:07 PM
Thanks for your patience and help so far guys, it seems we're getting there. But before I go ahead I have some doubts (I'm a beginner with php).

The two php files' tasks are:
1) choose the name and insert it in the "send to" field
2) pick up the assigned email address and send

I understand that using links might not be the best option, but actually the idea behind it is correct, as the user will need to click on the name/occupation to trigger the form into a "ready-to-launch" state. If a name is not clicked the form cannot be sent.

The "switch loop" method is the best option, but how would it read from file 1 if links are not advisable? In other words, if I cannot use <a href="sendEmail.php?contact=jack">Jack</a>, what html can I use in file 1 ?

Then, to summarize, I reckon file 2 should be like this:

<?php
// Define the email addresses for the specific names
$to = strtolower($_POST['ureceipient']);
$to = switch($to)
{
case jack:
'jack@domain.com'
break;
case general manager
'jack@domain.com'
break;
case frank:
'frank@domain.com'
break;
case technical director:
'frank@domain.com'
break;
case mary:
default:
'mary@domain.com'
break;
}

// Populate the email "to" field
$receipient = $to[$_POST['ureceipient']];
if ($receipient != null) {
// Send Email
mail($receipient,'Subject','Message');
} else {
echo "you did not choose a name";
}
?>

boogyman
12-11-2007, 07:15 PM
1) choose the name and insert it in the "send to" field
2) pick up the assigned email address and send

I understand that using links might not be the best option, but actually the idea behind it is correct, as the user will need to click on the name/occupation to trigger the form into a "ready-to-launch" state. If a name is not clicked the form cannot be sent.

this is something very achievable. using links throughout the site the user could click on the link to get to the contact form page, the contact would then be populated into the "To:" field automatically, which would solve that dilemma.




The "switch loop" method is the best option, but how would it read from file 1 if links are not advisable? In other words, if I cannot use <a href="sendEmail.php?contact=jack">Jack</a>, what html can I use in file 1 ?


I am not really following you at all here? can you restate?




Then, to summarize, I reckon file 2 should be like this:

<?php
// Define the email addresses for the specific names
$to = strtolower($_POST['ureceipient']);
$to = switch($to)
{
case jack:
'jack@domain.com'
break;
case general manager
'jack@domain.com'
break;
case frank:
'frank@domain.com'
break;
case technical director:
'frank@domain.com'
break;
case mary:
default:
'mary@domain.com'
break;
}




One advantage of the switch loop is that you can assign multiple values to 1 sequence. so where you have.


case frank:
'frank@domain.com'
break;
case technical director:
'frank@domain.com'
break;

it could reduced to



case frank:
case technical director:
'frank@domain.com'
break;

The loops will attempt to find the appropriate "person" in this case being either frank or technical director and continue on until it finds some value / break...
That is a very crude and probably not very helpful definition, but take a look at http://us2.php.net/switch for a better explanation...

The default at the very bottom is for values that are not found under any of the other conditions, ... basically its a "catch-all".

The problem I see with this is that if someone wanted the Technical Director Frank, they could it in wrong and what is submitted is "Tech Director" or "tehc writer" or something along those lines. This is why its really not advised to have them enter in a "name" persay, but an email address, and if they dont have an email address, then providing them a list of person(s) in the form of a drop down or a check box or radio boxes.




if ($receipient != null) {
// Send Email
mail($receipient,'Subject','Message');
} else {
echo "you did not choose a name";
}
?>

this will never be false because of the default switch loop value.
also the email address is not the only thing that would cause the email to not be sent, so something that I think would be more appropriate would be



if( !mail($to, 'Subject', 'Message') )
{
echo "ERROR MESSAGE";
}
else
{
echo "THANK YOU EMAIL MESSAGE";
}

stardom
12-11-2007, 08:34 PM
I'm gathering your precious suggestions... I think that I had to make myself clearer in the first place, as we are going the wrong direction. Let me try to explain better.

- As you can see from www.arsenalemusica.eu/staff.php (http://www.arsenalemusica.eu/staff.php) the names are already in the form, therefore I believe there's no need for a "default catch-all" email.

- Also, the send to field must not be allowed for typing, as it is a recipient for the name that is being clicked, i.e. a visual reminder that the form will be sent to that person. Therefore we don't need to prevent wrong spelling either, as there are no chances of getting it wrong.

- Neither we need an echo "mail not sent/error message", because the submit button will not activate until a name is clicked. But after submitting the form, it could be nice to have a "email sent successfully" appearing in the send to field.

So, I have made the following codes. Please tell me if they are correct:

form.php

<div>technical director<br>Frank Martin</div><br>
<div>artistic director<br>John Doe</div><br>
<td>
<div>
<br>send to<br><input name="receiver" type="text" id="receiver" size="30">
<br>your enquiry<br><textarea name="message" cols="30" rows="8" id="message"></textarea>
<br>your email<br><input name="sender" type="text" id="sender" size="30">
</div>
</td>
<td>
<form name="staff" method="post" action="mailstaff.php">
<input name="Submit" type="submit" class="buttons" value="send query">
</form>
</td>

email.php

<?php
// Define the email addresses for the specific names
$to = strtolower($_POST['receiver']);
$to = switch($to)
{
case Frank Martin:
case technical director:
'frank@domain.com'
break;
case John Doe:
case artistic director:
'john@domain.com'
break;
}

// Populate the email "to" field
$receiver = $to[$_POST['receiver']];

// Send Email
mail($receiver,'sender','message');
{
echo "email sent successfully";
}
?>

What we are missing is making "send to" an active field !! Do you feel like helping me finish this? :)
Criss