0

I am sending a newsletter using the following code I am going to post. I have a $to variable that is for email addresses in my database. I use a while loop to send an email for each email address in my database to preserve privacy. At the bottom of the email I have a link for unsubscribing which is linked to a simple script that has the users email in the link. The $to variable is not working in the link though. The email sends but when I look to see if it sent all the data the link looks like http://example.com/scripts/php/unsubscribe.php?email= instead of http://example.com/scripts/php/[email protected].

I'm not sure what I've done wrong here since I am getting no errors, and the script is working except for sending the email in the link.

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<[email protected]>' . "\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');
3
  • $to not to...see it? /php/unsubscribe.php?email="'.to.'" Commented Jul 24, 2015 at 22:58
  • Dang it lol I noticed that earlier and updated my code to get the same results. Let me update my question. Commented Jul 24, 2015 at 23:00
  • Wrote it as an answer below take a look Commented Jul 24, 2015 at 23:05

1 Answer 1

1

You are closing the href attribute before the email address is included so...

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

Should be

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

As is it would render as

<a href="http://example.com/scripts/php/unsubscribe.php?email=" [email protected]"....

Which would make the link http://example.com/scripts/php/unsubscribe.php?email=.

Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it. I see what you meant now that it's visual :) Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.