Mr Chris Posted November 16, 2006 Share Posted November 16, 2006 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]<?phpinclude("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 addressAnd 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?ThanksChris Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/ Share on other sites More sharing options...
Orio Posted November 16, 2006 Share Posted November 16, 2006 Try:[code]$messagebody .= "<a href=\"http://www.hfhfhhddhd.com/unsubscribe.php?email=".urlencode(trim($email))."\">http://www.hfhfhhddhd.com/unsubscribe.php?email=".urlencode(trim($email))."</a>";[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/#findComment-125529 Share on other sites More sharing options...
Mr Chris Posted November 16, 2006 Author Share Posted November 16, 2006 Thanks, but that does exactly the same thing. Just outputs:http://www.www.hfhfhhddhd.com/unsubscribe.php?email= not grabbing each email addressAny other ideas people?Thanks Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/#findComment-125554 Share on other sites More sharing options...
Orio Posted November 16, 2006 Share Posted November 16, 2006 I cant see you defining $email. Try replacing all of the $email with $row['email'].Orio. Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/#findComment-125557 Share on other sites More sharing options...
Mr Chris Posted November 16, 2006 Author Share Posted November 16, 2006 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]<?phpinclude("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?ThanksChris Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/#findComment-125568 Share on other sites More sharing options...
alpine Posted November 16, 2006 Share Posted November 16, 2006 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] Link to comment https://forums.phpfreaks.com/topic/27445-unsubscribe-using-mail-function/#findComment-125608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.