Mail::Sender blocked - Use MIME-Lite to send text file as Attachment


Mail:Sender no longer works where my site is hosted. They decided make a change. Scripts can no longer use SMTP.

They suggested that I do
shell_exec("cat email.txt | /var/qmail/bin/qmail-inject");
but I don't know enough about MIME to make it so.

So I thought that I could use MIME-Lite. But it didn't work. No mail is delivered.

Any ideas?

use MIME::Lite;

    $msg = MIME::Lite->new(
                 From    =>'',
                 To      =>'',
                 Subject =>'A message with 2 parts...',
                 Type    =>'multipart/mixed'

    ### Add parts (each "attach" has same arguments as "new"):
    $msg->attach(Type     =>'TEXT',
                 Data     =>"Here's the file you wanted"
    $msg->attach(Type     =>'text/plain',
                 Path     =>'nc/jh10083.txt',
                Filename =>'nc/jh10083.txt',
         Disposition => 'attachment'
$msg->send('sendmail', "/usr/sbin/sendmail");
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

are you getting any error message?

what if you replace /usr/sbin/sendmail with  /var/qmail/bin/qmail-inject ?
BrentNJAuthor Commented:
No error message. Unless I use /var/qmail/bin/qmail-inject instead of sendmail.
It looks to me like you should be able to use the 'sendmail' method to send mail via qmail-inject. It's one of qmail's advertised compatibilities.

$msg->send( 'sendmail', "/var/qmail/bin/qmail-inject");

If you've already tried this, we'd need to see whatever error messages are produced, to make much further progress. The same people who cut off your SMTP access may have done something that requires you to go through an extra hoop (but writing the outgoing mail to an external mail.txt file just so you can 'cat' it through a pipe to 'qmail-inject' would seem to be a pretty silly hoop to have to go through).
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

BrentNJAuthor Commented:
Previously tried
$msg->send('qmail', "/var/qmail/bin/qmail-inject");
which had error
Can't locate object method "send_by_qmail" via package "MIME::Lite"

I just tried
$msg->send( 'sendmail', "/var/qmail/bin/qmail-inject");
which has same problem as
$msg->send('sendmail', "/usr/sbin/sendmail");
 ---- no email is received at destination

The following sends the text file from the command line as an attachment, but removes line feeds or form feeds (not sure which but file in notepad is on one line - looks OK in VIM)
cat nc/jh10083.txt | uuencode nc/jh10083.txt | /usr/sbin/sendmail

I suppose getting Mime-Lite to work would be the best way to go.
BrentNJAuthor Commented:
The following works - it sends the attachment with proper line/form feeds.

One last question, besides attaching the file, the contents are including in the message itself. I wanted to send a different message.



use MIME::Lite;

$SUBJECT="Your attachment";

# Create a new multipart message:
$msg = new MIME::Lite
   From    => "$MAILFROM",
   To      => "$MAILTO",
   Subject => "$SUBJECT",
   Type    => 'multipart/mixed';

# Add parts (each "attach" has same arguments as "new"):
attach $msg
   Type     => 'text/plain',
   Path     => "$TXTFILE";

# Output the message to sendmail

open (SENDMAIL, "| /usr/sbin/sendmail -t -oi");
open(F,">email.txt") && print F $msg->as_string(); close(F);
system("cat email.txt | /var/qmail/bin/qmail-inject");
It's getting more and more odd. If you can pipe something into sendmail explicitly, why the heck won't it work using MIME::Lite's normal methods? Your flags "-t -oi" match MIME::Lite's except it adds "-oem" to say "mail back errors".

I don't imagine you have access to your mail logs to see if any entry appears when you attempted to send mail with either sendmail or qmail-inject using the methods we were originally proposing, do you?
> ..  why the heck won't it work using MIME::Lite ..
'cause it uses some defaults, like /usr/lib(sendmail, which might not be accurate on this server
If the ISP responds with a recomendation, you either should use it, or ask why this must be done this (strange and unsecure) way.
BrentNJAuthor Commented:
Thanks everyone. Yes its odd. This company was working out well for me until recently. After an upgrade, I could access files OK with SSH but not FTP; They added a footer to all email about reporting SPAM; They blocked SMTP; Probably something else.

Anyway, I don't have access to any mail logs.

To sum up issue with attaching files
1) Mail::Sender won't work anymore as SMTP is blocked from scripts;
2) MIME::Lite with $msg->send('sendmail', "/usr/sbin/sendmail"); or $msg->send( 'sendmail', "/var/qmail/bin/qmail-inject"); results in no email being delivered and no notification of failed delivery to originating address;
3) shell_exec("cat email.txt | /var/qmail/bin/qmail-inject"); is too cumbersome - I don't want to write MIME details in email.txt to denote attachments;
4) cat nc/jh10083.txt | uuencode nc/jh10083.txt | /usr/sbin/sendmail sends an attachment but concatenates the text into one line; and

5) MIME::Lite with open (SENDMAIL, "| /usr/sbin/sendmail -t -oi");
$msg->print(\*SENDMAIL); close(SENDMAIL);
works great, but also adds the text file contents into the body of the email itself.

At this point item 5 is the closest, but I’m nervous that if item 2 doesn’t work, then it’s just a matter of time before item 5 stops working.

For item 5, any idea how to eliminate the text file contents from displaying in the body of the email itself?

Anything else that I should try or be concerned with?

Thanks again
sounds that the culprit is sendmail (or whatever your ISP installed under this name).
sendmail itself cannot handle attachments propper, you need to do it in the stream (mail body) piped to sendmail.
MIME::Lite does this for you.
If it does not send mails, you need to check the logfiles (you need help from your ISP there).
If all fails, you may use Net::SMTP and compute the mail header and body yourself, and then send it using native SMTP commands (using Net::SMTP). This gives you at least the chance to catch the errors from you mail-server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I'd try:

$mailtxt = $msg->print;

open QMAIL, "|/var/qmail/bin/qmail-inject" or die "Can not start qmail $!\n";
print QMAIL $mailtxt;
close QMAIL;
BrentNJAuthor Commented:
Thanks everyone, I ended up using

system("cat nc/jh10083.txt | unix2dos | uuencode nc/jh10083.txt | /usr/sbin/sendmail");
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.