SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with perl script

    Hey guys, I am having problems. Long time HTML coder, which these days is like knowing COBOL, and am trying to get my form emailed back to me. I've tried lots, but never get the email. Can anyone help?


    #!/usr/bin/perl w
    use CGI::Carp qw(fatalsToBrowser);

    # The following accepts the data from the form and splits it into its component parts

    if ($ENV{'REQUEST_METHOD'} eq 'POST') {

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    @pairs = split(/&/, $buffer);

    foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
    }


    open(MAIL, "| $usr/sbin/sendmail -t");

    print MAIL "To: mail\@qtscreen.com\n"; # Don't forget to escape this @ symbol!
    print MAIL "From: $FORM{Customer}\n";
    print MAIL "Reply To: $FORM{E-Mail}\n";
    print MAIL "Phone: $FORM{Phone}\n";
    print MAIL "Subject: Web Submitted Order from $FORM{Contact} \n\n";
    print MAIL "Printing Information: $FORM{OrderInfo}\n";
    print MAIL "Print Location: $FORM{printloc}\n";
    print MAIL "Follow Up Info: $FORM{Followup}\n";

    close (MAIL);



    Any help would be greatly appreciated.

  2. #2
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing jumps out at me as non-working and you haven't said in what way it's failing to work other than that you don't receive the mail, so I don't know what will get it working, but there's a good chance that it may be on the mail service side - have you checked the sending machine's mail queue for undelivered messages, checked the sendmail log for errors, etc.?

    I would, however, strongly recommend that you not attempt to interface with CGI and email by hand. Those are easy things to get subtly wrong, so it's much better to use well-tested modules to handle those interactions for you.

    For the web side, I would normally suggest that you look at a web framework such as Dancer, Mojolicious, or Catalyst, but, in this case, I guess you could get away with using plain old CGI.pm, although that's very nasty and old and really not the best way to go if you ever find yourself wanting to do something more involved than receiving a single form and emailing it.

    On the email side, there are many, many options, but I'll recommend looking into MIME::Lite, mainly because it's what I like and use myself.

    Here's a quick (and untested!) example which uses CGI.pm and MIME::Lite to do what your posted code appears to intend:
    Code:
    #!/usr/bin/env perl    
    
    use strict;
    use warnings;
    
    use CGI qw(:standard);
    use CGI::Carp qw(fatalsToBrowser);
    use MIME::Lite;
    
    my $q = CGI->new;
    
    my $body = "Printing Information: " . $q->param('OrderInfo') . "\n";
    $body .= "Print Location: " . $q->param('printloc') . "\n";
    $body .= "Follow Up Info: " . $q->param('Followup') . "\n";
    $body .= "Phone: " . $q->param('Phone') . "\n";
    
    my $msg = MIME::Lite->new(
      From => $q->param('Customer'),
      To    => 'mail@qtscreen.com',   
      Reply-To => $q->param('E-Mail'),
      Subject => 'Web Submitted Order from ' . $q->param('Contact'),
      Data  => $body,
    );
    
    $msg->send;
    As I said, this code is untested, but it's something I've done enough times before that it should, at worst, be very close to correct.

    Note that I moved "Phone" into the message body, as it is not a standard SMTP header. Depending on server configurations, it's entirely possible that your problem may be that a server is rejecting the message due to the invalid "Phone" header.

    I would also definitely recommend that you standardize how the field names on your form are capitalized. It may not be a problem yet, but you will one day drive yourself crazy with errors caused by typing "PrintLoc" instead of "printloc".

  3. #3
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for taking the time to do this. I appreciate it. I will try each of these things, but most importantly the code you wrote. THANKS!

  4. #4
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    You rock, Dave!

    I wonder if we should link to that lightning talk, CGI.pm Must Die in this Perl section.

    Here's a video from the Israeli Perl Workshop http://www.youtube.com/watch?v=tu6_3fZbWYw (really starts at 1min.30) since I don't think the YAPC onces are online yet.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •