将SQL查询结果作为参数传递,使用Perl发送Email

时间:2012-02-03 10:15:49

标签: sql perl email

我想从表中检索电子邮件地址,并使用它来使用perl脚本发送电子邮件。

如何在邮件中使用查询结果。 我是perl脚本的新手,请帮助。

我已根据建议进行了更新,但仍存在一些问题。 请告诉我哪里出错了。

提前致谢。

#!/usr/bin/perl
# $Id: outofstockmail.pl,v 1.0 2012/03/01 21:35:24 isha Exp $

require '/usr/home/fnmugly/main.cfg';

use DBI;
my $dbh = DBI->connect($CFG{'mysql_dsn'},$CFG{'mysql_user'},
                       $CFG{'mysql_password'})
    or &PrintError("Could not connect to the MySQL Database.\nFile could not be made!\n");
$dbh->{RaiseError} = 1;        # save having to check each method call

print "<H1>Hello World</H1>\n";

$sql = "Select OS.name, OS.customer_email, OS.product, OS.salesperson,  
               OS.salesperson_email
        from   products AS P
        LEFT JOIN outofstock_sku AS OS ON OS.product = P.sku
        LEFT JOIN tech4less.inventory AS I ON (I.sku = P.sku AND I.status = 'A')
        WHERE mail_sent='0'
        GROUP BY OS.product";

#$sth = $dbh->do($sql);

my $sth1 = $dbh->prepare($sql);

$sth1->execute; 

while ( my @row = $sth1->fetchrow_array ) {   

    # email
    open MAIL, "| $mail_prog -t" || die "Could not connect to sendmail.";
    print MAIL "To: $row[1]"; 
    print MAIL "From: $row[4]";
    print MAIL "Reply-To:$row[4]";
    print MAIL "CC: $row[4]";  
    print MAIL "Subject: Product requested is back in inventory\n";
    print MAIL "\n";
    print MAIL "Hi $row[0] , The product $row[2] is available in the stock.\n";
    print MAIL "\n";
    close MAIL;

    $sql = "Update outofstock_sku SET mail_sent='0' WHERE mail_sent='1'";
    $sth2 = $dbh->do($sql);
}               

$sth = $dbh->do($sql);

$dbh->disconnect();

exit;

3 个答案:

答案 0 :(得分:2)

此脚本有太多问题无法解决问题:

1)使用use strict; use warnings;。它会帮助你更准确。

2)DBI->connect()将选项作为最后一个参数,因此您可以在那里设置RaiseError:

my $dbh = DBI->connect($dsn, $user, $pwd, { RaiseError => 1 });

3)$dbh->do不返回某个对象。您需要准备并执行:

my $sth = $dbh->prepare($sql);
$sth->execute;
while ( my @row = $sth->fetchrow_array ) {
    ...
    print MAIL "Hi $row[0]. We're happy to ... $row[1]...\n";
    ...
}

4)发送邮件使用模块,例如Email::Sender::Simple

答案 1 :(得分:0)

可怕的想到有多少像这样的旧代码仍在使用中。

你实际上并没有说出你的问题。只是说代码有“一些问题”并没有真正帮助我们帮助你。

一些建议......

  1. 使用strictwarnings

  2. RaiseError作为参数传递给connect来电

  3. 打印出@row的内容,以确保SQL正确

  4. 使用Email::Simple创建电子邮件,Email::Sender发送电子邮件

  5. (非常不重要)考虑使用DBIx::Class与数据库通信

答案 2 :(得分:0)

#!/usr/bin/perl 
require '/main.cfg';

# use DBI interface for MySQL
            use DBI;

            # connect to MySQL

            $dbh = DBI->connect($CFG{'mysql_dsn'},$CFG{'mysql_user'},$CFG{'mysql_password'}) or
                    &PrintError("Could not connect to the MySQL Database.\nFile could not be made!\n");
            $dbh->{RaiseError} = 1;        # save having to check each method call

            $mailprog = $CFG{'mail_prog'};

            $sql = "Select OS.name,OS.customer_email,OS.product,OS.salesperson_email from products AS P LEFT JOIN outofstock_sku AS OS ON OS.product = P.sku LEFT JOIN inventory AS I ON (I.sku = P.sku AND I.status = 'A') WHERE mail_sent='0' GROUP BY OS.product";

            $sth = $dbh->prepare($sql);

            $sth->execute();
                while($ref = $sth->fetchrow_hashref()) 
                        {

                            open MAIL, "| $mailprog -f sssss\@gmail.com -t" || die "Could not connect to sendmail.";
                            print "Content-Type: text/html\n\n";
                            print MAIL "To: $ref->{'customer_email'}\n";
                            print MAIL "From: \"\" <wwwwwwwwww\n>";
                            #   print MAIL "From: \"\" <$ref->{'salesperson_email'}\n>";
                            print MAIL "Reply-To: ";
                            #   print MAIL "CC: $ref->{'salesperson_email'}\n";
                            print MAIL "Subject:#$ref->{'product'}\n";
                            print MAIL "Hi $ref->{'name'},\nThe product $ref->{'product'} is available in the stock.\n";
                            close MAIL;

                                                        }


            $sth->finish();

            # Close MySQL connection
            $dbh->disconnect();
            exit;