Forum Moderators: coopster

Message Too Old, No Replies

Using a link on one page to send email on a common, separate form

send email to an advertiser via a form without showing email address

         

kevinashby

7:34 am on Oct 3, 2010 (gmt 0)

10+ Year Member



Hi everybody,

Lets see if I can explain this so that I don't leave anything out.

I am trying to figure out how to use a link on one page to send an email to the person's (advertiser's) email address.

What I am trying to achieve is a way to include an "email this person" link that will use a common form to send an enquiry without exposing the email address to the whole world (of spammers).

I guess one way of doing this would be to retrieve the email address, using a username, from either a spreadsheet or some other form of database - preferably a simple spreadsheet, but I am not sure how to do it.

I hope that the information provided is sufficient and that this is posted to the correct board.

I would prefer to achieve this using php, cgi, or plain old html - or a mix thereof.

Thanks in advance for any help that can be offered.

penders

11:37 am on Oct 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It might also depend on how 'secure' you need it to be? ie. Can the user guess how to email someone else, perhaps by altering the username?

You could form your links like:
<a href="emailthisperson.php?id=1234">Email this person</a>


Your emailthisperson.php page builds the contact form and includes id=1234 as a hidden field. This identifies (when the form is submitted) who to email the form contents to. ie. 1234 refers to someone@example.com (looked up from DB, or even a static array if there isn't that many).

<form action="emailthisperson_submission.php" method="post"> 
<input type="hidden" name="id" value="1234">


You then have a standard form to email script, except the recipient is governed by the value returned by the ID field. Bear in mind that the end user can change this value in this instance. If you used usernames ('fred', 'bob', etc.), the end user could perhaps guess a correct value - which might be OK for you?

kevinashby

1:35 pm on Oct 4, 2010 (gmt 0)

10+ Year Member



Thanks penders.

This is what I had in mind, but am not sure exactly how to go about doing it.

I was thinking that if I could "call" the ID from say an Excel spreadsheet that would suffice. It does not need to be secure.

Do you have any input as to how I should go about doing this either using an Excel sheet or if need be a MySql database?

Thanks in advance for any help that could be offered.

penders

2:17 pm on Oct 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How does the Excel spreadsheet fit into this? Do you have an Excel spreadsheet that contains links to 'email this person' (ie. not a webpage at all, just a document on your PC), OR are you wanting to lookup email addresses in an Excel spreadsheet from a server-side script on your webserver? I would not recommend the later - complex and might not even be possible with the resources available.

How many email addresses are there likely to be? Are these likely to be updated often? Unless you have 100's (or may be even 1000's) or you wish to update them via an admin panel of some kind then you don't need a MySQL database, just a simple lookup array should suffice, such as:

In "emailthisperson_submission.php":

$recipients = array ( 
'WEBMASTER' => 'webmaster@example.com', // When something goes wrong
'1234' => 'someone@example.com',
'8362' => 'bob@example.co.uk',
'9072' => 'fred@anotherexample.com',
);


When the form is submitted then you can access the recipients email address something like:
$recipientId = isset($_POST['id']) ? $_POST['id'] : 'WEBMASTER'; 
$recipientId = isset($recipients[$recipientId]) ? $recipientId : 'WEBMASTER';
$recipientEmail = $recipients[$recipientId];


...process rest of form, like any HTML form, and mail() $recipientEmail.

kevinashby

2:29 pm on Oct 4, 2010 (gmt 0)

10+ Year Member



Actually, what you suggest looks fine.

We will have hundreds or even thousands, but I guess we could still use the array method as you suggested.

I am a little unsure of how to do this, do you have a little sample of how this would look?

Please accept my apologies for being so dim. It's late here and I am not functioning properly.

enigma1

12:49 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could save the file from the spreadsheet application as CSV then check the ready php function like fgetcsv (save as -> save it with the coma separated CSV option).

[php.net...]

Has an example right after the function description to test it.

penders

2:14 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am a little unsure of how to do this...


Which bit?
- Creation of links to emailthisperson.php?
- Construction of the initial HTML form?
- Processing the form submission? Validation...
- Retrieving the recipient email (although one method is explained above)?
- Constructing the email to send to the recipient from the form submission?
- Sending the email?
- Displaying thank you page?
- Returning to the page the user initially clicked the "Email this Person" link?
- All of the above?!

In many ways the code is the same as a standard form-to-email script, like most contact forms. With the addition of looking up the recipients email address, rather than it being static.

kevinashby

11:52 pm on Oct 6, 2010 (gmt 0)

10+ Year Member



Hi enigma,

Thanks for that link, I will have a look.

kevinashby

11:56 pm on Oct 6, 2010 (gmt 0)

10+ Year Member



Yes, I guess some more details should have been supplied - sorry about that.

So, I have part of it working. The form will send to the "WEBMASTER" variable, but not to the "ID" that was requested in the string (eg: www.my-domain.com/form.php?id=001).

I have included a copy of a very basic form and the script:

The form:

<form method="post" action="url-to-form-script.php">
Name: <input name="name" type="text" /><br />
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input type="submit" />
</form>


The script:

<?php

$recipients = array (
'WEBMASTER' => "name@my-domain.com",
'001' => "name@some-domain.com",
'002' => "name@another-domain.com",
'003' => "name@yet-another-domain.com",
);

$email = $_POST['email'] ;
$message = $_POST['name'] ;
$message .= $_POST['message'] ;

$recipientId = isset($_POST['id']) ? $_POST['id'] : 'WEBMASTER';
$recipientId = isset($recipients[$recipientId]) ? $recipientId : 'WEBMASTER';
$recipientEmail = $recipients[$recipientId];

mail( "$recipientEmail", "Feedback Form Results",
$message, "From: $email" );
header( "Location: www.redirect-to-this.com" );
?>


Please could you point me in the right direction.

Many thanks.

penders

11:12 am on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to pass the ID value back to the server-side script, so we can lookup the corresponding email address. One way to do this is to incorporate it as a hidden field in your form, it is then passed back to your server-side script with the rest of the form when it is submitted.

So, in the form page (form.php) add the following:
<?php 
// Retreive the value of the 'id' param in the URL (if it exists)
// Convert to HTML entities to prevent code injection
$ID_VALUE = isset($_GET['id']) ? htmlentities($_GET['id']) : '';
?>

<form method="post" action="url-to-form-script.php">
<input type="hidden" name="id" value="<?php echo $ID_VALUE; ?>" />
:


That should be all that's reqd in order to get your script sending emails to the other recipients when a link such as www.my-domain.com/form.php?id=001 is followed, since you already have the code in your script to check for this 'id' value.

Note that if "id=" is omitted from the URL then an empty string will appear in the value attribute in the HTML form. This will fail to lookup in the $recipients array when the form is posted and therefore the recipient will default to the WEBMASTER's email again. You might want to put checks in to prevent this?

kevinashby

12:01 pm on Oct 7, 2010 (gmt 0)

10+ Year Member



Thanks for the help penders - that works well.

Would it be possible to use this method in conjunction with a CSV file using "fgetcsv"?

penders

9:47 pm on Oct 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, this is easily adaptable to read in a CSV file. The CSV file can be used to populate the $recipients array so the rest of the code can remain unchanged.

Assuming you have a CSV file saved from Excel in a "data" sub directory called "emailaddresses.csv" of the form:
WEBMASTER,name@my-domain.com 
001,name@some-domain.com
002,name@another-domain.com
003,name@yet-another-domain.com


Then (using code based on the link in enigma1's post) replace the $recipients array definition:
$recipients = array (  
'WEBMASTER' => "name@my-domain.com",
'001' => "name@some-domain.com",
'002' => "name@another-domain.com",
'003' => "name@yet-another-domain.com",
);


With code to read in the CSV file:
$filename = 'data/emailaddresses.csv'; 
$recipients = array();
if (($handle = fopen($filename, 'r')) !== false) {
while (($data = fgetcsv($handle, 200)) !== false) {
$recipients[$data[0]] = $data[1];
}
fclose($handle);
}

kevinashby

5:05 am on Oct 11, 2010 (gmt 0)

10+ Year Member



Thanks, Penders.

I have been talking to Enigma about this and I will be looking into that method further today.