变量不工作在循环中

时间:2015-07-24 22:56:06

标签: php email mysqli

我正在使用以下代码发送简报。我有一个$to变量,用于我的数据库中的电子邮件地址。我使用while循环为我的数据库中的每个电子邮件地址发送一封电子邮件,以保护隐私。在电子邮件的底部,我有一个取消订阅的链接,该链接链接到一个简单的脚本,该脚本中的用户通过链接发送电子邮件。但$to变量在链接中不起作用。电子邮件会发送,但当我查看它是否发送了所有数据时,该链接看起来像http://example.com/scripts/php/unsubscribe.php?email=而不是http://example.com/scripts/php/unsubscribe.php?email=example@email.com

我不确定我在这里做错了什么,因为我没有收到任何错误,除了在链接中发送电子邮件之外,脚本正在工作。

require('/home/jollyrogerpcs/public_html/settings/globalVariables.php');
require('/home/jollyrogerpcs/public_html/settings/mysqli_connect.php');
mysqli_select_db($conn,"newsletterlist");
$query = "SELECT * FROM newsletterusers";
$result = mysqli_query($conn, $query);

$subject = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['subject']);
$message = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['body']);


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Jesse Elser<jesse@jollyrogerpcs.com>' . "\r\n";

if (!$result) exit("The query did not succeded");
else {
    while ($row = mysqli_fetch_array($result)) {
        $to = $row['email'];
        $date = date("m/d/Y h:i:sa");
        $body ='<!DOCTYPE HTML>';
        $body .='<body style="padding: 0; margin: 0; background-color: #000; color: #fff; text-align: center; font-family: verdana;">';
        $body .='<div id="container" style="width: 90%; margin: 0 auto; text-align: left; background-color: #121212;">';
        $body .='<div id="header" style="border-bottom: 1px solid #ff6400;">';
        $body .='<img src="http://jollyrogerpcs.com/images/main/logo.png" width="100%">';
        $body .='</div>';
        $body .='<div id="subject" style="background-color: #121212; text-align: center;">';
        $body .='<h1 style="color: #ff6400; margin: 0;">'.$subject.'</h1>';
        $body .='</div>';
        $body .='<div id="message" style="background-color: #232323; color: #fff; padding: 10px;">';
        $body .=  $message;
        $body .='</div>';
        $body .='<div id="footer" style="background-color: #121212; padding: 10px;">';
        $body .='<a href="http://jollyrogerpcs.com" style="text-decoration: none; color: #ff6400;">Visit Our Site</a> | Thanks for subscribing to our newsletter! | <a href="http://jollyrogerpcs.com/scripts/php/unsubscribe.php?email="'.$to.'" style="text-decoration: none; color: #ff6400;">Unsubscribe</a> <br> E-mail sent: ';
        $body .= $date;
        $body .='</div>';
        $body .='</body>';
        mail($to,$subject,$body,$headers);
    }
}
mysqli_close($conn);
header('Location: http://jollyrogerpcs.com/newsletter.php');

1 个答案:

答案 0 :(得分:1)

您要在包含电子邮件地址之前关闭href属性,以便......

<a href="http://example.com/scripts/php/unsubscribe.php?email="'.$to.'"

应该是

<a href="http://example.com/scripts/php/unsubscribe.php?email='.$to.'"

它将呈现为

<a href="http://example.com/scripts/php/unsubscribe.php?email=" email@address.com"....

哪个会建立链接http://example.com/scripts/php/unsubscribe.php?email=