mail() help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skm376
Forum Newbie
Posts: 4
Joined: Thu Apr 13, 2006 11:57 am

mail() help

Post by skm376 »

Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to send an email that displays updates to a table in my database.  Basically, I am trying to cycle through the table in the body of my email.  I am not sure if I am doing this right or if there is even a way to do it.  Here is the code:

Code: Select all

$body = 'Peel, Inc.' . "\n"
	.'Weekly ShopDani Inventory Updates' . "\n"
	.date('jS F Y') . "\n\n\n"
	."\t\t". 'Jewelry Name' . "\t" . 'Old Amt.' . "\t" . 'New Amt.' . "\n"
	while ($row = mysql_fetch_array($result)) {
		$row = mysql_fetch_array($result);
		$name = $row[jewelryName];
		$oldAmt = $row[oldAmount];
		$newAmt = $row[newAmount];
		echo "\t\t $name \t $oldAmt \t $newAmt \n";
	}
Is this right? How can I cycle through and display the table in the body of the email? The error I am currently getting is:

Parse error: parse error, unexpected T_WHILE

Thanks!


Jcart | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

you might want to put the [php tags around the code so that people will understand the code.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you are missing a semicolon at the end of line 4

also, is it best that you quote your array indices.. as in'

Code: Select all

$name = $row['jewelryName'];
$oldAmt = $row['oldAmount'];
$newAmt = $row['newAmount'];
skm376
Forum Newbie
Posts: 4
Joined: Thu Apr 13, 2006 11:57 am

Post by skm376 »

I added in the semicolon, but that just outputs the info. I want in the email to the webpage, is there anyway to get that info in the email body??
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

skm376 wrote:I added in the semicolon, but that just outputs the info. I want in the email to the webpage, is there anyway to get that info in the email body??
im not really sure how to do this code wise,but you would first have to make an array with all the values and then use foreach and make it add a new line in the formatting you want into the email. After you have the total list made,you would call the mail() function and add in the recipent and the body of the message into there.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

while ($row = mysql_fetch_array($result)) {
                $name = $row['jewelryName'];
                $oldAmt = $row['oldAmount'];
                $newAmt = $row['newAmount'];
                $body = "\t\t $name \t $oldAmt \t $newAmt \n";
                mail( .... );
        }
pass the $body variable to the mail() function. I would recommend using the phpMailer package instead since many filters are very picky and phpMailer solves many, many emailing problem.
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Re: mail() help

Post by tomprogers »

First, I would change that last line (line 18 here) to add the current row to the body-in-progress, instead of replacing it.
Secondly, to make this table appear in an email, I'd recommend using actual HTML table tags, like so:

Code: Select all

$report_date = date('jS F Y'); // I'm not sure how to simply insert this into a heredoc, so I'm defining ahead of time
$body =<<<END
<table>
<thead>
<tr><th colspan="3">Peel, Inc.</th></tr>
<tr><th colspan="3">Weekly ShopDani Inventory Updates for {$reportdate}</th></tr>
<tr><th>Jewelry Name</th>
	<th>Old Amt.</th>
	<th>New Amt.</th></tr>
</thead>
<tbody>
END;
while ($row = mysql_fetch_array($result)) {
	$row = mysql_fetch_array($result);
	$name = $row['jewelryName'];
	$oldAmt = $row['oldAmount'];
	$newAmt = $row['newAmount'];
	$body .= "<tr><td>$name</td><td>$oldAmt</td><td>$newAmt</td></tr>\n";
	}
$body .= '</tbody></table>';
I'm not a big fan of those huge pre-written email handlers - it's not that hard to build your own MIME-compliant email header by hand and pass it into mail() as a parameter. In either case, you will want to supply a plain-text version for that guy who lives in a cave and is still using Eudora 1.0.
Post Reply