[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-02
8
Medium Priority
?
128 Views
Last Modified: 2014-08-07
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?
0
Comment
Question by:brucegust
  • 4
  • 2
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40236152
            $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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40236206
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
 

Author Comment

by:brucegust
ID: 40236216
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40236231
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
 

Accepted Solution

by:
brucegust earned 0 total points
ID: 40236504
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
 

Author Comment

by:brucegust
ID: 40236712
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
 

Author Closing Comment

by:brucegust
ID: 40245619
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question