我有两个MySQL表,一个有订阅者详细信息,另一个跟踪发送给他们的电子邮件,哪个广告系列,哪个版本等(分别是crawler和sent_mail)。我建了一个脚本可以通过爬虫表并基于SendGrid API,Web表单和一些javascript发送电子邮件。但我想弄清楚如何告诉脚本从抓取工作表中提取没有sent_mail表中列出的当前广告系列的电子邮件。我知道我必须使用连接,但我不确定如何在查询中构建逻辑。思考?我的尝试如下:
foreach ('SELECT crawler.email FROM crawler LEFT JOIN sent_mail ON crawler.email = sent_mail.email WHERE sent_mail.campaigns <> "$campaign"') as $address) as $address) {
$email = new SendGrid\Email();
$email
->addTo($address)
->setFrom($address)
->setSubject($title)
->setText($text)
->setHtml($text)
;
$sendgrid->send($email);
$db-> $query = "
INSERT INTO sent_mail (email, campaigns, campaign_version)
VALUES ($address, $campaign, $campaign_version)"; echo 'great success';
}
答案 0 :(得分:0)
我认为您正在寻找以下代码.. 我认为以下代码可能会有所帮助
$get_records = mysql_query('SELECT crawler.email as `email_val` FROM crawler LEFT JOIN sent_mail ON crawler.email = sent_mail.email WHERE sent_mail.campaigns <> "$campaign"');
$i = 1;
while($data = mysql_fetch_assoc($get_records)){
$to = $data['email_val'];
$subject = 'Your Subject';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: Your Name <you@you.com>' . "\r\n";
$sendmail = mail($to,$subject,$message,$headers);
if($sendmail){
$insert = mysql_query("INSERT INTO sent_mail (email, campaigns, campaign_version)
VALUES ('your value', 'your value', 'your value')");
if($insert){
echo $i.'Value inserted & Mail sent to'.$data['email'];
}else{
echo $i.'Value not inserted but Mail sent to'.$data['email'];
}
}else{
echo $i.'Mail sending fail to'.$data['email'];
}
$i++;
}
注意:在运行上述代码之前,请确保已建立数据库连接。