views:

399

answers:

6

What techniques are available for sending an email via a webpage or a form on a webpage?

I've got some background idea that you POST the form data to a script but I've don't really know what a cgi script is (I'd love to learn if this is the suggested method!) or what the current practice is.

This is just to provide some way for users to contact the operators. The in-page form seems like it would be easier on the user than ask them to open their mail client. I was also concerned about bots harvesting the contact email address (in the case of mailto: links).

A: 

If you are using the ASP.NET 2.0, you can use the System.Net.Mail namespace. More information here

Michael Kniskern
+1  A: 

For most unix/bsd/linux systems, most languages provide a programmatic wrapper around the Mail command.

FlySwat
A: 

todays languages for web development usually have libraries for sending e-mail. it depends which language you use, but you'd find it in your language's docs. it's pretty simple, the library inside your language usually encapsulates and provides 'smtp client' behavior which you use. you provide mail message with sender and recipient, and the data for connecting to your SMTP server.

or, you sometimes may use the SMTP capabilities on the machine where your web server is, if those are available. i'm not sure whether it gets worse for the e-mail at the recipient server because your server might not be recognized as mail server for the domain... someone with more experience might comment on that.

zappan
A: 

Well, here's what not to do:

<a href="mailto:[email protected]">Please spam me, kthx</a>

It is a better code pattern to have users submit a form, sanitize the input and format it however you see fit, and then pass the data to a mail function in your language of choice.

bigmattyh
Does assembling this type of link in javascript provide adequate protection?
Albert
If there's a good way of doing it, I haven't seen it. (Which isn't to say there isn't a good way out there.) Adding to my comment above.
bigmattyh
A: 

Sending mail should be done server-side - the specifics change according to your server-side language, your operating system, and what access your server has to an SMTP server.

If you're looking for a lightweight way to add a contact form to a blog or public website, try Wufoo - you can add a contact form that will send you email very easily (up to 3 forms for free). I am not affiliated with them, I just think they're cool.

orip
+2  A: 

When you submit a form, the data in that form gets sent to the server-side script. For example, in PHP you access that data with the $_POST array, the <input name=""> becomes the arrays index.. For example..

// <form action="mailer.php">[..]<input name="subject" [..]><input name="content" [..]></form>
echo("The subject is: ". $_POST['subject']);
echo("The content is:" . $_POST['content']);

At the most basic level, all you have to do is use your programming languages built in mail function. Again, in PHP this is simple mail():

mail($to, $subject, $message);

You would just set $to to your email address (Do not allow the user to set this, or they are able to send mail as "you", to anyone - "spam"..), $subject and $message would be set form $_POST[]

Before you go any have a HTML file that goes to a script with mail("[email protected]", $_POST['subject'], $_POST['content']);, think what would happen if someone reloaded that page 200 times.. You must have some kind of security in it, probably a captcha, and/or rate-limiting.

One thing, that has bugged me before - remember a "contact us form" is not a replacement for giving an actual email address! For example, my mail client keeps a copy of all mail I send, and I can attach files, and it's much nicer writing in a familiar mail client than a form <textarea> (especially when the I accidently hit "back" and the form decides to clear itself)!

dbr
Thanks for your answer! Also the tip about displaying an actual email address too...
Albert