Hack 93 Send Automatic Emails to High Bidders


Send payment instructions to your customers automatically.

Here's a simple script that will scan all your auctions that have ended in the last 24 hours and send a payment-instructions email to each high bidder.

This script requires the email template from [Hack #74]; just place it in the directory specified by $localdir in your config.pl include file.

require 'ebay.pl';

$template = "template.txt";

$today = &formatdate(time);
$yesterday = &formatdate(time - 864000);

my $page_number = 1;
while (1) {
    my $rsp = call_api({ Verb => 'GetSellerList',
                  DetailLevel => 8,
                       UserId => $user_id,
                  EndTimeFrom => $yesterday,
                    EndTimeTo => $today,
                   PageNumber => $page_number

    if ($rsp->{Errors}) {
      last PAGE;
    foreach (@{$rsp->{SellerList}{Item}}) {
      my %i = %$_;
      ($id, $title, $currency, $price, $highbidder, $checkout) =
             @i{qw/Id Title CurrencyId CurrentPrice HighBidder Checkout/};
      $bidderemail = $highbidder->{User}{Email};
      if ($bidderemail =~ "\@") {
        $shipping = $checkout->{Details}{ShippingHandlingCosts};
        $total = $price + $shipping;

        open(MAIL,"|/usr/sbin/sendmail -t");
        print MAIL "To: $bidderemail\n";
        print MAIL "From: $selleremail\n";
        print MAIL "Reply-To: $selleremail\n";
        print MAIL "Subject: $title\n\n";
        open (COVER, "$localdir/$template");
          while ( $line = <COVER> ) {
            if ($line eq "<insert title here>\n") { print MAIL $title; }
            elsif ($line eq "<insert item here>\n") { print MAIL $id; }
            elsif ($line eq "<insert shipping here>\n") {
                     print MAIL $currency . sprintf("%0.2f", $shipping); } 
            elsif ($line eq "<insert total here>\n") {
                     print MAIL $currency . sprintf("%0.2f", $total); }
            else {
              if ($line eq "\n") { $line = "$line\n"; }
                     else { chomp $line; }
              print MAIL $line;
    last PAGE unless $rsp->{SellerList}{HasMoreItems};

The amount charged for shipping is taken from the Checkout.Details.ShippingHandlingCosts field, which is suitable if you've specified fixed shipping costs. If you're using eBay's Calculated Shipping feature, as described in [Hack #45], then you'll need to use the GetShippingRates function. Simply pass it these fields (all children of Checkout.Details), and it will give you the same information your winning bidder sees:

ShipFromZipCode, ShipToZipCode, ShippingPackage,
WeightUnit, WeightMajor, and WeightMinor

Note also that this script will need to be modified in order to accommodate Dutch auctions. Use the GetHighBidders API call to retrieve multiple high bidders and the quantities they purchased for any single Dutch auction. Naturally, you'll need to supplement the shipping cost calculations to account for any per-item shipping expenses.

8.13.1 AuctionEndOfAuction Notifications

The script in this hack retrieves the list of completed auctions for the last 24 hours, just like most of the other scripts in this chapter that use GetSellerList. (Refer to [Hack #85] for details on scheduling and running the script at regular intervals.)

There is another approach, however. If you sign up to receive notifications, such as AuctionEndOfAuction, CheckoutBuyerRequestsTotal, AuctionCheckoutComplete, and FixedPriceEndOfTransaction, eBay's server will send the appropriate information to you as soon as the event is triggered, and you can then do your post-auction processing on an auction-by-auction basis.

Unfortunately, notifications are not available to developers with the free Individual license (discussed at the beginning of this chapter), but if you're operating under a Basic, Professional, or Enterprise license, you can sign up to receive notifications at developer.ebay.com. Look up "Getting Started with eBay Platform Notifications" in the API documentation for details.