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

What is a simple way to send an e-mail using qmail? I just need a website script to send simply e-mail messages with a to, from, subject and message field.

Thank you.

2006-02-21 Retitled by g0n, as per Monastery guidelines
Original title: 'qmail'

Replies are listed 'Best First'.
Re: Sending email using qmail
by moklevat (Priest) on Feb 21, 2006 at 02:11 UTC
    How about using Mime::Lite and Mail::QmailQueue?

    Here is some code from the QmailQueue documentation:
    use Mail::QmailQueue; use Mime::Lite; # generate mail. my $mime = MIME::Lite->new( #... ); # send mail via qmail-queue. my $qmail = Mail::QmailQueue->new; $qmail->sender($ENV{USER}); $qmail->recipient('postmaster@foo.bar'); $qmail->data($mime->as_string); $qmail->send;
      Thanks but what do I put on line six where it says #... ?

        Have you tried looking into the MIME::Lite documentation and adapting the examples there?

        This might duplicate a reply you received from another monk under your sendmail question, but the following works fine for me (taken almost verbatim from the MIME::Lite docs).
        #!/usr/bin/perl -w use strict; use MIME::Lite; my $msg = MIME::Lite->new( From =>'keiusui@qmailquestions.com', To =>'keiusui@qmailquestions.com', Subject =>'Helloooooo, nurse!', Data =>"How's it goin', eh?" ); $msg->send || die "It did NOT work!";

        If you use your own e-mail address in the To: and From: fields, does this work for you from the command line?
        To get any more help with this I think you are going to have to post some of your own code, and whatever error/behavior you observe.

        Update: Since you asked about the #..., I should have mentioned that the portion of the code above that defines $msg is what can be used to define $mime in the example I gave previously. However, as the present example shows, you do not necessarily need to use the Mail::QmailQueue module if your system has an alias from sendmail to qmail-inject or qmail-queue.
Re: Sending email using qmail
by friedo (Prior) on Feb 21, 2006 at 02:09 UTC
    You can open a pipe to qmail-inject and write to it, just like sendmail. Or you can use Email::Send::Qmail which does (some) dirty work for you.
Re: Sending email using qmail
by spiritway (Vicar) on Feb 21, 2006 at 03:57 UTC

    Hi, [id://keiusui]. It just occurred to me to ask you - are you sure you've got these programs (sendmail and qmail) on your computer? I know this sounds dumb, but it's the sort of thing that can be overlooked (and I've done it).

    I'm assuming you're using Linux (if you're using Windows, then you probably don't have those programs). Try going to a console and typing 'type qmail', or 'type sendmail', and seeing whether you get a path to the program, or a message saying that the program couldn't be found. On my Linux box, I have neither qmail nor sendmail; I've got procmail.

    If you've got one or both of these, you may need to check the man pages (typing, 'man sendmail' or 'man qmail') to find out what the missing pieces are. You should be able to send mail from the command line, and certainly from a Perl script. Of course, if you don't have these programs, then you'll need to use what you have, or install one or both of them.

      My tech support has told me that qmail is installed on my server by default, while sendmail is not. However, the path to send (/usr/sbin/sendmail) doesn't return any errors, so I was assuming it is installed.

      So now I am just focusing on getting qmail to work, grrr.

        A standard qmail installation will include a program called sendmail. This emulates the standard sendmail behavior for sending email but passes all of the actual processing on to qmail. So you should be able to use any example code that you find that uses sendmail to send mail (like, for example, the perl FAQ).

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        OK, you've got sendmail. When you write your script, where is the script run from? Your computer, or the server? If it's your computer, then I suspect you won't get it to work, since the sendmail program isn't on your computer.

Re: Sending email using qmail
by pileofrogs (Priest) on Feb 21, 2006 at 05:56 UTC

    Be very careful, whatever you do. If you're not extremely careful, the forces of darkness will corrupt your script and use it to send spam. I don't know off the top of my head any really good articles that talk about how to make a script like this secure... Does anyone else know of such a thing?

    In my own experience, keeping the To: field secure is really important and also really hard. If you can hard code that value, that helps a lot. Also, make sure you quote any arguments to the mail command you use. If you don't and someone says the subject of their email is "foo; rm -r $HOME", you're going to have a bad day.

    Really, I'd stay as far away from executing a command from a web script as you possibly can. I've used Net::SMTP, which is great but unwieldy... Previous poster's have recommended things that might help.

    I'm paranoid, though, and I fear they're just wrappers around the command line... If they are, you need to stay cautious of what you send them. Do they quote the arguments to the commands they call? Are you sure?

    --Pileofrogs

Re: Sending email using qmail
by hesco (Deacon) on Feb 21, 2006 at 08:17 UTC
    qmail-inject is the cli tool for injecting a new email into the delivery que. Like so many Mail Transfer Agents, its written to replicate the functionality of sendmail. I'd bet on inspection you might find that /usr/bin/sendmail is simply a symlink to qmail-inject.

    man qmail-inject ought to spell out the details. I switch between using mailx (or nail when I need to control the From: line in the headers), or Mail::Mailer, which as I recall takes a hash ref to the header parameters, and permits you to open, print to then close the $mail handle, as if it were any other filehandle. On close its handed off to the mta.

    -- Hugh