Jump to content

[SOLVED] HTML formatted mail from PHP+MySQL


Senuf

Recommended Posts

Hello, y'all.

Greetings from Buenos Aires (far down south, for most of you).

Being more of a designer than a programmer, I sometimes need some help from a friend around here, but he's on a one month trip, pure nature, no laptop, no mail, really nature-freak, if you don't mind my exaggeration. Hence, here I am, humbly soliciting a bit of help from you, people far from where I am, people I don't know but who might lend a helping hand.

 

Now, to the point:

 

On a website I designed and coded, I embedded a shopping cart, a very well known one. You can read about it in http://v3.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart.

I embellished it, styled it better, etc. It works really good (and I'm proud of my work), but there's something the code provided does not explain: I need to send the cart's content by mail. I've already done some mail sending via PHP with from data, etc., on many sites, but this is different.

Here is what I have in my PHP document, which is needed to be sent by HTML-formatted e-mail:

<?php 
ini_set("session.gc_maxlifetime","28800");

session_start();

// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');

////////// Mete campos de form /////////////////
$Name=$_POST['Name'];
$Phone=$_POST['Phone'];
$Email=$_POST['Email'];
$Province=$_POST['Province'];
$Comment=$_POST['Comment'];


///////// Here comes what I have to send by HTML formatted mail: 

/*function showCart() {*/
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<table><tr><td>';
	foreach ($contents as $Id=>$qty) {
		$sql = 'SELECT * FROM Productox WHERE Id = '.$Id.'';
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<table class="tablitacadaprod" cellspacing="0" height="64"><tr height="64">';
		$output[] = '<td height="64"><img src="../fotos_ch/'.$Cod.'.jpg" width="80" height="60" class="imgcadaprod"></td>';
		$output[] = '<td height="64" width="4"><img src="http://www.thecompany.com/images/spacer.gif" width="4" height="60"></td>';
		$output[] = '<td height="64"><input type="text" name="qty'.$Id.'" value="'.$qty.'" size="2" maxlength="3" class="qtycadaprod" /></td>';
		$total += $Precio * $qty;
		$output[] = '</tr></table>';
	}
	$output[] = '</td></tr></table>';
	$output[] = '<h1>Total: <strong>$'.$total.'</strong></h1>';

}
return join('',$output);



EOF; 

////////// Mail already sent, let's go and thank the user /////////////////
$loc="Location:thanks.php?Nombre=$Name";
header($loc);
?>

 

You see that up there, at the beginning, I have

// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');

 

Just in case those are needed, I'll attach those files (slightly modified, not including real user and password).

 

Thanks a lot in advance, and please pardon my english. It's not my native tongue.

 

 

Senuf

 

[attachment deleted by admin]

Not sure I understand the question. So I'm going to go based on the thread title. You need to send an HTML formatted e-mail. I'm going to guess that $output is the contents of said e-mail?

 

$header = "From: [email protected]\r\n"; // who sent mail
$headers .= "MIME-Version: 1.0\r\n"; // html support
$headers .= "Content-Type: text/html; charset=us-ascii\r\n"; // html support

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

 

$to is the e-mail address to send to.

$subject is the subject of the email.

$message is the main body of the e-mail ($output?).

$headers contains e-mail headers, I provided the most important ones you'll need.

Not sure I understand the question. So I'm going to go based on the thread title. You need to send an HTML formatted e-mail. I'm going to guess that $output is the contents of said e-mail?

 

$header = "From: [email protected]\r\n"; // who sent mail
$headers .= "MIME-Version: 1.0\r\n"; // html support
$headers .= "Content-Type: text/html; charset=us-ascii\r\n"; // html support

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

 

$to is the e-mail address to send to.

$subject is the subject of the email.

$message is the main body of the e-mail ($output?).

$headers contains e-mail headers, I provided the most important ones you'll need.

 

Hi, and thanks for your prompt answer.

 

Yes, that's the way I would send a regular HTML formatted mail, but in the middle of the mail I have regular HTML formatted code, PHP and calling for MySQL (queries), as well as data from a user session, and somehow (so to speak) this goes a bit beyond my knowledge. It seems I can't call variables nor PHP functions inside the HTML coded message I want to send.

 

Thanks again.

Oh, I see.

 

$output is only PART of what I should send. It's part of the cart's code, but what I have to send by HTML formatted email is what's below:

 

///////// Here comes what I have to send by HTML formatted mail: 

/*function showCart() {*/
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<table><tr><td>';
	foreach ($contents as $Id=>$qty) {
		$sql = 'SELECT * FROM Productox WHERE Id = '.$Id.'';
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<table class="tablitacadaprod" cellspacing="0" height="64"><tr height="64">';
		$output[] = '<td height="64"><img src="../fotos_ch/'.$Cod.'.jpg" width="80" height="60" class="imgcadaprod"></td>';
		$output[] = '<td height="64" width="4"><img src="http://www.thecompany.com/images/spacer.gif" width="4" height="60"></td>';
		$output[] = '<td height="64"><input type="text" name="qty'.$Id.'" value="'.$qty.'" size="2" maxlength="3" class="qtycadaprod" /></td>';
		$total += $Precio * $qty;
		$output[] = '</tr></table>';
	}
	$output[] = '</td></tr></table>';
	$output[] = '<h1>Total: <strong>$'.$total.'</strong></h1>';

}
return join('',$output);

 

As you see, there are parts that need to be "processed" by PHP inside what's to be sent.

 

Perhaps this should clarify it a bit:

There's a PHP document that shows the user the contents of his cart. From PHP, as we all know, the user is shown pure HTML. THAT HTML is what I need to be sent, properly formatted, in a mail message.

 

Thanks for your patience.

 

Senuf

I understand that, but you don't seem to understand your code/the concept of how PHP/HTML work in conjunction with each other. To start with you have commented out the function declaration, thus making the return statement obsolete.

 

If you were to replace...

 

return join('',$output);

 

...with...

 

$message = join('',$output);

 

...and use the code I gave you in my first post, it will e-mail the HTML source to the user.

I know I could be dumb, but not to that extent. Well, it proves what I know about me...

 

Thanks a lot, I'll try what you suggested and come back later with an answer. Right now I have to go and get my daughter from kindergarten.

 

Thanks again.

 

Hello Cags.

 

Here I am again.

 

It seems I was looking for the problem where there was NOT a problem and what indeed happened was what you saw (and I didn't).

 

Sometimes you (I, in this case) need some other's eyes to see what you cannot. It always happened to me with chess. When playing I ignore the obvious, but I see it when others play (hence, I'm a horrible player).

 

I cannot thank you enough.

 

A "thank you" as big as the distance from Buenos Aires, Argentina (here) to wherever you might be.

 

Senuf

Yes, I'll click on solved right now.

I see you're in Leicestershire. Glad to get help from there. I love english writers, actors, etc. I'm a big fan of Monty Python, A bit of Fry and Laurie (BTW, Fry's "Making History" is a great book, IMHO), Martin Amis, Ian McEwan, Douglas Adams, William Golding...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.