Page 1 of 1

mail() help

Posted: Thu Apr 13, 2006 1:35 pm
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]

Posted: Thu Apr 13, 2006 1:40 pm
by a94060
you might want to put the [php tags around the code so that people will understand the code.

Posted: Thu Apr 13, 2006 1:40 pm
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'];

Posted: Thu Apr 13, 2006 1:45 pm
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??

Posted: Thu Apr 13, 2006 1:49 pm
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.

Posted: Thu Apr 13, 2006 1:59 pm
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.

Re: mail() help

Posted: Thu Apr 13, 2006 3:18 pm
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.