Solved

Using mail module --Sender module

Posted on 2001-08-28
9
213 Views
Last Modified: 2008-03-06
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
Comment
Question by:sureshp
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6431950
can you verify you have MIME::Lite installed on your webserver to handle sending the attachment?
0
 
LVL 1

Author Comment

by:sureshp
ID: 6435113
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
 
LVL 8

Expert Comment

by:bebonham
ID: 6435148
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
 
LVL 1

Author Comment

by:sureshp
ID: 6435534
bebonham,
Thanks a lot for your reply.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
bebonham earned 125 total points
ID: 6436235
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
 
LVL 1

Author Comment

by:sureshp
ID: 6449603
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
 
LVL 1

Author Comment

by:sureshp
ID: 6449687
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
 
LVL 8

Expert Comment

by:bebonham
ID: 6450713
thanks, hopefully I can help you better then :)


Regards,
Bob
0
 
LVL 1

Author Comment

by:sureshp
ID: 6466241
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now