tags:

views:

214

answers:

5

I am using sendmail in perl and noticed (after much banging of head against wall) that when the script is run at the command line it needs you to leave out the \n(s) after your e-mail and the recipient's email address in order to format the mail correctly, but when running via CGI if those \n(s) aren't there it returns an error stating that the recipient's e-mail is malformed.

Has anyone else encountered this? What are the two doing differently?

A: 

Where is the data coming from? Hard coded in the script, or from a web form?

Just as an aside, if you get the recipient's email address from a web form, your form will be used by spammers. It's a 100% guarantee.

Paul Tomblin
thanks for the head's up. it's not coming from a web form.
Dr.Dredel
+2  A: 

I am betting that you are getting data from prompts in on the commandline and not chomping them like this:

my $send_to = <>;

This means $send_to will already have a "\n". To make them both work the same way chomp the variables:

my $send_to = <>;
chomp($send_to);

or just

chomp(my $send_to = <>);
Chas. Owens
no no... I wouldn't post the question without first making sure. The data will come from the user eventaully, but for the purpose of this test, I'm just hardcoding values.
Dr.Dredel
Well, are you hardcoding them with newlines then? You are asking what could cause it with no real clues, what do expect but a bunch of guesses? The variables already having newlines could cause a problem. The next thing to look for would be if the environment is different when you run commandline vs CGI.
Chas. Owens
A: 

The term "CGI" is broad, if you mean your perl script run as a CGI versus yur perlscript run at the command line, I would look toward the pathing that the script has and its general inherited environment. Especially if your running it as different userids. If the webserver is in a chroot, etc.

use Data::Dumper;
warn(Dumper(\%ENV));
hpavc
interesting. Yeah, it is a perl script with a cgi extension. When I run it like so >perl -l foo.cgi [enter] it insists that the to field be "To: [email protected]"but when I run it through the browser it fails unless it's "To: [email protected]\n"I'm not sure what the environment would have to do with it, but I'll poke around.
Dr.Dredel
Why are you using "perl -l"? The -l option messes with line endings which might be causing the different behaviour.
noswonky
A: 

So I'm guessing that you have something like this for running it via the command line:

my $your_email = "[email protected]";
my $recipient_email = "[email protected]";

and this when "running via CGI":

my $your_email = "[email protected]\n";
my $recipient_email = "[email protected]\n";

So the question I would ask you then is how you're calling sendmail with the above variables, and also what you mean when you say "running via CGI" versus running via the command line? Are you just adding CGI code and still running via the command line or by visiting its URL in a web browser?

Youdaman
I'm printing to sendmail in the standard prescribed way. I'm running the same file, just changing it between runs. When I say CGI, I mean, I'm calling it via a browser, having chmoded it +x and 765. At the command line I'm just running it as perl -l foo.cgi
Dr.Dredel
A: 

In a couple of your comments you mention that you're running the script from the command line with the -l option (perl -l foo.cgi).

The -l option enables automatic line-ending processing, and as your problem is with line endings, I suggest you try it without the -l.

noswonky