[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

Formatting output from shopping cart

I have a shopping cart on my company's site. It proccesses credit card data and sends the order via email to the person that takes care of that. The output is kinda sucky. How do I fix this? BTW the shopping cart is pretty complicated and I do not wnat to mess with it's code.

Typical email
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following data was entered at http://www.domain.com/cgi/checkout2.cgi:

 Address:    301 E Eleventyfive Street

 Address2:   301 E Eleventyfive Street

 Amount:     580.00

 City:       Wuzzleville

 City2:      Wuzzleville

 Company:    Fizzy Wizzles Whistles

 Company2:   Fizzy Wizzles Whistles

 DESCRIPTION:  Stamp Order

 Email:      wizzie@fizzies.com

 LOGIN:      techinc

 Name:       Weasal Fizzlebottom

 Name2:      Weasal Fizzlebottom

 PARTNER:    bigbank

 Phone:      620-123-4567

 State:      KS

 State2:     KS

 TAX+ID:     1111222333

 TYPE:       S

 Zip:        12345

 Zip2:       12345

 comments:   None

 o01Item:    HS²Hand Stamps²Stamp Size: HS26 1.75 in diameter +$19.50³Font          : Times Roman Bold³Font Size   : Fit to Size³Signature  : Yes  +$3.00³Art Work : Yes  +$5.00³Reduce/Enlarge Art: Yes  +$2.00³Text Line 1: This is a Stamp³Text Line 2: I use it to stamp things³Text Line 3: IN FACT³Text Line 4: I just used it to³Text Line 5: Stamped this!²r;$29.50²c;11²r;$324.50

 o02Item:    NSC²Round²Stamp Size: HS28 Hand Stamp +$19.50³Name       : Weasal Fizzlebottom²r;$19.50²c;11²r;$214.50

 s01Subtotal:  

 s02Salestax:  Sales Tax(KS)²r;$38.00

 s03Shipping:  Shipping(Out of town)²r;$3.00

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Script that sends email
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!/usr/local/bin/perl

# IMPORTANT: MAKE SURE THESE TWO VALUES ARE SET CORRECTLY FOR YOU!
$mailprog= "/usr/lib/sendmail" ;
$recipient= "user\@domain.com" ;      # make sure to \ escape the @

# Get the CGI input variables
%in= &getcgivars ;

# Open the mailing process
open(MAIL, "|$mailprog $recipient")
    || &HTMLdie("Couldn't send the mail (couldn't run $mailprog).") ;

# Print the header information
$ENV{'HTTP_REFERER'} || ($ENV{'HTTP_REFERER'}= "your Web site") ;
print MAIL "Subject: Form data from the Web\n\n",
           "The following data was entered at $ENV{'HTTP_REFERER'}:\n\n" ;


# Find length of longest field name, for formatting; include space for colon
$maxlength= 0 ;
foreach (keys %in) {
    $maxlength= length if length > $maxlength ;
}
$maxlength++ ;

# Print each CGI variable received by the script, one per line.
# This just prints the fields in alphabetical order.  To define your own
#   order, use something like
#     foreach ('firstname', 'lastname', 'phone', 'address1', ... ) {
foreach (sort keys %in) {

    # If a field has newlines, it's probably a block of text; indent it.
    if ($in{$_}=~ /\n/) {
        $in{$_}= "\n" . $in{$_} ;
        $in{$_}=~ s/\n/\n    /g ;
        $in{$_}.= "\n" ;
    }

    # comma-separate multiple selections
    $in{$_}=~ s/\0/, /g ;

    # Print fields, aligning columns neatly
    printf MAIL "%-${maxlength}s  %s\n", "/n $_:", $in{$_} ;
}


# Close the process and mail the data
close(MAIL) ;


# Print an HTML response to the user
print <<EOF ;
Content-type: text/html

<html>
<body>
<h3>Your stamp order has been sent.</h3>
</body>
</html>
EOF

exit ;


#-------------- start of &getcgivars() module, copied in -------------

# Read all CGI vars into an associative array.
# If multiple input fields have the same name, they are concatenated into
#   one array element and delimited with the \0 character (which fails if
#   the input has any \0 characters, very unlikely but conceivably possible).
# Currently only supports Content-Type of application/x-www-form-urlencoded.
sub getcgivars {
    local($in, %in) ;
    local($name, $value) ;


    # First, read entire string of CGI vars into $in
    if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
         ($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
        $in= $ENV{'QUERY_STRING'} ;

    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
            length($ENV{'CONTENT_LENGTH'})
                || &HTMLdie("No Content-Length sent with the POST request.") ;
            read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;

        } else {
            &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") ;
        }

    } else {
        &HTMLdie("Script was called with unsupported REQUEST_METHOD.") ;
    }
   
    # Resolve and unencode name/value pairs into %in
    foreach (split(/[&;]/, $in)) {
        s/\+/ /g ;
        ($name, $value)= split('=', $_, 2) ;
        $name=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
        $value=~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge ;
        $in{$name}.= "\0" if defined($in{$name}) ;  # concatenate multiple vars
        $in{$name}.= $value ;
    }

    return %in ;

}


# Die, outputting HTML error page
# If no $title, use a default title
sub HTMLdie {
    local($msg,$title)= @_ ;
    $title= "CGI Error" if $title eq '' ;
    print <<EOF ;
Content-type: text/html

<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<h3>$msg</h3>
</body>
</html>
EOF

    exit ;
}

#-------------- end of &getcgivars() module --------------------------

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
Bob Stone
Asked:
Bob Stone
  • 5
  • 5
1 Solution
 
ahoffmannCommented:
and what is the question now?
0
 
Bob StoneIT GuruAuthor Commented:
Mainly they are whining that they can't read this part very well. How would I clean it up?

o01Item:    HS²Hand Stamps²Stamp Size: HS26 1.75 in diameter +$19.50³Font          : Times Roman Bold³Font Size   : Fit to Size³Signature  : Yes  +$3.00³Art Work : Yes  +$5.00³Reduce/Enlarge Art: Yes  +$2.00³Text Line 1: This is a Stamp³Text Line 2: I use it to stamp things³Text Line 3: IN FACT³Text Line 4: I just used it to³Text Line 5: Stamped this!²r;$29.50²c;11²r;$324.50
0
 
ahoffmannCommented:
> How would I clean it up?
however you like it to look like.

Sorry for my sarcastic comment, but you need to give more information what you want to do.
I can read it, so there is no need for a change ;-)
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
Bob StoneIT GuruAuthor Commented:
So can I but we are talking about secretaries being able to read it.

I can't really change the variables names in the shopping cart app without doing a ton of work, just wondering if there is a way to maybe replace characters with a script, like change the ² thingy for a <br> or \n. I have no idea how to do that tho and can't find anything on web.
0
 
ahoffmannCommented:
ok, please save the data into a file and use (assuming Unix/Linux)
  od -x file
to identify that character, please post the hex value of the character
0
 
Bob StoneIT GuruAuthor Commented:
I am not sure how to use that command or where to put it in the mail script
0
 
ahoffmannCommented:
oops, this command was not meant to be used in your script
You need it once to identify what's the code of these characters.
0
 
Bob StoneIT GuruAuthor Commented:
 ²
Ent  Value =  &sup2;
Dec Value =  &#178;
Hex Value =  &#xB2;
0
 
ahoffmannCommented:
assuming your valu in question is in variable $item and you want to replace these chars by a <br> tag, then use something like:

$item=~s/\xb2/<br>/g;
0
 
Bob StoneIT GuruAuthor Commented:
<br> didn't work, but \n did =o)

        $value=~s/\xb2/\n/g;
        $value=~s/\xb3/\n/g;


Instead of -
 o01Item:    HS²Hand Stamps²Stamp Size: HS26 1.75 in diameter +$19.50³Font          : Times Roman Bold³Font Size   : Fit to Size³Signature  : Yes  +$3.00³Art Work : Yes  +$5.00³Reduce/Enlarge Art: Yes  +$2.00³Text Line 1: This is a Stamp³Text Line 2: I use it to stamp things³Text Line 3: IN FACT³Text Line 4: I just used it to³Text Line 5: Stamped this!²r;$29.50²c;11²r;$324.50

 o02Item:    NSC²Round²Stamp Size: HS28 Hand Stamp +$19.50³Name       : Weasal Fizzlebottom²r;$19.50²c;11²r;$214.50


I get -
 o01Item:    
    HS
    Hand Stamps
    Stamp Size: HS25 2 1/2 in x 3 1/2 in +$25.95
    Font          : Helvetica
    Font Size   : Fit to Size
    Signature  : No
    Art Work : No
    Reduce/Enlarge Art: No
    Text Line 1: My Stamp is big
    Text Line 2: I like to stamp things
    Text Line 3: BOOOM!
    Text Line 4: I just stamped this!
    Text Line 5: YAY! I are so happy!
    r;$25.95
    c;11
    r;$285.45


 o02Item:    
    SI
    Self Inking Stamps
    Stamp Size: SI50 1.06 in x 2.56 in +$24.50
    Font          : Helvetica
    Font Size   : Fit to Size
    Ink Color  : Black
    Signature  : Yes  +$3.00
    Art Work : Yes  +$5.00
    Reduce/Enlarge Art: No
    Text Line 1: This is another stamp
    Text Line 2: I like to stamp things
    Text Line 3: Lookie!
    Text Line 4: I just stamped it with
    Text Line 5: This stamp too!
    r;$32.50
    c;11
    r;$357.50
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now