我有一个数据库。字段为id
,email
和city
。我想向我数据库中的所有电子邮件发送一些电子邮件。
例如:我居住在一个城市,所以我想向所有与我的城市匹配的电子邮件发送电子邮件。
我已经编写了这样的方法代码:
$kota = mysql_real_escape_string(stripcslashes( $_POST['kota']));
$baca_kota = mysql_query("SELECT email FROM conselor WHERE kota='$kota'");
while($read = mysql_fetch_array($baca_kota))
{
$emailcc = $read['email'];
}
require '../phpmailer/PHPMailerAutoload.php';
require '../phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxxxxxxxxxxxxxxxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom($email,$name);
$mail->addAddress('stevanlai04@gmail.com'); // Add a recipient
$mail->addReplyTo($email);
$mail->addCC($emailcc);
答案 0 :(得分:0)
您需要从
中的PHPmailer sdk调用Send()方法while
$this->mail->IsSMTP();
$this->mail->SMTPAuth = true;
$this->mail->Host = 'mail.example.com';
//$this->mail->SMTPSecure = 'ssl';
$this->mail->Port =25;
$this->mail->Username = 'mail@example.com';
$this->mail->Password = 'your password';
foreach($listemails as $key=>val){
$this->mail->SetFrom($val["email address"], 'topic');
$this->mail->Send();
}
编辑:你也可以创建一个功能发送电子邮件,它返回自己!例如
function sendemail($emailaddres){
// call your phpmailler medhot
/ and return it
$this->mail->Send()
return sendemail($val["email address"])
答案 1 :(得分:0)
在循环中执行$mail->addCC($emailcc);
:
while ($read = mysql_fetch_array($baca_kota)) {
$emailcc = $read['email'];
$mail->addCC($emailcc);
}