Midnight Voices (https://www.peteatkin.com/cgi-bin/mv/YaBB.cgi)
Atkin admin >> Tech >> How do I email the content of an HTML form?
(Message started by: Secret Drinker on Today at 13:51)

Title: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 13:51
[OT Techie Question]

Apologies if this isn't on-topic, but I understood that it would be OK to post such questions here - please correct me if I'm wrong.

The problem I have is this: I created an HTML form on a website, the idea being that a visitor to the site would be able to fill in the form with certain details (name, address, etc.). The form had an action to submit an email when the user clicked on the button at the end of the form, using "mailto:" action. An email would then be sent to me without the user needing to do anything else after submitting the form - fairly standard I'd have thought.

This all worked fine on my PC when I tested it - it sent me an email with the fields of the form included in the message body. However (and this is where the story really starts) when I tried it on a different PC, I just got a normal email window popping up, with none of the form information in it (just as if I'd included a normal href="mailto:..." link in the web page). On looking into it further, it seems that M$ in its wisdom has stopped supporting the forms mailto: feature in later versions of IE, supposedly for "security reasons". So my form is now useless and I've had to revert to asking the user to send a simple email with the details - a big step backwards, as far as I'm concerned :(

My question is - does anyone know of another easy way to achieve what I want, which is to email a form to me directly from the web page without any further intervention from the user after he/she clicks on the submit button? I'm sure there must a be a suitable script somewhere if only I could find it...

Any help with this would be appreciated.

Cheers

Paul

Title: Re: How about a sub-Forum for techie questions?
Post by Snoopy on Today at 14:01
Hi Paul,

Firstly, it's quite OK for you to post any techie-related questions in the forum - I may not be able to answer them, but I'll try!  However, could you make a new thread for the question, rather than adding to an existing thread - it makes it easier to keep track, and also helps others browse.  I've split this off into a seperate thread.

The problem that you are having is that using the 'mailto:' action relies on the user's PC and e-mail client to do the mail delivery - this means that if they have a different e-mail client (or worse, use hotmail and have no e-mail client at all), the results may not be as you desire.

The best way to get around this is to have the server do the e-mailing - this means that the form sends the e-mail data (address, subject line, message body) to a script running on the server, which then sends the e-mail to the desired location.

However, this requires some form of scripting support to be on your server.  These days, almost all hosting companies will support either PHP, Perl, or both.  There are thousands of simple e-mail scripts written in either PHP or Perl that will do exactly what you need, and return the same results whatever the computer setup of the user is.  

If you are able to let me know what scripting support your hosting company provides (they should be able to tell you), then I'll look at recommending a simple script for you to try.

Cheers,

Alexis.

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 14:09
Thanks for the reply, Alexis.

I was hoping to avoid php, cgi, asp, etc. as (a) I don't currently know how to write the scripts and (b) I don't know which are supported by my ISP. I'll try and find out though (they don't seem to say on their website, as I've already looked there - I'll have to ask them the question).

I'd overlooked the fact that web mail users wouldn't be able to use the mechanism anyway, which is a very valid point - thanks for pointing that out.

I'll let you know when I find out which scripts my ISP supports.

Thanks again for the prompt reply - noted about the new topic for new questions, I just thought this particular one was too OT for that.

Cheers

Paul

Title: Re: How do I email the content of an HTML form?
Post by Snoopy on Today at 14:22
Hi Paul,

A mail script is very simple to do in either PHP or Perl, and is an excellent way to 'dip a toe' into the wonderful world of web scripting!  PHP is the simpler language to understand, and is supported by pretty much any hosting company these days.

I've sketched out a simple PHP script that should work below - you can try this on your website to find out if it supports PHP, for one thing!  You'll need an HTML page with the following form code in it:


Code:
<form action="mail.php" method="post">
Name: <input type="text" name="name" size="20" maxlength="20"><br>
Your Email: <input type="text" name="email" size="30" maxlength="30"><br>
Subject: <input type="text" name="subject" size="30" maxlength="30"><br>
Text:<textarea name="text" name="text" cols="50" rows="10"></textarea><br>
<input type="submit" name="submit" value="Send">
</form>


You then need to create a file called 'mail.php', containing the following:


Code:
<?php

/* Extract the data that was sent in the */
/* form - each form element becomes a */
/* variable in PHP identified by the 'name' */
/* attribute of the form. */
@extract($_POST);

/* Tidy up the data submitted - get rid of */
/* any slashes that could confuse the mailer */
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);

/* The mail function - change */
/* 'toaddress@domain.com' to the address */
/* you want the mail to go to */
mail('toaddress@domain.com',$subject,$text,"From: $name <$email>");

/* Finally, redirect the user to a page letting */
/* them know the mail was successfully sent */
header("location:success.html");
?>


This is a very simple form that only allows the visitors to e-mail you, and you wouldn't even be giving out your e-mail address, so the automated spambots won't collect it.

Edit: Slight mistake in the code - the address in the 'mail' function is the address the mail is sent to - i.e. your address!

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 14:39
Hi Alexis

Thanks for the detailed reply. I'll certainly give that a go (I'll have to wait until tonight when I can upload the script to the server).

Good tip about the email address. I've already removed any explicit email addresses from my websites to deter spambots, etc., and replaced them with a Javascript script that is invoked from a separate .js file. I can still have the script display the actual address on screen if I want, without it being embedded in the code for spammers to find.

Anyway, I'll give your script a go and let you know how I get on.

Thanks again

Paul

Title: Re: How about a sub-Forum for techie questions?
Post by Gerry Smith on Today at 14:09
Hi Paul - your ISP/Web Host will in all likelihood have a ready to roll form to email script pre-installed in your cgi-bin (or similar) along with instructions for use.   Failing that, there are many such scripts available - try Matt's Script Archive.  

Gerry




Title: Re: How do I email the content of an HTML form?
Post by Roger Cornwell on Today at 15:00
Actually, the programs in Matt's Script Archive were developed some time ago and there are security issues with some of them. Don't get me wrong  -- I've used them myself and indeed I probably still am. But I suggest you look at the replacements in NMS (which they say definitely does not stand for Not Matt's Scripts  ;) as these are more recent and more secure. See http://nms-cgi.sourceforge.net/ for the home page of the project and http://nms-cgi.sourceforge.net/scripts.shtml for the page that you can download FormMail from.

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 19:00
Thanks for all the help so far, guys.

I tried Alexis's script but I got the following error message:

Method Not Allowed
The requested method POST is not allowed for the URL xxx

Apache/1.3.26 Server at xxx


Any ideas? Does it mean my ISP doesn't support PHP?

Cheers

Paul

Still ignorant in Herts

Title: Re: How do I email the content of an HTML form?
Post by Snoopy on Today at 19:10
Try renaming the file 'mail.php' to 'mail.php3', and also altering the form to point to the new filename.

If that doesn't work, it seems that your server might not support PHP (the error message is basically saying that it doesn't know how to handle a .php file, either because they've configured it to only accept .php3 files, or because PHP isn't installed).

Best thing to do (assuming no luck with the rename) is to mail them and ask them if they do support any scripting languages.  :(

Title: Re: How do I email the content of an HTML form?
Post by Gerry Smith on Today at 19:12
Paul - see if your ISP has an off the peg script you can use.  If no or only limited server side scripting is available, consider changing your host - Supanames at less than £30 a year offer a very good service with many server side functions available.

Gerry

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 19:55

on 08/17/04 at 19:10:05, Snoopy wrote :
Try renaming the file 'mail.php' to 'mail.php3', and also altering the form to point to the new filename.

If that doesn't work, it seems that your server might not support PHP (the error message is basically saying that it doesn't know how to handle a .php file, either because they've configured it to only accept .php3 files, or because PHP isn't installed).

Best thing to do (assuming no luck with the rename) is to mail them and ask them if they do support any scripting languages.  :(


I've tried that now, and get a similar message  :(

I've emailed them to find out what they do support but I'll have to wait for the proper answer (I've got the usual automated response saying someone will be contacting me with a proper answer).

Thanks again for the help - I'll let you know what they say...

Paul

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on Today at 19:57

on 08/17/04 at 19:12:35, Gerry Smith wrote :
Paul - see if your ISP has an off the peg script you can use.  If no or only limited server side scripting is available, consider changing your host - Supanames at less than £30 a year offer a very good service with many server side functions available.

Gerry


Thanks Gerry - I've also asked them that in the same email to their support folks.

I'll bear that in mind. I have more than one ISP so I might try using another one...

Cheers

Paul

Title: Re: How do I email the content of an HTML form?
Post by Snoopy on Today at 21:12
If we're recommending external hosting companies, I'd have to throw 34sp (http://www.34sp.com) into the mix - I was looking for a host for a website I'd been asked to design recently, and they were recommended by a few people.

15 quid a year (!), and they support just about everything under the sun - PHP, Perl, C, TCL, MySQL, Cron jobs, etc. etc.  Lightning fast, and based in the UK so ideal for hosting sites having a primarily UK-based audience.

But what really impressed me was when I got an instant reply (less than 2 minutes) to my technical support question.  Stunning enough in this day and age, you say, but even more amazing was that it was 7.30pm on a Sunday.

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on 18.08.04 at 12:01

on 08/17/04 at 21:12:42, Snoopy wrote :
But what really impressed me was when I got an instant reply (less than 2 minutes) to my technical support question.  Stunning enough in this day and age, you say, but even more amazing was that it was 7.30pm on a Sunday.


Sounds a lot better than my ISP. Last night I had to jump through various hoops to fill in a web form to contact them with my question about scripting languages and scripts they support/provide (they don't provded a simple email address), only to get a reply this morning saying my question was too complicated to answer by email and I'd have to call their help line between 8am and whenever they finish (at 50p/minute). No thanks! I've filled in another form, this time complaining that all I wanted was a straightforward answer to my original question and not an extra large phone bill  >:(

Anyroad, I've discovered meanwhile that my other ISP has a formmail script available (.pl - is that perl?), so I'm going to try that next. The only drawback I can see is that from my initial tests it seems I have to send it to an email address on the 2nd ISP, but it allows me to specify a url for the "successful submission" anywhere, and it looks as if I can do it that way, for the time being, as a workaround (fingers crossed...)  :-/

Thanks for all the help and the tips from everyone.  :)

Cheers

Paul

Now an MV Regular thanks to the All-Bran ;D

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on 22.08.04 at 12:49

on 08/18/04 at 12:01:55, Secret Drinker wrote :
Anyroad, I've discovered meanwhile that my other ISP has a formmail script available (.pl - is that perl?), so I'm going to try that next. The only drawback I can see is that from my initial tests it seems I have to send it to an email address on the 2nd ISP, but it allows me to specify a url for the "successful submission" anywhere, and it looks as if I can do it that way, for the time being, as a workaround (fingers crossed...)  :-/


I thought I had this working, but I've now got another problem. Although it worked fine for me, when I got someone else to test it, they got an error message to the effect that a form sent from outside the 2nd ISP needs to have the url in the 1st ISP's webspace(where the form origniates from) added to the @referers array on the 2nd ISP, or it won't work.

Is there a way I can do this? I don't fully understand what's involved, but if I need to change the actual formmail script I assume I can't do it.

(I know it would be simpler to change ISPs, but I'd like to know whether I can get round this problem!)

Can anyone help?

Thanks in advance,

Paul

Title: Re: How do I email the content of an HTML form?
Post by Roger Cornwell on 22.08.04 at 14:20
Paul -
There is (I think) a way round it, because otherwise you would need to change the $referers array (and yes I have spelt that the way it needs to be, ie only one r in the middle).

Can you put the entire web page containing the form on your second ISP's webspace? This should be possible, and you will have to change any links leading to it, and any code on the page refering to other files, ie the <A> <IMG> and <LINK> tags. You will have to replace relative addresses eg <IMG SRC="images/picture.jpg"> with full addresses eg <IMG SRC="http://www.secondisp.co.uk/~username/images/picture.jpg">

PM me with actual examples and we can continue this off-list unless there's a rush of MVs interested in the details...

Title: Re: How do I email the content of an HTML form?
Post by Secret Drinker on 22.08.04 at 14:50

on 08/22/04 at 14:20:22, Roger Cornwell wrote :
Paul -
There is (I think) a way round it, because otherwise you would need to change the $referers array (and yes I have spelt that the way it needs to be, ie only one r in the middle).

Can you put the entire web page containing the form on your second ISP's webspace? This should be possible, and you will have to change any links leading to it, and any code on the page refering to other files, ie the <A> <IMG> and <LINK> tags. You will have to replace relative addresses eg <IMG SRC="images/picture.jpg"> with full addresses eg <IMG SRC="http://www.secondisp.co.uk/~username/images/picture.jpg">


Thanks Roger - I've already thought of that, and I've prepared the new version to run on the 2nd ISP (although I haven't actually tested it yet). I was just wondering if there was any other way? I guess not, because my (very limited) understanding is that otherwise I would have to change the 2nd ISP's formmail script, which I can't!

One thing that puzzled me slightly is that it worked OK when I tested it myself. I was still invoking the 2nd ISP's formmail.pl from a different ISP, so why didn't it object then? It must somehow have known it was me (maybe by the IP address?). If anyone can explain that I'd be interested.

Thanks again for your help.

Cheers

Paul

Title: Re: How do I email the content of an HTML form?
Post by Roger Cornwell on 22.08.04 at 19:17

Quote:
One thing that puzzled me slightly is that it worked OK when I tested it myself.
Were you running it off your hard disk? I think from memory that leaves the HTTP_REFERER field null and that might have explained it. Some browsers also don't provide the HTTP_REFERER field back to the server.

There's another potential problem and there's not much you can do about it: some ISPs (eg BlueYonder) have caching and the referer there is set to the cache (which is a Blue Yonder domain name) rather than the original web page. This tripped me up when I was implementing code to prevent hot-linking (folk were downloading some nice images we had to use as wallpaper on their site). After I implemented the code to allow images only to be loaded by web pages on the same site, my brother (who has Blue Yonder broadband) stopped seeing them.

This is probably going too off-topic, even for an off-topic thread!  :D



Midnight Voices » Powered by YaBB 1 Gold - SP 1.3.1!
YaBB © 2000-2003. All Rights Reserved.