Why is this simple script not sending email?

Discussion in 'Perl Misc' started by fastjack, May 7, 2004.

  1. fastjack

    fastjack Guest

    #My email is in the "To: " line. just not listed below for spam reasons.

    #!/usr/bin/perl

    $mailprog = '/usr/lib/sendmail -i -t';
    open(MAIL,"|$mailprog");

    print MAIL "To: \n";
    print MAIL "From: (Test )\n";
    # Add Ability to send HTML
    print MAIL "Content-type: text/html\n";

    # Check for Message Subject
    print MAIL "Subject: Test\n\n";

    print MAIL "This is a test\n\n";
    close (MAIL);
    print "Content-type: text/html\n\n";
    print "<html><h1>The Email has been sent</h1></html>\n";
     
    fastjack, May 7, 2004
    #1
    1. Advertising

  2. fastjack

    Paul Lalli Guest

    On Fri, 7 May 2004, fastjack wrote:

    > #My email is in the "To: " line. just not listed below for spam reasons.
    >
    > #!/usr/bin/perl

    use strict;
    use warnings;

    >
    > $mailprog = '/usr/lib/sendmail -i -t';

    my $mailprog = '/usr/lib/sendmail -i -t';

    > open(MAIL,"|$mailprog");


    open MAIL, "|$mailprog" or die "Cannot start mail program: $!";

    >
    > print MAIL "To: \n";
    > print MAIL "From: (Test )\n";


    I'm not convinced that's an acceptable header, though I could be wrong.
    print MAIL "From: Test <>\n";


    > # Add Ability to send HTML
    > print MAIL "Content-type: text/html\n";
    >
    > # Check for Message Subject
    > print MAIL "Subject: Test\n\n";
    >
    > print MAIL "This is a test\n\n";
    > close (MAIL);
    > print "Content-type: text/html\n\n";
    > print "<html><h1>The Email has been sent</h1></html>\n";


    If this revised script doesn't die at the open line, then there's
    something wrong with the information you're sending to sendmail. If it
    does, figure out what to do based on the error message. (If you're
    running this via a webserver, as it appears, you will need to check the
    server logs and/or run the script on the command line to see the error
    message).

    Paul Lalli
     
    Paul Lalli, May 7, 2004
    #2
    1. Advertising

  3. On 7 May 2004 11:16:11 -0700, fastjack <> wrote:
    > #My email is in the "To: " line. just not listed below for spam reasons.


    One reason your script does not work is because "@domain" and "@test" are
    list variables when in enclosed in double quotes, so your addresses are
    invalid. You either need to escape the "\@" or use single quotes around
    that part of it like 'To: '."\n"

    However, you also fail to test your open() and close() to see if there are
    any other problems.

    > #!/usr/bin/perl
    >
    > $mailprog = '/usr/lib/sendmail -i -t';
    > open(MAIL,"|$mailprog");
    >
    > print MAIL "To: \n";
    > print MAIL "From: (Test )\n";
    > # Add Ability to send HTML
    > print MAIL "Content-type: text/html\n";
    >
    > # Check for Message Subject
    > print MAIL "Subject: Test\n\n";
    >
    > print MAIL "This is a test\n\n";
    > close (MAIL);
    > print "Content-type: text/html\n\n";
    > print "<html><h1>The Email has been sent</h1></html>\n";



    --
    David Efflandt - All spam ignored http://www.de-srv.com/
     
    David Efflandt, May 7, 2004
    #3
  4. In article <>, fastjack wrote:
    > #My email is in the "To: " line. just not listed below for spam reasons.
    >
    > #!/usr/bin/perl
    >
    > $mailprog = '/usr/lib/sendmail -i -t';
    > open(MAIL,"|$mailprog");


    Why not try:
    open(MAIL,"|$mailprog") || die "can't open $mailprog: $!";

    And sense this appears to be CGI, add the following BEFORE the above line:

    use CGI::Carp qw(fatalsToBrowser);

    >
    > print MAIL "To: \n";
    > print MAIL "From: (Test )\n";
    > # Add Ability to send HTML
    > print MAIL "Content-type: text/html\n";
    >
    > # Check for Message Subject
    > print MAIL "Subject: Test\n\n";
    >
    > print MAIL "This is a test\n\n";
    > close (MAIL);
    > print "Content-type: text/html\n\n";
    > print "<html><h1>The Email has been sent</h1></html>\n";


    Do you have access to the mail logs? Do you know for certain that sendmail is
    in the location you have specified?

    Kevin
     
    Kevin Collins, May 7, 2004
    #4
  5. In article <>, David Efflandt wrote:
    > On 7 May 2004 11:16:11 -0700, fastjack <> wrote:
    >> #My email is in the "To: " line. just not listed below for spam reasons.

    >
    > One reason your script does not work is because "@domain" and "@test" are
    > list variables when in enclosed in double quotes, so your addresses are
    > invalid. You either need to escape the "\@" or use single quotes around
    > that part of it like 'To: '."\n"
    >
    > However, you also fail to test your open() and close() to see if there are
    > any other problems.


    Not to sound like a smartass, but why would you test a close()? I have never
    thought about it before I read this post... If it doesn't close with close(),
    it definitely will on program termination.

    >> #!/usr/bin/perl
    >>
    >> $mailprog = '/usr/lib/sendmail -i -t';
    >> open(MAIL,"|$mailprog");
    >>
    >> print MAIL "To: \n";
    >> print MAIL "From: (Test )\n";
    >> # Add Ability to send HTML
    >> print MAIL "Content-type: text/html\n";
    >>
    >> # Check for Message Subject
    >> print MAIL "Subject: Test\n\n";
    >>
    >> print MAIL "This is a test\n\n";
    >> close (MAIL);
    >> print "Content-type: text/html\n\n";
    >> print "<html><h1>The Email has been sent</h1></html>\n";

    >
    >
     
    Kevin Collins, May 7, 2004
    #5
  6. (Kevin Collins) writes:
    > Not to sound like a smartass, but why would you test a close()? I have never
    > thought about it before I read this post... If it doesn't close with close(),
    > it definitely will on program termination.


    close() can fail in a number of odd, but potentially care-about-able
    cases. If a filehandle's buffer contains more data than will fit on
    the disk when it's flush()ed, the close() can fail. If the filehandle
    is a socket endpoint, bad things can happen there, too. Anyway, it
    doesn't really hurt anything, and it can give you more information on
    a failure if you use it, so why not?

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
     
    Eric Schwartz, May 7, 2004
    #6
  7. In article <>, Eric Schwartz wrote:
    > (Kevin Collins) writes:
    >> Not to sound like a smartass, but why would you test a close()? I have never
    >> thought about it before I read this post... If it doesn't close with close(),
    >> it definitely will on program termination.

    >
    > close() can fail in a number of odd, but potentially care-about-able
    > cases. If a filehandle's buffer contains more data than will fit on
    > the disk when it's flush()ed, the close() can fail. If the filehandle
    > is a socket endpoint, bad things can happen there, too. Anyway, it
    > doesn't really hurt anything, and it can give you more information on
    > a failure if you use it, so why not?


    Ok, those sound like intelligent reasons to me...

    Kevin
     
    Kevin Collins, May 7, 2004
    #7
  8. David Efflandt wrote:
    >
    > On 7 May 2004 11:16:11 -0700, fastjack <> wrote:
    > > #My email is in the "To: " line. just not listed below for spam reasons.

    >
    > One reason your script does not work is because "@domain" and "@test" are
    > list variables when in enclosed in double quotes, so your addresses are

    ^^^^^^^^^^^^^^
    Perl doesn't have list variables, just scalars, arrays and hashes.

    perldoc -q "What is the difference between a list and an array"



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 8, 2004
    #8
  9. Kevin Collins wrote:
    >
    > In article <>, David Efflandt wrote:
    > >
    > > However, you also fail to test your open() and close() to see if there are
    > > any other problems.

    >
    > Not to sound like a smartass, but why would you test a close()? I have never
    > thought about it before I read this post... If it doesn't close with close(),
    > it definitely will on program termination.


    perldoc perlipc
    [snip]

    Using open() for IPC
    Perl's basic open() statement can also be used for unidi­
    rectional interprocess communication by either appending
    or prepending a pipe symbol to the second argument to
    open(). Here's how to start something up in a child pro­
    cess you intend to write to:

    open(SPOOLER, "| cat -v | lpr -h 2>/dev/null")
    || die "can't fork: $!";
    local $SIG{PIPE} = sub { die "spooler pipe broke" };
    print SPOOLER "stuff\n";
    close SPOOLER || die "bad spool: $! $?";

    [snip]

    Be careful to check both the open() and the close() return
    values. If you're writing to a pipe, you should also trap
    SIGPIPE. Otherwise, think of what happens when you start
    up a pipe to a command that doesn't exist: the open() will
    in all likelihood succeed (it only reflects the fork()'s
    success), but then your output will fail--spectacularly.
    Perl can't know whether the command worked because your
    command is actually running in a separate process whose
    exec() might have failed. Therefore, while readers of
    bogus commands return just a quick end of file, writers to
    bogus command will trigger a signal they'd better be pre­
    pared to handle. Consider:

    open(FH, "|bogus") or die "can't fork: $!";
    print FH "bang\n" or die "can't write: $!";
    close FH or die "can't close: $!";

    That won't blow up until the close, and it will blow up
    with a SIGPIPE. To catch it, you could use this:

    $SIG{PIPE} = 'IGNORE';
    open(FH, "|bogus") or die "can't fork: $!";
    print FH "bang\n" or die "can't write: $!";
    close FH or die "can't close: status=$?";



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 8, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    991
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,261
    Smokey Grindel
    Dec 2, 2006
  3. Luke
    Replies:
    2
    Views:
    4,814
    Nigel Wade
    Mar 15, 2007
  4. rote
    Replies:
    8
    Views:
    796
  5. Kevin B. Pease
    Replies:
    1
    Views:
    389
    A. Sinan Unur
    Nov 19, 2004
Loading...

Share This Page