Jump to content

[SOLVED] problem with foreach


AdRock

Recommended Posts

I have been working on an email newsletter where i get all email addresses from a database and replace a placeholder with the email address before sending.

 

It works with one email address but i put in a foreach loop which is supposed to get the email address and replace the placeholder with that email.  It is also supposed to replace the {name} placeholder with the name in the database.

 

This is the error

Warning: Invalid argument supplied for foreach()

 

If i try putting what's in the foreach loop in the while loop that gets the email address, it sends but a blank email and there's no subject or sender info.

<?php

require_once('php/database/MySQL.php');
require_once('php/database/connection.php');
require_once('php/init.php'); 

$db = & new MySQL($host,$dbUser,$dbPass,$dbName);

$sql="SELECT id, DATE_FORMAT(date, '%D %M %Y') as date, article1_title, article1, article2_title, article2, article3_title, article3, article4_title, article4, article5_title, article5 FROM newsletters WHERE id=3";

// Perform a query getting back a MySQLResult object
$result = $db->query($sql);

while ($row = $result->fetch()) {
	$data['date'] = $row['date'];
	$data['article1_title'] = $row['article1_title'];
	$data['article1'] = $row['article1'];
	$data['article2_title'] = $row['article2_title'];
	$data['article2'] = $row['article2'];
	$data['article3_title'] = $row['article3_title'];
	$data['article3'] = $row['article3'];
	$data['article4_title'] = $row['article4_title'];
	$data['article4'] = $row['article4'];
	$data['article5_title'] = $row['article5_title'];
	$data['article5'] = $row['article5'];
}

$placeholders = array("{date}", "{article1_title}", "{article1}", "{article2_title}", "{article2}", "{article3_title}", "{article3}", "{article4_title}", "{article4}", "{article5_title}", "{article5}");

$sql="SELECT mail.email, users.first_name, users.email FROM mail, users WHERE mail.email=users.email"; 

// Perform a query getting back a MySQLResult object
$result = $db->query($sql);

while ($row = $result->fetch()) {
	$emails = $row['email'];
	$name['name'] = $row['first_name'];
}

$nameholder = array("{name}");

$HTML = '
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Template 1 - Single Column</title>
</head>
<body>

<table width="100%" cellspacing="10" cellpadding="0">
   <tr>
	  <td align="center" valign="top">

		 <table width="580" border="0" cellspacing="0" cellpadding="0">
			<tr>
			   <td align="center" style="font-size:8pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">
				  <p>You\'re receiving this newsletter because you SIGNED UP AT <a href="http://wwww.operationbraveheart.org.uk">operationbraveheart.org.uk</a>.</p>
				  <p>Not interested anymore? <a href="#">Unsubscribe Instantly</a>.  Having trouble reading this email? <a href="#">View it in your browser</a>.</p>
				  <br>
			   </td>
			</tr>
			<tr>
				<td>
					<table width="100%" border="1" cellspacing="0" cellpadding="0">
						<tr>
							<td height="117" valign="bottom" align="left" class="header">
								<a href="http://www.operationbraveheart.org.uk/"><img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/banner.jpg" width="580" height="117" border=0"" alt="Operation Braveheart" title="Operation Braveheart" /></a>
							</td>
						</tr>
						<tr>
							<td align="center">
								<table width="550" cellspacing="0" cellpadding="0">
									<tr>
										<td align="right" style="padding: 3px;font-weight: bold; font-size:12pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;"><p>{date}</p></td>
									</tr>
									<tr>
										<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">
											<p>Hello {name}</p>
											<p>Welcome to the latest newsletter from Operation Braveheart</p>
										</td>
									</tr>
									<tr align="left" valign="top">
										<td width="550" valign="top" align="left" style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">	
											<table cellspacing="0" cellpadding="4">
												<tr>
													<td style="color: #ff891a;font-family: Arial, Verdana, Times, sans-serif;">
														<h2 style=" margin: 0; font-size: 12pt;">{article1_title}</h2>
													</td>
												</tr>
												<tr>
													<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">{article1}</td>
												</tr>
											</table>
											<br><br>
											<img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/hr-big.gif" width="550" height="27" />';
											if(!empty($data['article2'])) {
												$HTML.='
												<table cellspacing="0" cellpadding="4">
													<tr>
														<td style="color: #ff891a;font-family: Arial, Verdana, Times, sans-serif;">
															<h2 style=" margin: 0; font-size: 12pt;">{article2_title}</h2>
														</td>
													</tr>
													<tr>
														<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">{article2}</td>
													</tr>
												</table>
												<br><br>
												<img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/hr-big.gif" width="550" height="27" />';
											}
											if(!empty($data['article3'])) {
												$HTML.='
												<table cellspacing="0" cellpadding="4">
													<tr>
														<td style="color: #ff891a;font-family: Arial, Verdana, Times, sans-serif;">
															<h2 style=" margin: 0; font-size: 12pt;">{article3_title}</h2>
														</td>
													</tr>
													<tr>
														<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">{article3}</td>
													</tr>
												</table>
												<br><br>
												<img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/hr-big.gif" width="550" height="27" />';
											}
											if(!empty($data['article4'])) {
												$HTML.='
												<table cellspacing="0" cellpadding="4">
													<tr>
														<td style="color: #ff891a;font-family: Arial, Verdana, Times, sans-serif;">
															<h2 style=" margin: 0; font-size: 12pt;">{article4_title}</h2>
														</td>
													</tr>
													<tr>
														<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">{article4}</td>
													</tr>
												</table>
												<br><br>
												<img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/hr-big.gif" width="550" height="27" />';
											}
											if(!empty($data['article5'])) {
												$HTML.='
												<table cellspacing="0" cellpadding="4">
													<tr>
														<td style="color: #ff891a;font-family: Arial, Verdana, Times, sans-serif;">
															<h2 style=" margin: 0; font-size: 12pt;">{article5_title}</h2>
														</td>
													</tr>
													<tr>
														<td style="font-size:10pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">{article5}</td>
													</tr>
												</table>
												<br><br>
												<img src="http://www.operationbraveheart.org.uk/images/newsletter/layout/hr-big.gif" width="550" height="27" />';
											}

										$HTML.='
									</td>
								 </tr>
							  </table>
						   </td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
			   <td align="center" style="font-size:8pt; font-family: Arial, Verdana, Times, sans-serif; margin: 0;">
				  <p>Not interested anymore? <a href="#">Unsubscribe Instantly</a>.  Visit us at <a href="http://www.operationbraveheart.org.uk">www.operationbraveheart.org.uk</a></p>
				  <p>Operation Braveheart, 82 New Street, Cullompton, Devon, EX15 1HD</p>
			   </td>
			</tr>
		 </table>
	  </td>
   </tr>
</table>
</body>
</html>
';
$from         = "[email protected]";
$subject     = "I'm sending a test HTML email";

$HTML = str_replace($placeholders, $data, $HTML);

function sendHTMLemail($HTML,$from,$to,$subject)
{
// First we have to build our email headers
// Set out "from" address

	$headers = "From: $from\r\n"; 

// Now we specify our MIME version

	$headers .= "MIME-Version: 1.0\r\n"; 

// Create a boundary so we know where to look for
// the start of the data

	$boundary = uniqid("HTMLEMAIL"); 

// First we be nice and send a non-html version of our email

	$headers .= "Content-Type: multipart/alternative;".
				"boundary = $boundary\r\n\r\n"; 

	$headers .= "This is a MIME encoded message.\r\n\r\n"; 

	$headers .= "--$boundary\r\n".
				"Content-Type: text/plain; charset=ISO-8859-1\r\n".
				"Content-Transfer-Encoding: base64\r\n\r\n"; 

	$headers .= chunk_split(base64_encode(strip_tags($HTML))); 

// Now we attach the HTML version

	$headers .= "--$boundary\r\n".
				"Content-Type: text/html; charset=ISO-8859-1\r\n".
				"Content-Transfer-Encoding: base64\r\n\r\n"; 

	$headers .= chunk_split(base64_encode($HTML)); 

// And then send the email ....
}

foreach($emails as $email) {
	$to = $email;
	$HTML = str_replace($nameholder, $first_name, $HTML);

	sendHTMLemail($HTML,$from,$to,$subject);

	mail($to,$subject,"",$headers);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/149442-solved-problem-with-foreach/
Share on other sites

The only place I see you assigning anything to $emails is this while loop:

 

while ($row = $result->fetch()) {
      $emails = $row['email'];
      $name['name'] = $row['first_name'];
   }

 

and you aren't assigning an array to it or assigning it as an array or anything, so when it gets to your foreach loop, $emails is just a variable, not an array.  Perhaps you meant to do

 

$emails[] = $row['email'];

 

?

try changing this

foreach($emails as $email) {
	$to = $email;
	$HTML = str_replace($nameholder, $first_name, $HTML);

	sendHTMLemail($HTML,$from,$to,$subject);

	mail($to,$subject,"",$headers);
}

 

to this

foreach($emails as $key => $email) {
	$to = $email;
	$HTML = str_replace($nameholder, $first_name, $HTML);

	sendHTMLemail($HTML,$from,$to,$subject);

	mail($to,$subject,"",$headers);
}[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.