我正在制作php脚本以发送新闻通讯,并为每位收件人取消订阅。
以下代码有效 - 向我在DB中的每个用户发送和发送电子邮件。但是它会为每个人添加取消订阅链接。因此,如果我有2个订阅者,他们都会获得相同的邮件正文和2个指向unsubcscribe的链接。
这是代码。
if (isset($_POST['sendemail'])) {
require_once "phpmailer/class.phpmailer.php";
require_once "phpmailer/class.smtp.php";
$message = "TEST MESSAGE";
$mail = new PHPMailer();
$body = eregi_replace("[\]",'',$message);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "myserver";
$mail->Port = 465;
$mail->Username = "myuser";
$mail->Password = "mypass";
$mail->SMTPDebug = false;
$mail->do_debug = 0;
$mail->SetFrom("me@domain.ltd", "Me");
$mail->AddReplyTo("me@domain.ltd", "Me");
$mail->AddAddress("some@email.com", "member");
// I GET LIST OF SUBSCRIBED EMAILS AND LOOP THE MAIL SENDING
$result = dbquery("SELECT * FROM emails WHERE email_sub='1'");
while($row = dbarray($result)) {
// HERE SHOULD BE ONIQUE UNSUBSCRIBE LINKO FOR EACH EMAIL
$body .= "<br /><a href=\"http://someurl.tld/emails.php?email=".$row["email_address"]."\">Unsubscribe</a>";
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->WordWrap = 50;
$mail->AddBCC($row["email_address"], $row["email_name"]);
$mail->addCustomHeader("List-Unsubscribe","<admin@keepity.com>, <http://keepity.com/?email=".md5(md5($row["email_address"])).">");
}
if(!$mail->Send()) {
echo "<i class=\"fa fa-warning\"></i> Error: " . $mail->ErrorInfo."<br />";
} else {
echo "Done";
}
}
所以电子邮件看起来像这样
Message body.....
Unsubscribe
Unsubscribe
因此,如果我有100个订阅者,每封电子邮件中将有100个链接..
答案 0 :(得分:1)
请试试这个
$body = eregi_replace("[\]",'',$message);
此代码应位于while循环内。 你现在正在做的是将取消订阅链接附加到$ body的当前值,每次循环继续添加新的取消订阅链接。
因此循环执行的次数,除了已经存在的内容之外,它还将继续添加一个取消订阅链接。
最终守则..
if (isset($_POST['sendemail'])) {
require_once "phpmailer/class.phpmailer.php";
require_once "phpmailer/class.smtp.php";
$message = "TEST MESSAGE";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "myserver";
$mail->Port = 465;
$mail->Username = "myuser";
$mail->Password = "mypass";
$mail->SMTPDebug = false;
$mail->do_debug = 0;
$mail->SetFrom("me@domain.ltd", "Me");
$mail->AddReplyTo("me@domain.ltd", "Me");
$mail->AddAddress("some@email.com", "member");
// I GET LIST OF SUBSCRIBED EMAILS AND LOOP THE MAIL SENDING
$result = dbquery("SELECT * FROM emails WHERE email_sub='1'");
while($row = dbarray($result)) {
$body = eregi_replace("[\]",'',$message);
// HERE SHOULD BE ONIQUE UNSUBSCRIBE LINKO FOR EACH EMAIL
$body .= "<br /><a href=\"http://someurl.tld/emails.php?email=".$row["email_address"]."\">Unsubscribe</a>";
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->WordWrap = 50;
$mail->AddBCC($row["email_address"], $row["email_name"]);
$mail->addCustomHeader("List-Unsubscribe","<admin@keepity.com>, <http://keepity.com/?email=".md5(md5($row["email_address"])).">");
}
if(!$mail->Send()) {
echo "<i class=\"fa fa-warning\"></i> Error: " . $mail->ErrorInfo."<br />";
} else {
echo "Done";
}
}
答案 1 :(得分:0)
您要发送两个链接:
这是一个如何调用它的示例:
$mail->addCustomHeader("List-Unsubscribe",'<admin@keepity.com>, <http://keepity.com/?email='.$address.'>');