使用Perl将SQL查询结果作为发送电子邮件的参数传递
我想从表中检索电子邮件地址,并使用该地址使用perl脚本发送电子邮件 如何在邮件中使用查询结果。 我是perl脚本新手,请帮助 我已经按照建议进行了更新,但仍然存在一些问题。 请告诉我哪里出了问题 提前谢谢使用Perl将SQL查询结果作为发送电子邮件的参数传递,sql,perl,email,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->c
#!/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;
此脚本有太多问题,无法具体解决问题: 1严格使用;使用警告;。这将帮助你更准确 2 DBI->connect将选项作为最后一个参数,因此您可以在此处设置RaiseError:
my $dbh = DBI->connect($dsn, $user, $pwd, { RaiseError => 1 });
3$dbh->do不返回sth对象。您需要准备并执行:
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例如,要发送邮件,请使用模块。想想有多少像这样的旧代码仍在使用,真是可怕 你没有说你的问题是什么。仅仅说代码有一些问题并不能真正帮助我们帮助您 一些建议 使用严格的警告和警告 将RaiseError作为参数传递给connect调用 打印出@row的内容,以便确保SQL是正确的 用于创建和发送电子邮件
不太重要的考虑使用与数据库
交谈谢谢,但是邮件问题是使用sql查询结果发送邮件
#!/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;