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

I am in a weird situation. I am on a closed network so I don't have access to CMAP modules. I need to send email, and I would like it in a non block fashion. I.E. do something.... send email (don't block) do more stuff.... I have plenty examples of sendmail but they all seem to block for a while. I.E. I wrote a quick test program to send a mail, and the program did not exit for almost a minute....just to send one email.
open(MAIL, "|/usr/sbin/sendmail -t"); ## Mail Header print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; ## Mail Body print MAIL "write your mail body text here\n"; close(MAIL);
Its almost like the call is blocking until my mail server does a 'send/receive', although I am not sure. Any other examples of how I can simply call the sendmail (or other, maybe mail or mailx) in a non-blocking way such that I can send a few emails from my quick script and not have it take minutes. Again please no CMAP modules I cannot get them. Thanks.

Replies are listed 'Best First'.
Re: Send mail w(non blocking) default package
by Corion (Patriarch) on Jul 24, 2012 at 06:55 UTC

    I would guess that the sendmail program takes so long to finish because it tries to either verify your sender or the recipient address.

    Yes, even you can use CPAN. For example MIME::Lite is a well-tested and well working module for sending mails, and it has very few prerequisites.

    Alternatively, you could write your mail text to a file and spawn sendmail in the background and feed it from that file:

    sendmail <$temp_mail &
Re: Send mail w(non blocking) default package
by aitap (Curate) on Jul 24, 2012 at 07:22 UTC
    if (fork == 0) { # create a subprocess local $SIG{HUP} = sub {}; # ignore SIGHUP (untested) close STDIN; # daemonize close STDOUT; open STDOUT,">/dev/null" || die $!; close STDERR; open STDERR,">/dev/null" || die $!; # do anything open(MAIL, "|/usr/sbin/sendmail -t"); ## Mail Header print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; ## Mail Body print MAIL "write your mail body text here\n"; close(MAIL); exit(0); }
    Or you can use Fcntl to make your file handles non-blocking.
    EDIT: added exit
    Sorry if my advice was wrong.