Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Email and MIME::Lite

Status
Not open for further replies.

miked79

Technical User
Nov 28, 2008
3
AU
Hello,
I am new to this forum so please go easy. I am just trying to figure out for the first time how to send an email with an attachement using perl and a form so that users of my site can send a resume.

I keep getting 500 Errors without any detail. the hosting I use says to follow these instructions below to make sure i can use the modules.
I have made sure MIME::lite is there:

Location of Your Perl Module(s)

Path: /home/lot49495/perl

Using Your Perl Module(s)

You will need to add /home/lot49495/perl to the include path.
You can do this by adding the following code to your script:

BEGIN {
my $homedir = ( getpwuid($>) )[7];
my @user_include;
foreach my $path (@INC) {
if ( -d $homedir . '/perl' . $path ) {
push @user_include, $homedir . '/perl' . $path;
}
}
unshift @INC, @user_include;
}


Any help would be greatly appreciated.




Here is the whole thing:
_____________________________________________
#!/usr/bin/perl -w

BEGIN {
my $homedir = ( getpwuid($>) )[7];
my @user_include;
foreach my $path (@INC) {
if ( -d $homedir . '/perl' . $path ) {
push @user_include, $homedir . '/perl' . $path;
}
}
unshift @INC, @user_include;
}

# use MIME::Lite package
use MIME::Lite;



#Parse the Form Data
&GetData(*in);

# set up email
$smtp = "mail.mydomain.com.au";
$to = "michael\@mydomain.com.au";
$from = "michael\@yourdomain.com";
$subject = "Email Sent via Perl";
$message = "This email was sent using Perl.";
$file = "sample.txt";

# send email
#&email($to, $from, $subject, $message, $file);

# email function
sub email
{
#get incoming parameters
local ($to, $from, $subject, $message, $file) = @_;

# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);

# add the attachment
$msg->attach(
Type => "text/plain",
Path => $file,
Filename => $file,
Disposition => "attachment"
);

# send the email
MIME::Lite->send('smtp', $smtp, Timeout => 60);
$msg->send();

}
print "Content-type: text/html\n\n";
print "<div align=\"center\"><b>Thankyou for your request <br>We will get back to you ASAP.</b></div>";

_________________________________________________




Thanks again.
 
Code:
#!/usr/bin/perl -w


# use MIME::Lite package
use MIME::Lite;\
use CGI::Carp qw/fatalsToBrowser/;#<--ADD THIS LINE


#Parse the Form Data
&GetData(*in); # <---THIS IS A PROBLEM WHERE IS funtion GetData() coming from? You should use the CGI module for form data processing

# set up email
$smtp = "mail.mydomain.com.au";
$to = "michael\@mydomain.com.au";
$from = "michael\@yourdomain.com";
$subject = "Email Sent via Perl";
$message = "This email was sent using Perl.";
$file = "sample.txt";

# send email
#&email($to, $from, $subject, $message, $file);

# email function
sub email
{
 #get incoming parameters
 local ($to, $from, $subject, $message, $file) = @_;

# create a new message
 $msg = MIME::Lite->new(
  From => $from,
  To => $to,
  Subject => $subject,
  Data => $message
 );

# add the attachment
 $msg->attach(
  Type => "text/plain",
  Path => $file,
  Filename => $file,
  Disposition => "attachment"
 );

 # send the email
 MIME::Lite->send('smtp', $smtp, Timeout => 60);
 $msg->send();

}
print "Content-type: text/html\n\n";
print "<div align=\"center\"><b>Thankyou for your request <br>We will get back to you ASAP.</b></div>";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for your reply Kevin,
Sorry I didnt paste that bit in. I have used this GetData sub for ages without a hitch. it is unchanged from many previous uses

###########################################################
#PARSING INFO BELOW THIS POINT


#/***********************************************************************************************/

# ReadParse
# For Reading Input from forms.
sub GetData {
local (*in) = @_ if @_;
local ($i, $key, $val);

# Read in text
if (&MethGet) {
$in = $ENV{'QUERY_STRING'};
} elsif (&MethPost) {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/[&;]/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}
}

#/***********************************************************************************************/

# MethGet
# Return true if this cgi call was using the GET request, false otherwise

sub MethGet {
return ($ENV{'REQUEST_METHOD'} eq "GET");
}


#/***********************************************************************************************/

# MethPost
# Return true if this cgi call was using the POST request, false otherwise

sub MethPost {
return ($ENV{'REQUEST_METHOD'} eq "POST");
}



Thanks again
 
Looks like it has decided to work.

I commented out the line:
MIME::Lite->send('smtp', $smtp, Timeout => 60);

and it all seemed to work fine? I thought this was a bit strange cause at no point do i say where the mail server is?

Does anyone know why this made it work?

now all i have to do is figure out how to do the form to to attach someones resume.

thanks again.
 
Most unix servers have a MAILHOST entry and if that fails I think it will look to localhost to try and send it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
MIME::Lite uses sendmail by default, its discussed in the modules documentation.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top