如何发送电子邮件到从其他数据库中获取数据库的电子邮件列表
问题描述:
我一直有问题使用phpmailer发送批量电子邮件,虽然没有一个电子邮件的问题。如何发送电子邮件到从其他数据库中获取数据库的电子邮件列表
这里是我的代码:
$result = mysql_query("select * from $to",$conn) or die("list
selected ($to) does not exist ".mysql_error());
while ($row = mysql_fetch_array($result))
{
$email[] = $row['email'];
$student[] = $row['name'];
}
foreach ($email as $val => $uemail) {
$email = $uemail;
$students= $student[$val] ;
require("class.phpmailer.php");
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('[email protected]', 'My Name');
$mail->AddAddress("$email", "$student");
$mail->SetFrom('[email protected]', 'MyName');
$mail->AddReplyTo('[email protected]', 'My nameg');
$mail->Subject = "$sub";
$mail->MsgHTML("Dear $student<br> $msg <br>
<img src=\"$path\"> <p>
$host_upper
______________________________________________________
THIS IS AN AUTOMATED RESPONSE.
***DO NOT RESPOND TO THIS EMAIL****
");
$mail->AddAttachment("$path2"); // attachment
$mail->Send();
echo "Message Sent OK to $email </p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
任何帮助,建议将不胜感激。
答
分配$student[$val]
到$students
在foreach内,但随后分配 阵列$student
您PHPMailer的对象:
$mail->AddAddress("$email", "$student");
不该它是
$mail->AddAddress("$email", "$students");
除此之外,为每个必须发送的电子邮件实例化一个新的邮件对象是一个不好的实践,您应该在动态变量(如AddAddress
)上注册并保留所有其他外部以避免重载,并记住清除将会发生的变量如AddAddress
,像这样:
require_once("class.phpmailer.php");
$result = mysql_query("select * from $to",$conn) or die("list selected ($to) does not exist ".mysql_error());
while ($row = mysql_fetch_array($result))
{
$email[] = $row['email'];
$student[] = $row['name'];
}
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo('[email protected]', 'My Name');
$mail->SetFrom('[email protected]', 'MyName');
$mail->AddReplyTo('[email protected]', 'My nameg');
$mail->Subject = "$sub";
$mail->AddAttachment("$path2");
foreach ($email as $val => $uemail) {
$email = $uemail;
$students= $student[$val] ;
$mail->AddAddress("$email", "$students");
$mail->MsgHTML("Dear $student<br> $msg <br>
<img src=\"$path\"> <p>
$host_upper
______________________________________________________
THIS IS AN AUTOMATED RESPONSE.
***DO NOT RESPOND TO THIS EMAIL****
");
$mail->Send();
$mail->ClearAddresses();
echo "Message Sent OK to $email </p>\n";
}
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
答
您可以发送邮件while循环中:
<?php
require_once("class.phpmailer.php");
$result = mysql_query('SELECT `email`, `student`, `data1` FROM `students` ORDER BY `email` ASC;');
while ($row = mysql_fetch_assoc($result))
{
$current_mail = new PHPMailer(true);
$current_mail->AddReplyTo('[email protected]', 'My Name');
// ....
$current_mail->Send();
unset($current_mail);
}
?>
+0
谢谢我不够明智。现在完美 – kplus
谢谢。完善 – kplus