Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2003-10-24
Medium Priority
Last Modified: 2008-02-01

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    =>'me@myhost.com',
                 To      =>'me@myhost.com',
                 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");
Question by:BrentNJ
  • 5
  • 3
  • 2
  • +2

Assisted Solution

andreif earned 80 total points
ID: 9617878
are you getting any error message?

what if you replace /usr/sbin/sendmail with  /var/qmail/bin/qmail-inject ?

Author Comment

ID: 9619550
No error message. Unless I use /var/qmail/bin/qmail-inject instead of sendmail.
LVL 20

Expert Comment

ID: 9620219
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).
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 9620662
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 me@myaddress.net

I suppose getting Mime-Lite to work would be the best way to go.

Author Comment

ID: 9620827
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");
LVL 51

Expert Comment

ID: 9620877
open(F,">email.txt") && print F $msg->as_string(); close(F);
system("cat email.txt | /var/qmail/bin/qmail-inject");
LVL 20

Assisted Solution

jmcg earned 520 total points
ID: 9621525
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?
LVL 51

Expert Comment

ID: 9622164
> ..  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.

Author Comment

ID: 9622452
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 me@myaddress.net 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
LVL 51

Accepted Solution

ahoffmann earned 520 total points
ID: 9623345
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.
LVL 48

Assisted Solution

Tintin earned 80 total points
ID: 9631289
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;

Author Comment

ID: 9651498
Thanks everyone, I ended up using

system("cat nc/jh10083.txt | unix2dos | uuencode nc/jh10083.txt | /usr/sbin/sendmail me@myhost.com");

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.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Six Sigma Control Plans

876 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