How do I put a loop within a message in an email?

Normally, when I send an email out, I've got a one row's worth of info and it's a no brainer. But this morning, I've got a situation where I may have to document several rows of info and I'm stuck.

Here's what I've got thus far:

$to = $email;
            $subject = "Your Tae Kwon Do Password";
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers = "From: info@rodgerstkd.com\r\n" ."X-Mailer: php";
            $message = "Hello!\n\nYour TKD password info is:\n\n";
            $message .=
            while($queryrow=mysqli_fetch_assoc($result))
            {
            extract($queryrow);
            }
            $queryrow=mysqli_fetch_assoc($result);
            extract($queryrow);

I get to my $message and I'm thinking how do I embed in my $message several rows worth of info?

Thoughts?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
            $to = $email;
            $subject = "Your Tae Kwon Do Password";
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headers = "From: info@rodgerstkd.com\r\n" ."X-Mailer: php";
            $message = "Hello!\n\nYour TKD password info is:\n\n";                                                                             
            while($queryrow=mysqli_fetch_assoc($result))
            {
                $message .= extract($queryrow);
            }

Open in new window

0
Ray PaseurCommented:
Pretty sure extract() doesn't belong here (it returns an integer, and injects variables into the symbol table).  The query results set can be visualized with something along these lines.  Then you will know the names of the data elements and their contents.  We can help further if you can post the output showing the results set.

// MAKE FORMATING EASY TO READ
echo '<pre>';

// RETRIEVE AND PRINT THE RESULTS SET
while($row = mysqli_fetch_object($result))
{
    print_r($row);
}

Open in new window

0
brucegustPHP DeveloperAuthor Commented:
This worked:

$to = $email;
		$subject = "Your Tae Kwon Do Password";
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$headers = "From: info@rodgerstkd.com\r\n" ."X-Mailer: php";
		$message = "Hello!\n\nYour TKD password info is:\n\n";  
		$message .="<table><tr><td>name:</td><td>userid:</td><td>password</td></tr>";
			while($queryrow=mysqli_fetch_assoc($result))
			{
			$message .=extract($queryrow); 
			$message .= "<tr><td>";
			$message .=stripslashes($queryrow['first_name']). '|' .stripslashes($queryrow['last_name']);
			$message .="</td><td>";
			$message .=$queryrow['userid'];
			$message .="</td><td>";
			$message .=$queryrow['password'];
			$message .="</td></tr>";
			}
		$message .="</table>\n\nThanks!";
			 if (!mail($to, $subject, $message, $headers)) {
			 echo("<p>Message delivery failed...</p>");
			 }
		 }

Open in new window


Problem is, the email looked like this:

Hello!

Your TKD password info is:

<table><tr><td>name:</td><td>userid:</td><td>password</td></tr>39<tr><td>Bruce|Gust</td><td>287688</td><td>GustTKD1</td></tr>39<tr><td>Michelle|Gust</td><td>123456</td><td>Secret</td></tr>39<tr><td>Carter|Gust</td><td>123458</td><td>Secret</td></tr></table>

Thanks!

How can I makes sure my html is being recognized as such and not as text?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Marco GasiFreelancerCommented:
Headers seem be set correctly, so I don't have any idea. Perhaps Ray can help in this. Personally, when somone ask help about mail() function I suggest him to use PhpMailer: you have to play a bit to understand how to use it, but once you learnt, it works like a charm.
0
brucegustPHP DeveloperAuthor Commented:
Here's what I got to work...

if($rowCount>1)
		{
		$to = $email;
		$subject = "Rodgers TKD Student Password Info";

		$message = "
		<html>
		<head>
		<title>Rodgers TKD Student Password Info</title>
		</head>
		<body><br>
		Here the userids and passwords associated with the email address you just entered...<br><br>
		<table width=\"600\" border=\"1\">
		<tr>
			<td style=\"background-color:#cccccc;\">name:</td>
			<td style=\"background-color:#cccccc;\">userid:</td>
			<td style=\"background-color:#cccccc;\">password</td>
		</tr>";
		while($queryrow=mysqli_fetch_assoc($result))
		{
		extract($queryrow); 
		$message .="
		<tr>
			<td>";
			$message .= stripslashes($queryrow['first_name']).' '.stripslashes($queryrow['last_name']);
			$message .="</td><td>";
			$message .= $queryrow['userid'];
			$message .="</td><td>";
			$message .= $queryrow['password'];
			$message .="</td></tr>";
			}
			$message .="</table></body></html>";
	

		// Always set content-type when sending HTML email
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

		// More headers
		$headers .= 'From: <info@rodgerstkd.com>' . "\r\n";

		mail($to,$subject,$message,$headers);

		 }
		 else
		{

Open in new window


Smell that? That's the smell of success, baby!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
I'll accept my answer then. I just wanted to give Marco Gasi some credit since it was his answer that got me moving in the right direction.

As far as extract() not being necessary, not sure how else you would retrieve the info from the loop, but my biggest challenge was to maneuver things around so the php and the html were being properly perceived -  resulting in an email that displayed the correct info in the right formatting.

I appreciate your wisdom.
0
brucegustPHP DeveloperAuthor Commented:
Administrator was not agreeable to the first solution which I chose to receive the credit, since it didn't actually get the job done.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.