• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Using mail module --Sender module

Hi all,

Pls provide a solution to this question:

I would like to browse a file from my machine and send this file to the mail id.( Body content will go to one email id and attachment(browsing file) will go to another mail id.) How can i do this?

Code for html:
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>

<FORM name="xyz" method ="post" action="/cgi/fv/stry.cgi">
<input type="file" name="file" value="browse">
<input type="submit" name="submit" value="submit">


and code for cgi:
use Mail::Sender;
use CGI;
print "Content-type: text/html\n\n";
$smtp = "world.com";
$query = new CGI;
$fi_le = $query->param('file');
$from = "xyz\@hotmail.com";
$to = "trojen\@yahoo.com";
$subject = "test mail";
$body =<<END_OF_MAIL;
Hi, This is a Test Mail.

$sender = new Mail::Sender {};
$sender->Open({ smtp => $smtp, from => $from, to =>$to, subject => $subject, file => $fi_le, headers => "MIME-Version: 1.0\r\nContent-type: text/plain\r\nContent-Transfer-Encoding: 7bit" }) or return 0;
print "Sent!";

Please modify my cgi code.


Aim is to separate the body part and attachment file and send it separately.

Waiting for the answer.
More points will be awarded.
  • 5
  • 4
1 Solution
can you verify you have MIME::Lite installed on your webserver to handle sending the attachment?
sureshpAuthor Commented:

MIME::Lite is not installed on our webserver. Without using  MIME::Lite , can i solve? If no, pls help me solve this problem.

Waiting for your answer.
well, yes it can be done, it is really outside my expertise to support you on this because I am not familiar with mime on that basic a level.

you have to 1. print the proper mime headers, and 2. encode the data in mime format.

I know maneshr knows how, and he has a script that works...

since he is back in the perl ta, perhaps he will help you if you ask.

in the mean time, I will see if I can locate that script...tomorow.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

sureshpAuthor Commented:
Thanks a lot for your reply.
this is courtesy of maneshr

<TITLE>Email your Resume</TITLE>

<form ENCTYPE="multipart/form-data" method=post action="/cgi-bin/attach_cv_details.pl">
<table border=1 cellspacing=3 cellpadding=4>

<TD><input type=text name=name></TD>

<TD><input type=text name=age size=4></TD>

<TD><input type=text name=address size=30></TD>

<TD><B>Your CV <I>(in MS-Word format)</I></B></TD>
<TD><input type=file name=cv></TD>

<TD align=center colspan=2><input type=submit value="Send it!!"></TD>



use CGI;

$query=new CGI;

##  Read HTML form variables into PERL variables

print "Content-type: text/html\n\n";

##  Location of the uploaded file

##  The sender of this form and the receiver of this email.
##  The sender can be the user himself/herself.
##  In that case you will have to add an extra element to the HTML form.
my($from,$to) = ("user_sending_cv\@resume.com","sender\@server.com");

##  Compose the text message.
$message= qq{A CV has been submitted from the web.
Here are the details.

Name: $name
Age: $age
Address: $address

Also find below the attached CV.



die('Sender name not found') if (! $from);

##  Now that the file has been uploaded to the server...
##  ...send it out as an email attachment.
$mailprog ="/usr/lib/sendmail";
open(MAIL, "| $mailprog -t ");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: CV submitted from the Web!\n";
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-type: Multipart/1;\n";
print MAIL "\tboundary=$boundary\n";
print MAIL "\n";
print MAIL "\n";
print MAIL "--$boundary\n";

##  Print out any message sent with the email
if ($message){
 print MAIL "Content-type: text/plain; charset=US-ASCII\n";
 print MAIL "Content-description: Mail message body\n";
 print MAIL "Content-transfer-encoding: 7BIT\n";
 print MAIL "\n";
 print MAIL $message."\n";
 print MAIL "--$boundary\n";

##  Process the uploaded CV/Resume file.
$tmp_uploaded_file=~ s/\\/\//g;

$filename = $dir_to_store."/".$tmp_uploaded_file[$#tmp_uploaded_file];

##  Allow upload of ONLY those files that have the extension .doc
if (!($tmp_uploaded_file[$#tmp_uploaded_file] =~ /\.doc$/i )){
 print "<Font color=red>Invalid file type!!</FONT>\n";

##  The file type is valid (extension is valid!!).
open(MYFILE,"> $filename") || die $!;
binmode MYFILE;
 print MYFILE $data;

##  Print the header for that attachment.
print MAIL "Content-type: application/octet-stream; name=\"$filename\"; type=Unk
print MAIL "Content-transfer-encoding: BASE64\n";
print MAIL "Content-disposition: attachment\n";
print MAIL "\n";

##  Open the actual file and read its contents.
undef $/;
open(F, $filename) || die("Cannot read $filename: $!\n");
binmode F;
#$body = encode_base64(<F>);  ##  Use base64 for encoding the contents
$body = my_encode_base64(<F>);  ##  Use base64 for encoding the contents
close F;

##  Send out the contents!!
print MAIL $body;
print MAIL "\n";
print MAIL "--$boundary\n";

print "<P><B>Your resume has been E-mailed as an attachment.!!</B><br>\n";
##  Indicate end of all attachments.
print MAIL "--$boundary--\n";

sub my_encode_base64 ($;$){
 my $res = "";
 my $eol = $_[1];
 $eol = "\n" unless defined $eol;
 pos($_[0]) = 0;                          # ensure start at the beginning
 while ($_[0] =~ /(.{1,45})/gs) {
   $res .= substr(pack('u', $1), 1);
 $res =~ tr|` -_|AA-Za-z0-9+/|;               # `# help emacs
 # fix padding at the end
 my $padding = (3 - length($_[0]) % 3) % 3;
 $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
 # break encoded string into lines of no more than 76 characters each
 if (length $eol) {
   $res =~ s/(.{1,76})/$1$eol/g;
sureshpAuthor Commented:
Thanks for your immediate response. I was out of station, so i couldn't respond to your answer.
I will go thru the code and reply.
sureshpAuthor Commented:
Hope this code may work. I will check it.
I have a doubt regarding the mail distribution using IMAP module. I will post it as an another question. Please go through it.
Thanks for your help.
thanks, hopefully I can help you better then :)

sureshpAuthor Commented:
Pls click on this link:

can you pls solve or give hint for this question?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now