#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Can perl end not printing to the web browser?


    I have a form tha calla a perl script. All I want the script to do is validate the email address and send an email. No thing else. However, when I try this, I get an error, and the error goes away when I have a print command after everything like:

    print "your email has been sent.\n";

    And this prints on the same web page as my form.

    Is there a way to end a perl script without printing anything at the end? If not, can I put that print in some sort of dialog box?
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    A print command is not required. You'd need to show more of your script for us to understand where the error is coming from.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by keath
    A print command is not required. You'd need to show more of your script for us to understand where the error is coming from.
    Her is the code I'm using:

    #!/usr/bin/perl -w

    use CGI;

    my(%frmFlds);
    $buffer = "";

    getFormData(\%frmFlds);

    #################################
    #
    # Change these variables as needed.
    #
    #################################

    $sendmail = "/usr/sbin/sendmail";
    $from = "learneasymoney\@gmail.com";
    $subject = "Thank you for your interest in $frmFlds{'product'}";

    open(MAIL, "|$sendmail -t") || die "Cannot open mail sender.\n";

    print MAIL "To: $frmFlds{'email'} \n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n";
    print MAIL "Content-Type: text/html; charset=ISO-8859-1\n\n $frmFlds{'content'}\n";

    close(MAIL);

    sub getFormData {
    my($hashRef) = shift;
    $buffer = "";

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    $buffer = $ENV{'QUERY_STRING'};
    }
    else {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    }

    foreach (split(/&/, $buffer)) {
    my($key, $value) = split(/=/, $_);
    $key = decodeURL($key);
    $value = decodeURL($value);
    %{$hashRef}->{$key} = $value;
    }
    }

    sub decodeURL {
    $_ = shift;
    tr/+/ /;
    s/%(..)/pack('c', hex($1))/eg;
    return($_);
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Is this being called as an AJAX request?

    First step is to add these 2 pragmas, which should be in every Perl script you write.
    Code:
    use strict;
    use warnings;
    Next, get rid of those 2 subroutines and instead use the methods provided by the CGI module to parse the submitted form data.

    For validating the email, I'd use the Email::Valid module.

    I prefer CGI's OO interface, so this how I'd begin the script.
    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    use CGI;
    use Email::Valid;
    
    my $cgi = CGI->new;
    my %form_data = $cgi->Vars;
    
    if ( Email::Valid->address($form_data{email}) ) {
        send_email();
    }
    else {
        # do something else
    }
    
    
    sub send_email {
        
        # send email using your preferred method
        # my preference would be to use one of the email modules on cpan
        # such as MIME::Lite
    }

IMN logo majestic logo threadwatch logo seochat tools logo