in reply to Re: sendmail working for me but not another
in thread sendmail working for me but not another

## Create the email message body $email_message = qq{To: $recipient Subject: Adoption Application From: $fstname $lstname<$email> First name: $fstname Last name: $lstname

So you are saying it should look like this? Thank you toby, i'm trying to clarify it, its just hard to explain. I do receive emails of the form data, i have received about 20 so far. However a few people have complained they have submitted but I haven't received the data, neither have the other 2 email boxes. One person in particular, submits the form and makes it to the confirmation page, but no one gets the data in an email. I know the code is horrible but i'm learning. I will just remove the email validity code. I don't send an email to that, its just a means to collect their address for this application.

My host is telling me i'm not allowed to look at the email logs, I've asked them what I can access and i'm waiting to hear. I'm not sure they know what i'm talking about, but they did present to me a list of emails that were sent successfully with my perl but that doesn't help me.. cause I know they were sent, I have them. I can't ssh in right now, having trouble with it and i'm trying to correct that through them. I will try this new empty line you asked me to add, and also what bliako mentioned. Thanks you two.

Replies are listed 'Best First'.
Re^3: sendmail working for me but not another
by haukex (Archbishop) on May 25, 2020 at 17:57 UTC
    So you are saying it should look like this?

    Yes, what you showed is correct. Note it's generally better to use heredocs instead of a multiline qq, so e.g.

    my $email_message = <<"ENDMAIL"; To: $recipient Subject: Adoption Application ... ENDMAIL
    My host is telling me i'm not allowed to look at the email logs, I've asked them what I can access and i'm waiting to hear.

    You could write out your data to a logfile on your account, in case something with the emails doesn't work out.

    I know the code is horrible but i'm learning.

    If this were a legacy script, ok, but if this a new script, then you'd be much better off learning something more modern. I've been doing a lot with Mojolicious recently, and I think it's pretty great. See Mojolicious::Guides::Tutorial for getting started. Otherwise, at the very least what this script needs is to Use strict and warnings! See also UP-TO-DATE Comparison of CGI Alternatives.

    And with that many variables, it'd be much better to store them in a data structure like a hash. Then you could do things like loop over it so you don't have to repeat the regex a bunch of times. (Also, note you don't seem to be doing anything with the yardtypeother field.)

    Also, you should check if the host has Email::Sender::Simple installed, that should be much better and easier to use than shelling out to sendmail.

      See Mojolicious::Guides::Tutorial for getting started.
      Thanks, I will definitely check mojo out. My script is legacy, at least this is a good opportunity to learn perl, should have long time ago. I will up date my script with heredocs. I was using strict and warnings but I keep getting blank page or 404. I've been working on finding out what is causing it, but havn't been able to. I'm using use CGI::Carp qw(fatalsToBrowser warningsToBrowser); When I get rid of strict it will process with warnings, 3 variables were not initialized properly. but with strict it is just a blank screen. There is a lot of information here, i'm going to go through what everyone has given me and change my code. If I could get some kind of log, it would help but i'm still waiting on my host..

        I was using strict and warnings but I keep getting blank page or 404. I've been working on finding out what is causing it, but havn't been able to.

        When you add strict, you need to declare your variables with my, as in my $fstname = substr param ('first'), 0, 40; (see Use strict and warnings). Also, note that CGI scripts can be run at the command line, as in perl script.cgi 'first=John&last=Doe&...', that way you'll see any error messages the script may be giving you (you might want to disable the email sending while testing, of course).

        If I could get some kind of log, it would help but i'm still waiting on my host.

        Yes, getting access to the server logs would be helpful (see also CGI Help Guide and Troubleshooting Perl CGI scripts). With my suggestion I meant that you could add code to your script that writes the data you've received to a log file as well (because POST data won't be in the server logs anyway), that way you still have a backup of the data in case someone submits the form and the email isn't sent out. (A database would be even better, but let's take it one step at a time :-) )

Re^3: sendmail working for me but not another
by tobyink (Canon) on May 25, 2020 at 20:08 UTC

    Also, here's what I mean about eliminating repetition. It's untested though.

    #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; $CGI::POST_MAX = 1024 * 1; my %fields = ( fstname => [ first => 40 ], lstname => [ last => 40 ], adda => [ 1ad1a => 60 ], addb => [ 1ad1b => 60 ], city => [ 1ad2 => 60 ], state => [ 1ad3 => 20 ], zip => [ 1ad4 => 5 ], email => [ eadd => 70 ], phone => [ pnum => 16 ], residence => [ residence => 9 ], yardtype => [ yardtype => 16 ], yardtypeother => [ yardtypeother => 40 ], landlord => [ landlord => 40 ], preadda => [ 2ad1 => 60 ], precity => [ 2ad2 => 60 ], prestate => [ 2ad3 => 20 ], prezip => [ 2ad4 => 5 ], alone => [ alone => 14 ], household => [ household => 100 ], vet => [ vet => 45 ], pet => [ pet => 3 ], petname => [ petnme => 30 ], currentpets => [ currentpets => 60 ], previouspets => [ previouspets => 60 ], references => [ references => 140 ], ok => [ ok => 7 ], ); my %values; for my $key ( keys %fields ) { my ( $post_param, $max_length ) = @{ $fields{$key} } my $value = substr( param($post_param), 0, $max_length ); $value =~ s/[\|\/\\}{\[\]\(\)\*&\^%\$\#<>;:]/ /g; $values{$key} = $value; } $values{email} =~ tr/[A-Z]/[a-z]/; ## Check email address if ( $values{email} ne "" and $values{email} !~ /\@/ ) { print "Content-type: text/plain\n\n"; print "error (Your email is invalid, please check and resubmit you +r application)"; exit; } ## Check data my $total_form = $values{fstname} . $values{lstname} . $values{email}; if ( length($total_form) < 25 ) { print "Content-type: text/plain\n\n"; print "error (The form was incomplete, please check that you fille +d in your full first and last name, and email address, then resubmit +your application.)"; exit; } my $recipient = "email1\@emailsrv1, email2\@emailsrv2, email3\@emailsr +v3"; ## Create the email message body my $email_message = <<"MESSAGE" To: $values{recipient} Subject: Adoption Application From: $values{fstname} $values{lstname} <$values{email}> First name: $values{fstname} Last name: $values{lstname} Current address: $values{adda} $values{addb} $values{city} $values{state} $values{zip} _________________________________________________ Email address: $values{email} Phone number: $values{phone} Type of residence: $values{residence} Yard: $values{yardtype} Landlord contact info: $values{landlord} Previous address: $values{preadda} $values{precity} $values{prestate} $values{prezip} How many hours will pet be alone: $values{alone} Name and age of people in household: $values{household} Veterinarian: $values{vet} Dog or Cat: $values{pet} Name of pet requested: $values{petname} Current pets: $values{currentpets} Previous pets: $values{previouspets} References: $values{references} Aggree to consent form: $values{ok} MESSAGE ## Send email my $mailprog = '/usr/sbin/sendmail'; open my $mailhandle, '|-' "$mailprog -t" or die "Could not open mailhandle"; print $mailhandle $email_message; close $mailhandle; print "Location: application-submitted\n\n"; exit;

      Wow, thanks for this. I see what you mean. I will go through it after I figure out where my error is. In the send mail part

      my $mailprog = '/usr/sbin/sendmail'; open my $mailhandle, '|-' "$mailprog -t" or die "Could not open mailhandle"; print $mailhandle $email_message; close $mailhandle;
      What are you doing with mailhandle?

        For details, see open and fork functions to start.

        That open syntax forks the process (which I had long forgotten); $mailprog is run in child process. A pipe is opened to $mailprog in order to write to its standard input by writing to $mailhandle. (If the pipe-open syntax were -|, then that would be to read standard output of the command.)

Re^3: sendmail working for me but not another
by tobyink (Canon) on May 25, 2020 at 19:47 UTC

    Yep, that's where the empty line should be.

    Sucks not having access to logs. I'm generally admin of most machines I work on these days, but I have had to deal with limited hosting accounts at times and not having direct access to logs can be one of the most frustrating things, especially with how prone I am to typos and other silly little mistakes.

    And don't worry about your code. It's not that bad; standard beginner stuff. Could be a lot worse. It's pretty repetitive though. As you start learning more, you'll learn to hate repetitive code, and start to use loops and write things in a more abstract way. Everybody has to start somewhere though. :)