Using MIME::Lite for multipart message - trouble with string manipulation

A

alamukutty

Hi,

I am pretty new to cgi-perl and I'm trying to process a form and
send the email using sendmail. I figured how to send attachments using
MIME::Lite and also figured how to process a form and send a plain
text mail like

for each param()
ParamName = Param vAlue

When I try to put the two together, I am stuck. I don't know how to
process strings with perl and any help will be appreciated.
For now, this is what I am trying and I get a server error.

----------------------

#! / usr/bin/perl -w
use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use lib '/usr/lib/perl5/site_perl/5.8.0/MIME';
use MIME::Lite;

print start_html("Thank You");

# Set the PATH environment variable to the same path
# where sendmail is located:

$ENV{PATH} = "/usr/sbin";

# Now print the body of your mail message.

my $body_string;

foreach my $p (param()) {

$body_string = print "$p = ", param($p), "\n";

}

my $msg = MIME::Lite->new(
From =>'(e-mail address removed)',
To =>[email protected]',
Subject =>'Form Data!',
Type =>'multipart/mixed',
);
$msg->attach(
Type => 'TEXT',

Data => $body_string );

$msg->attach(
Type =>'application/msword',
Path => '/usr/local/apache2/htdocs/checking.doc'

);

$msg->send();

# Be sure to close the MAIL input stream so that the
# message actually gets mailed.

# Now print a thank-you page

print <<EndHTML;

<h2>Thank You</h2>

<p>Your information has been submitted.</p>

<p>Return to our <a href="index.htm">home page</a>.</p>
EndHTML

print end_html;
 
G

Gunnar Hjalmarsson

... I get a server error.

<snip>

You need to print a content-type header and tell the server that you
start printing the actual page before printing other stuff to STDOUT.

print header();
 
P

Paul Lalli

foreach my $p (param()) {
$body_string = print "$p = ", param($p), "\n";
}

What exactly do you believe this is doing? I sincerely doubt that it's
doing it.

Paul Lalli
 
A

AlamuKutty

I figured that wasnt doing what I wanted to do. I changed that part of
the code to

my $body_string = "" ;

foreach my $p (param()) {
$body_string .= sprintf("%s = %s\n", $p, param($p));
}


Now, I have another question.

Is there anyway to format this text to be HTML ? So that I get a nice
format in my e-mail

param name(inbold) = Param value
<double spacing here>

..... repeated?


Thanks !
 
G

Gunnar Hjalmarsson

AlamuKutty said:
foreach my $p (param()) {
$body_string .= sprintf("%s = %s\n", $p, param($p));
}

Now, I have another question.

Is there anyway to format this text to be HTML ? So that I get a nice
format in my e-mail

HTML is not a nice format in any email message. ;-)
param name(inbold) = Param value
<double spacing here>

.... repeated?

You just print the appropriate HTML stuff in addition to what you
already have... What's the reason why you ask?
 
A

AlamuKutty

Hi GUnnar, I'll try that. NOw... ANOTHEr question..

Having trouble with this part ..

$file_attached = param('fileattach');
$msg->attach(
Type =>'application/msword',
Path => $file_attached

);

It says file not found. I am guessing it looks in the webserver and not
in the local machine? And the PATH to the file selected is not
displayed. For instance,if I select a file "Testing.doc" from
C:/Desktop/blahblah/TEsting.doc It doesnt work ... what should i be
doing? THANKS!
 
G

Gunnar Hjalmarsson

AlamuKutty said:
Having trouble with this part ..

$file_attached = param('fileattach');
$msg->attach(
Type =>'application/msword',
Path => $file_attached

);

It says file not found. I am guessing it looks in the webserver and not
in the local machine?

That sounds plausible.

I would suggest that you split the problem in two parts.

1) You need to learn how to upload a file to the server machine.

2) You need to know how to send an email message with an attachment.

I'd suggest that you write two separate scripts to handle those things.
Only after you have done so successfully, it's time to combine them.

As regards uploading a file, please study the CGI.pm docs about it. Note
also that the form needs

enctype="multipart/form-data"

in the <form> element.
 
T

Tintin

Hi,

I am pretty new to cgi-perl and I'm trying to process a form and
send the email using sendmail. I figured how to send attachments using
MIME::Lite and also figured how to process a form and send a plain
text mail like

for each param()
ParamName = Param vAlue

When I try to put the two together, I am stuck. I don't know how to
process strings with perl and any help will be appreciated.
For now, this is what I am trying and I get a server error.

That's a might strange path to perl on your system.
use lib '/usr/lib/perl5/site_perl/5.8.0/MIME';

Why? Do you have different versions of perl installed?
 
A

AlamuKutty

I got it to work with MIME::Lite and CGI.pm . Thanks to everyone for
their patience and help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top