[ back to toc ]

mailform

Date: 2002/02/01 08:46

Q:
here is an easy question:

in a FormMail Script i have the folowing inputs:
name, email subject and contents

e.g.:

name martin
email mvermaak@hotmail.com

this is what it looks like in my cgi file
note, it's not the complete cgi file, only the imortant parts.

$name = $formdata{'name'}
$email = $formdata{'email'}
$subject = $formdata{'subject'};
$contents = $formdata{'contents'};

$from = $name + "<" + $email + ">"

print MAIL "To: $to \nFrom: $from\n";
print MAIL "Subject: $subject\n";
print MAIL "$contents\n";

The value of $from should be:

martin<mvermaak@hotmail.com>

but it doesn't work.
I know the problem is in the part of

$from = $name + "<" + $email + ">"

but i don't know what it should look like.

please help!

A:
I suppose that the issue is not the format of the From field, but rather
that the SMTP protocol requires the client to log in saying EHLO or HELO
and control information before the body of the mail.

Here I send you a script that sends SMTP mail. You can learn from it, or
just use it.

Regards,
Peter

#!/usr/bin/perl
# LEGAL WARNING:
#
# This software is provided on an "AS IS", basis,
# without warranty of any kind, including without
# limitation the warranties of merchantability, fitness for
# a particular purpose and non-infringement. The entire
# risk as to the quality and performance of the Software is
# borne by you. Should the Software prove defective, you
# and not the author assume the entire cost of any service
# and repair.
#
$smtp_addr = '';# this variable is set by admin using a PVAR
#
# An SMTP mail client sending mail through
# the server $smtp_addr
#
#
sub sockets_mail
{

my ($to, $from, $subject, $message) = @_;
my ($replyaddr) = $from;

if (!$to) { return -8; }

my ($proto, $port, $smptaddr);

my ($AF_INET) = 2;
my ($SOCK_STREAM) = 1;

$proto = (getprotobyname('tcp'))[2];
$port = 25;

$smtpaddr = ($smtp_addr =~
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
? pack('C4',$1,$2,$3,$4)
: (gethostbyname($smtp_addr))[4];

if (!defined($smtpaddr)) { return -1; }

if (!socket(SmtpMailSocketHandle, $AF_INET, $SOCK_STREAM, $proto))
{ return -2; }
if (!connect(SmtpMailSocketHandle, pack('Sna4x8', $AF_INET, $port,
$smtpaddr))) { return -3; }

my($oldfh) = select(SmtpMailSocketHandle); $| = 1; select($oldfh);
binmode SmtpMailSocketHandle;
$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -4; }

print SmtpMailSocketHandle "helo localhost\r\n";
$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -10; }

print SmtpMailSocketHandle "mail from: $from\r\n";
$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -5; }

print SmtpMailSocketHandle "rcpt to: $to\r\n";
$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -6; }

print SmtpMailSocketHandle "data\r\n";
$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -8; }

print SmtpMailSocketHandle "To: $to\r\n";
print SmtpMailSocketHandle "From: $from\r\n";
print SmtpMailSocketHandle "Reply-to: $replyaddr\r\n" if $replyaddr;
print SmtpMailSocketHandle "X-Mailer: pWErliB Sendmail \r\n";
print SmtpMailSocketHandle "Subject: $subject\r\n\r\n";
print SmtpMailSocketHandle "$message";
print SmtpMailSocketHandle "\r\n.\r\n";

$_ = <SmtpMailSocketHandle>; if (/^[45]/) { close
SmtpMailSocketHandle; return -7; }

print SmtpMailSocketHandle "quit\r\n";
$_ = <SmtpMailSocketHandle>;

close SmtpMailSocketHandle;
return 1;
}

1;

[ back to toc ]