Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

body content is missing while sending mail with attachment

by bheeshmaraja (Novice)
on Jul 28, 2012 at 06:16 UTC ( [id://984151]=perlquestion: print w/replies, xml ) Need Help??

bheeshmaraja has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm a perl developer. I need to attach a word document file from my perl cgi script and send mail to our users whoever using our website. I have used mailx command to attach a file. There are no library(MIME::Lite, Mail::Sender, Mail::Mailer) available in my server. That's why I'm using the inbuilt mailx command to send mails. It is sending mails, but the problem I'm facing is I'm not getting body of the mail. I'm getting attachment only. Body message is not getting delivered.

I have a function called sendmailto which sends email to users. Can any one help me to get the body of mail too..

Function Code: sub sendmailto { $mailing_list = $_[0]; $subject = $_[1]; $message = $_[2]; $from = "naga.rajan@gmail.com" open (MAIL, "|uuencode /home/naga/testing-scripts/Package_Spec +ification_Document.doc Package_Specification_Document.doc |mailx -s \ +"$subject\" -r $from $mailing_list"); print MAIL "Hi, \n"; print MAIL "\n"; print MAIL "This email was sent using an automated email syste +m. Please do not reply, or forward to this email otherwise your messa +ge will not reach your intended recipients. \n"; print MAIL "If you like to contact us pleas write to *UNIX Tea +m. \n"; print MAIL "\n"; print MAIL "Regards, \n"; print MAIL "UNIX Team \n"; print MAIL "\n"; print MAIL "\n"; print MAIL "$message"; close MAIL; }
My OS details: uname -a SunOS testingserver 5.10 Generic_147440-19 sun4v sparc SUNW,T5440 perl --version This is perl, v5.8.4 built for sun4-solaris-64int (with 35 registered patches, see perl -V for more detail)

Please help me, how can we achieve this using mailx function? Installing library is a big task.. so I don't want to go for library installation.. Wanted to achieve this with built-in features available in this version of perl...

Thanks in Advance.. Nagarajan

Replies are listed 'Best First'.
Re: body content is missing while sending mail with attachment
by roboticus (Chancellor) on Jul 28, 2012 at 11:20 UTC

    bheeshmaraja:

    Caveat: I don't use mailx or uuencode or ...

    I'm expecting that the uuencode command isn't forwarding on the contents of its input stream. So it's as if your open statement is doing something equivalent to:

    echo "your message body" >/dev/null uuencode /path/to/document | mailx

    If you want to have both of them in the EMail, I'd suggest you do it more like:

    sub sendmailto { # Get the text of the attachment: my $attachment = `uuencode /path/to/document.doc`; # Create the mail body: my $message = <<EOMSG; Hullo, all This message blah, blah, blah.... EOMSG # Now send both chunks to mailx open (MAIL, "|mailx -s \"$subject\" -r $from $mailing_list); print MAIL $message; print MAIL $attachment; close MAIL; }

    Of course, I'd change the open to use the three argument form, add a little error checking, and neaten it up a little bit. Also this is just typed into a web browser, never tested, so don't be alarmed if it cures world hunger or causes the outbreak of WWIII. Just sayin'.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks roboticus. I have modified my function and now I'm able to get both body message as well as attachment in mail. Function Code:
      sub sendmailto { $mailing_list = $_[0]; $subject = $_[1]; $message = $_[2]; $from = "naga.rajan@gmail.com" $attachment = `uuencode "/home/naga/testing-scripts/Packa +ge_Specification_Document.doc" "Package_Specification_Document.doc" open (MAIL, "|mailx -s "$subject\" -r $from $mailing_list"); print MAIL "Hi, \n"; print MAIL "\n"; print MAIL "This email was sent using an automated email syste +m. Please do not reply, or forward to this email otherwise your messa +ge will not reach your intended recipients. \n"; print MAIL "If you like to contact us pleas write to *UNIX Tea +m. \n"; print MAIL "\n"; print MAIL "Regards, \n"; print MAIL "UNIX Team \n"; print MAIL "\n"; print MAIL "\n"; print MAIL "$message"; print MAIL $attachment; close MAIL; }
      If I paste  print MAIL $attachment; next to mailx  open (MAIL, "|mailx -s "$subject\" -r $from $mailing_list"); then body content is getting missed. If we paste before closing MAIL file handler then we are able to get both body message as well as attachment in mail.

        @ uuencode is not in my meachine .Still i can use same meathod to send email attachment and message body ?

        .
Re: body content is missing while sending mail with attachment
by tobyink (Canon) on Jul 28, 2012 at 11:27 UTC

    Meh. Typed a reply to this earlier, but must have only hit "preview" because it's disappeared. :-(

    Anyway, the gist was: don't use the uuencode command. Just use mailx's "-a" option to attach a file.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: body content is missing while sending mail with attachment
by Anonymous Monk on Jul 28, 2012 at 06:45 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://984151]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-19 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found