Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Unexpected 'newline' token breaks sendmail.

by hesco (Deacon)
on Oct 13, 2006 at 15:41 UTC ( [id://578162]=perlquestion: print w/replies, xml ) Need Help??

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

I'm getting errors which look like this, trying to run the code below. My question is, where does this 'newline' get introduced and what can I do about it that I haven't already done?

-- Hugh

Update
Thanks folks. I'm starting to toy around the edges with refactoring some code I wrote years ago and stretched and prodded at for a long time. Its been installed on three or five servers so far that I am aware of. You're seeing small pieces of it. This routine was pulled from a cgi script and has just been dropped into a module with a proper ->new() constructor. My $self = { }; above was simply an adaptation to create something runnable.

Thanks VSarkiss. That was helpful.

My error messages and a runnable snippet follows.

[Fri Oct 13 07:32:42 2006] [error] [client 192.168.1.102] sh: -c: line + 1: syntax error near unexpected token `newline' [Fri Oct 13 07:32:42 2006] [error] [client 192.168.1.102] sh: -c: line + 1: `/usr/sbin/sendmail -f script-cgi@tld.hostname.com him@thatone.ex +ample.net,her@theotherone.example.net "Coordinator's Name" <test@mydo +main.com>' [Fri Oct 13 07:32:42 2006] [error] [client 192.168.1.102] Now sending +email using the `sendmail` method.
And the code I'm working with should run as this:
#!/usr/bin/perl -w use strict; use Mail::Mailer; my $self = {}; my $from = "script-cgi\@tld.hostname.com"; my $to = "him\@thatone.example.net,her\@theotherone.example.net"; my $cc = "\"Coordinator's Name\" <test\@mydomain.com>"; my $subject = "Test sendemail path"; my $email_method = "sendmail"; my $msg =<<EOM; This is a test message. This is only a test. EOM &sendemail($self,$from,$to,$cc,$subject,$msg,$email_method); 1; sub sendemail() { my($self,$from,$to,$cc,$subject,$msg,$email_method)=@_; my($hdr,$qmail,$sendmail); chomp($from); chomp($to); chomp($cc); $qmail = "/var/qm/bin/qmail-inject"; $sendmail = "/usr/sbin/sendmail -f $from $to $cc"; if($email_method eq "qmail"){ # code similiar to the next case } elsif ($email_method eq "sendmail") { $hdr =<<EOH; From: $from To: $to Cc: $cc Subject: $subject EOH open (MAILER, "|$sendmail" ) || die "Unable to open sendmail"; print MAILER ( $hdr, $msg ); close MAILER; print STDERR "Now sending email using the `sendmail` method."; return 1; } elsif ($email_method eq "mailer") { my $mailer = Mail::Mailer->new(); $mailer->open({ From => "$from", To => "$to", Cc => "$cc", Subject => "$subject", }) or die "Can not open: $! \n"; print $mailer $msg; $mailer->close(); return 1; } else { return 0; } } # END sendemail()
if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Unexpected 'newline' token breaks sendmail.
by VSarkiss (Monsignor) on Oct 13, 2006 at 16:41 UTC

    As davido and Fletch have pointed out above, you should really be using Mail::Mailer.

    But as to the error message: those are coming not from Perl, but from the shell that perl is spawning to run sendmail. You're using unquoted angle brackets in the email addresses, which the shell interprets as redirection symbols. Except there's no file name or symbol after the >, just a newline, which is a ... wait for it ... syntax error because the newline is not expected.

    You could fix it by escaping the angle brackets, but as the others have mentioned, it'd probably be better to go with Mail::Mailer.

    Update
    Cleaned up text spacing a little.

      which is a ... wait for it ... syntax error because the newline is not expected.

      I so saw that coming!

Re: Unexpected 'newline' token breaks sendmail.
by davido (Cardinal) on Oct 13, 2006 at 16:00 UTC

    I can't say whether this contributes to your problem, but it is a problem: You're declaring sendemail() with an empty prototype. You're then calling it with arguments, but you're probably not getting a related warning because calling with the & ampersand (as in &mysub(args)) syntax silences prototype checking.

    Your sub call should look like this instead:

    sendemail( args );

    And your sub declaration / definition should look like this:

    sub sendemail { #code here }

    Notice how parenthesis were excluded from the sub declaration.

    More on this in perlsub. But the short of it is don't use prototypes.

    Also, your code example makes it look almost like you're creating an object (my $self = {}), though it never gets blessed, and it's happening out in the open instead of isolated within a subroutine such as new() in a separate module. The 1; line also makes it look like you started by simply cutting code out of a module. Also, your sendemail() subroutine declares a "$self".... but what for if there's no object and it's not an object method?


    Dave

Re: Unexpected 'newline' token breaks sendmail.
by Fletch (Bishop) on Oct 13, 2006 at 16:18 UTC

    Also fishy is the fact that you use Mail::Mailer and then . . . erm, don't use it and reimplement your own. Presuming you've got the qmail compatibility sendmail command in place I'd think Mail::Mailer should use it just fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://578162]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-16 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found