19 May, 2006
Emails in CGI
Note: these examples are all in Perl.
Emails in UNIX
The simplest way to send emails on a UNIX system is to use the sendmail utility:
open(MAIL, '| /usr/lib/sendmail user@there.com') or "die";
print MAIL <<EOF;
To: user@there.com
From: user@here.com
Hello, World!
EOF
This uses an external program to send emails. This is fine on UNIX systems which have this utility, but on another platform e.g. Windows you can use the following:
use Net::SMTP;
# connect to an SMTP server
$smtp = Net::SMTP->new('here.com');
# use the sender's address here
$smtp->mail( 'user@here.com' );
# recipient's address
$smtp->to('user@there.com');
# Start the mail
$smtp->data();
# Send the header.
$smtp->datasend("To: user@there.com\n");
$smtp->datasend("From: user@here.com\n");
$smtp->datasend("\n");
# Send the body.
$smtp->datasend("Hello, World!\n");
# Finish sending the mail
$smtp->dataend();
# Close the SMTP connection
$smtp->quit;
This makes use of the Perl SMTP module to send mail.