building a mail from the db

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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

building a mail from the db

Post by rsmarsha »

twigletmac | Using

Code: Select all

tags makes syntax errors easier to spot.[/color]


I need to build a mail with results from the database.

Code: Select all

$mesg = 'THIS IS AN ORDER FOR '.$fr['q'].' COMPUTER(S)'."\r\n"
                ."\r\n".'Ex VAT: '.$fr['exvat'].''."\r\n"
	."\r\n".'Price: '.$fr['price'].''."\r\n"
	."\r\n".'Bulk Discount: '.$fr['discount'].''."\r\n"
	."\r\n".'Total ex VAT: '.$fr['totalexvat'].''."\r\n"
	."\r\n".'Total Order Price: '.$fr['total'].''."\r\n"
	."\r\n".''."\n".'Order Number: '.$SR."\r\n"
	."\r\n".'Title: '.$fr['title']."\r\n"
	."\r\n".'Full Name: '.$fr['fullname']."\r\n"
	."\r\n".'Email: '.$fr['username']."\r\n"	
	."\r\n".'Payment Method: '.$fr['payment']."\r\n"
	."\r\n".'Home Tel: '.$fr['hometel']."\r\n"
	."\r\n".'Work Tel: '.$fr['worktel']."\r\n"
	."\r\n".'Mob Tel: '.$fr['mobtel']."\r\n"
	."\r\n".'Date: '.$today."\r\n"
	."\r\n".'Address 1: '.$fr['daddress1']."\r\n"
	.'Address 2: '.$fr['daddress2']."\r\n"
	.'Address 3: '.$fr['daddress3']."\r\n"
	.'Town/City: '.$fr['dtown']."\r\n"
	.'County: '.$fr['dcounty']."\r\n"
	.'Post Code: '.$fr['dpostcode']."\r\n"
	.'Delivery details: '.$fr['deldet']."\r\n"
$build_mail = "SELECT * FROM categories,orders_products WHERE caegories_category_id=orders_products.category_id AND orders_products.orderno='$SR'";
$bq = mysql_query($build_mail, $db_conn) or die("Query $build_mail Failed".mysql_error());
	while ($br = mysql_fetch_assoc($bq))
		{
	                ."\r\n".''.$br['category_name'].': '.$br['description'].''."\r\n"
		}
	mail($emailto, $subj, $mesg, $additional_headers)or die('mail not sent');
The above code won't work as it see's the query and the rest as errors. Any ideas?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Do you get any error messages?

Mac
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

good point :)

but if you dont get any error message... try to set display_error = on;
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

it sees an error at this line:

Code: Select all

$build_mail = "SELECT * FROM categories,orders_products WHERE caegories_category_id=orders_products.category_id AND
Parse error: parse error, unexpected T_VARIABLE in /homepages/40/d108546070/htdocs/test/sec_test/admin/v12_update.php on line 229
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

im guessign the error might be the line before...show all your code

and you have spelt caegories_category_id wrong
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

.'Delivery details: '.$fr['deldet']."\r\n"
Missing a semi colon ( ; ) on the end of that line.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

If i add the semi colon won't that end the building of the message and not allow the next to be added?

I've changed it to this:

Code: Select all

$mesg = 'THIS IS AN ORDER FOR '.$fr['q'].' COMPUTER(S)'."\r\n"
."\r\n".'Ex VAT: '.$fr['exvat'].''."\r\n"
	."\r\n".'Price: '.$fr['price'].''."\r\n"
	."\r\n".'Bulk Discount: '.$fr['discount'].''."\r\n"
	."\r\n".'Total ex VAT: '.$fr['totalexvat'].''."\r\n"
	."\r\n".'Total Order Price: '.$fr['total'].''."\r\n"
	."\r\n".''."\n".'Order Number: '.$SR."\r\n"
	."\r\n".'Title: '.$fr['title']."\r\n"
	."\r\n".'Full Name: '.$fr['fullname']."\r\n"
	."\r\n".'Email: '.$fr['username']."\r\n"	
	."\r\n".'Payment Method: '.$fr['payment']."\r\n"
	."\r\n".'Home Tel: '.$fr['hometel']."\r\n"
	."\r\n".'Work Tel: '.$fr['worktel']."\r\n"
	."\r\n".'Mob Tel: '.$fr['mobtel']."\r\n"
	."\r\n".'Date: '.$today."\r\n"
	."\r\n".'Address 1: '.$fr['daddress1']."\r\n"
	.'Address 2: '.$fr['daddress2']."\r\n"
	.'Address 3: '.$fr['daddress3']."\r\n"
	.'Town/City: '.$fr['dtown']."\r\n"
	.'County: '.$fr['dcounty']."\r\n"
	.'Post Code: '.$fr['dpostcode']."\r\n"
	.'Delivery details: '.$fr['deldet']."\r\n";
		$build_mail = "SELECT * FROM categories,orders_products WHERE categories.category_id=orders_products.category_id AND orders_products.orderno='$SR'";
		$bq = mysql_query($build_mail, $db_conn) or die("Query $build_mail Failed".mysql_error());
		while ($br = mysql_fetch_assoc($bq))
		{
	."\r\n".''.$br['category_name'].': '.$br['description'].''."\r\n";
		}
		mail($emailto, $subj, $mesg, $additional_headers)or die('mail not sent');
And i get this error:

Parse error: parse error, unexpected '.' in /homepages/40/d108546070/htdocs/test/sec_test/admin/v12_update.php on line 233


If i'm right, thats due to the ; i added ending the building of the mail, so it doesn't see the "." as adding another line to the mail creation.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The semi-colon you added is correct, the problem is this line:

Code: Select all

."\r\n".''.$br['category_name'].': '.$br['description'].''."\r\n";
You need to change it to:

Code: Select all

$mesg .= "\r\n".''.$br['category_name'].': '.$br['description'].''."\r\n";
You can't have all that other code (SQL queries, while loop etc.) whilst setting the $mesg variable.

Mac
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

I need to start the mail build earlier in the page as the other info has to come first. The other lines are appended onto the previous ones. If i change the loop within the query won't that redefine the $mesg variable meaning the previous set of info was lost to it?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

rsmarsha wrote:If i change the loop within the query won't that redefine the $mesg variable meaning the previous set of info was lost to it?
No - by putting a period in front of the equals sign it means that you are appending to the string. Give it a try.

Mac
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Ah cheers. :) Sorry was just me being a bit blind, hehe didn't even see it. :oops:
Post Reply