• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • 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:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

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

</html>


and code for cgi:
#!/usr/bin/perl
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.
Regards,
XYZ.
END_OF_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;
$sender->Send($body);
$sender->Close();
print "Sent!";

Please modify my cgi code.

Note:

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

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

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.
0
 
bebonhamCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sureshpAuthor Commented:
bebonham,
Thanks a lot for your reply.
0
 
bebonhamCommented:
this is courtesy of maneshr

=========attach_cv_details.html
<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><B>Name</B></TD>
<TD><input type=text name=name></TD>
<TR>

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

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

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

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

=========attach_cv_details.pl
#!/usr/local/bin/perl

$|++;


use CGI;

$query=new CGI;

##  Read HTML form variables into PERL variables
$name=$query->param('name');
$age=$query->param('age');
$address=$query->param('address');

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

##  Location of the uploaded file
$dir_to_store="/tmp";

##  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.

};

$boundary="Message-Boundary-19990614";

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.
$uploaded_file=$query->param('cv');
$tmp_uploaded_file=$query->param('cv');
$tmp_uploaded_file=~ s/\\/\//g;
@tmp_uploaded_file=split(/\//,$tmp_uploaded_file);

$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";
 exit;
}

##  The file type is valid (extension is valid!!).
open(MYFILE,"> $filename") || die $!;
binmode MYFILE;
while($bytesread=read($uploaded_file,$data,1024)){
 $size+=$bytesread;
 $body.=$data;
 print MYFILE $data;
}
close(MYFILE);
close($uploaded_file);

##  Print the header for that attachment.
print MAIL "Content-type: application/octet-stream; name=\"$filename\"; type=Unk
nown\n";
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;
$/="\n";

##  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";
close(MAIL);

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);
   chop($res);
 }
 $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;
 }
$res;
}
0
 
sureshpAuthor Commented:
bebonham,
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.
Thanks
0
 
sureshpAuthor Commented:
bebonham,
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.
0
 
bebonhamCommented:
thanks, hopefully I can help you better then :)


Regards,
Bob
0
 
sureshpAuthor Commented:
bebonham,
Pls click on this link:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=perl&qid=20179311

can you pls solve or give hint for this question?
Thanks.
0

Featured Post

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.

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