Jump to content

unsubscribe using mail() function


Mr Chris

Recommended Posts

Hi Guys,

Have stumbled across a problem with the mail() function to unsubscribe.

Basically the below sends the headlines to email users in the email table in a MYSQL database, which works fine.

[code=php:0]
<?php

include("THE INCLUDE FILE TO CONNECT TO DB");

$messagebody = "<HTML>
<body bgcolor='#eeeeee'>
<IMG SRC='http://www.hfhfhhddhd.com/images/banner.jpg'>
<br />
<font face='Trebuchet MS' color='#666666'><b>Welcome to the <font color='#0000CC'>Website</font> Email Alerts listing.  Please find the latest stories from our website below</b></font>
<br />
</BODY>
</HTML>";


$SQL = "SELECT id, headline FROM news_stories ORDER BY id DESC LIMIT 3";
$result = mysql_query($SQL)  OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {
   $messagebody .= "<br /><b><font face='Trebuchet MS' size='4' color='#000000'>".stripslashes($row[headline])."</font></b>";
   $messagebody .= "<br /><i><font face='Trebuchet MS' size='4' color='#000099'><a href='http://www.hfhfhhddhd.co.uk/gtele/stories/story.php?id={$row[id]}'>Click Here to view the Story Online</a></font></i><br />";
}

$SQL = "SELECT email FROM emails";
$result = mysql_query($SQL)  OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {
  mail($row['email'], "Your Email Alert", $messagebody, "From: [email protected]\r\nContent-type: text/html");
  $messagebody .= "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=".$email."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=".$email."</a>";
}

?>
[/code]

However, the unsubscribe link I want to place on the bottom of the email:

[code=php:0]
  $messagebody .= "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=".$email."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=".$email."</a>";
[/code]

Does not pick up the email address for each user in the MYSQL database and simply appears on the bottom of the email as:

[b]http://www.www.hfhfhhddhd.com/unsubscribe.php?email=[/b]     //ie not grabbing each email address

And this link, ONLY appears on the bottom of the email for the first entry sent out ie email id=1?  Not for the other entries in the DB.

Any ideas?

Thanks

Chris
Link to comment
https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/
Share on other sites

Thanks – good call.

It’s now outputting email addresses, but I still have the problem with the loop on the second query I run:

On the first email it sees as a record in the database it adds and sends out the unsubscribe link fine:

http://www.hfhfhhddhd.com/[email protected]

But then on the second email it sees as a record in the unsubscribe link it sends out and adds:

http://www.hfhfhhddhd.com/[email protected] http://www.hfhfhhddhd.com/[email protected]

(ie adds the first unsubscribe URL then the second unsubscribe link)

And this continues as a re-occurring theme for all records.

Now I’ve tried taking

[code=php:0]  $messagebody .= "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."</a>"; 
[/code]

Out of the loop, but then it does not see any email addresses and I’ve also set the second query to limit=1, but that just sends to only the first record in the db.

[code=php:0]

<?php

include("CONNECT TO  DB");

$messagebody = "<HTML>
<body bgcolor='#eeeeee'>
<IMG SRC='http://www.thisisslough.com/images/banner.jpg'>
<br />
<font face='Trebuchet MS' color='#666666'><b>Welcome to the <font color='#0000CC'>Website</font> Email Alerts listing.  Please find the latest stories from our website below</b></font>
<br />
</BODY>
</HTML>";


$SQL = "SELECT id, headline FROM news_stories ORDER BY id DESC LIMIT 3";
$result = mysql_query($SQL)  OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {
  $messagebody .= "<br /><b><font face='Trebuchet MS' size='4' color='#000000'>".stripslashes($row[headline])."</font></b>";
  $messagebody .= "<br /><i><font face='Trebuchet MS' size='4' color='#000099'><a href='http://www.hfhfhhddhd.com/stories/story.php?id={$row[id]}'>Click Here to view the Story Online</a></font></i><br />";
}

$SQL = "SELECT your_email FROM emails";
$result = mysql_query($SQL)  OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {
  mail($row['your_email'], "Your Email Alert", $messagebody, "From: [email protected]\r\nContent-type: text/html");
  $messagebody .= "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."</a>"; 
}

?>
[/code]

Any ideas please?

Thanks

Chris
Your link mecomes a part of the messagebody building itself up while you loop through emails ($var[color=red] .[/color]= ), assign the link as it's own single variable and redefine it within each loop.

Try:
[code]
<?php


$SQL = "SELECT your_email FROM emails";
$result = mysql_query($SQL) OR die(mysql_error());
while ($row = mysql_fetch_array($result)) {

$messagelink = "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=". $row['your_email'] ."</a>";

$message = $messagebody.$messagelink;

mail($row['your_email'], "Your Email Alert", $message, "From: [email protected]\r\nContent-type: text/html");

}

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.