For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > cgi mail script









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cgi mail script
brian goulet

2004-05-22, 11:32 am

on my web page i would like to set up a typical "send this page to a friend"
feature. i currently have a script that will generate an html based email
using MIME::Lite and send it using the defined email adress. what i need
now is to adapt this script to get it's addresses in from a form but i
haven't been succesfull. i'm not sure what i'm doing wrong. please forgive
my code. i'm not a perl programmer and this script is mostly made up of
puzzling togeather other people's work.

########################################
#########

#!F:/Perl/Bin

use MIME::Lite;
use Net::SMTP;
use CGI;

my $query = new CGI;

# set the default from address
my $from_address = 'sender@somewhere.com';

# set the default to address
my $to_address = 'friend@somewhereelse.com';

my $subject = 'Funny Movies';
my $mime_type = 'text/html';

# Attach the HTML content
my $message_body = '<body>Hi, I saw some hilarious movies on <b>X.org</b>
and I thought you might like them. Go check it out!<br><a
href="http://www.X.org">www.X.org</a><br><br>
</body>';

#look to validate existance of input and reasign addresses to that input
if ($query->param('from')){
my $from_address = $query->param('from');
}
if ($query->param('to')){
my $to_address = $query->param('to');
}


# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
From => $from_address,
To => $to_address,
Subject => $subject,
Type => $mime_type,
Data => $message_body
)
or die "Error creating MIME body: $!\n";


# Attach the image
$mime_msg->attach(
Type => 'image/gif',
Id => 'rainbow_image',
Encoding => 'base64',
Path => 'logo.jpeg');


# We'll still need to set this variable to the smtp server name
my $ServerName = "smtp-server.x.com";

# Let MIME::Lite handle the Net::SMTP details
MIME::Lite->send('smtp', $ServerName);
$mime_msg->send() or die "Error sending message: $!\n";

########################################
####################

i have tried calling this script from the command line to test it. if i call
the script with no parameters it functions fine and goes to the predefined
addresses. if i call it by >mail.cgi from=me@home.com it fires off but the
reciever gets a mail from UNKNOWN_SENDER. basically, the sender info is
blank. that leaves me to believe that the script recognizes the input
variable 'from' does exist and falls TRUE into the 'if' but for some reason
it doesn't set the string as the value of $from_address. what am i doing
wrong here?

brian


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com