Jump to content

Include results of function in email message


k998

Recommended Posts

Hi all,

 

I am just starting out in the world of php and have got this far with a lot of googling.  But I'm really stuck with this part now.

 

I have a function that works perfectly for display on a page, eg <?php echo quickquote(); ?>

 

function quickquote() {
global $db;
global $cart;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$quickquote[] = 'Quote Required:<br />';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$quickquote[] = ''.$qty.'';
		$quickquote[] = ' x ';
		$quickquote[] = '' . $model . '';
		$quickquote[] = ' (' . $type . '- ';
		$quickquote[] = ''. $basin . '- ';
		$quickquote[] = ''. $top . ')';
		$quickquote[] = '<br />';
	}
	$quickquote[] = 'End of Quick Quote Request';
} else {
	$quickquote[] = 'The quote cart is empty.';
}
return join('',$quickquote);

}

 

However, i am trying to include this in an email message:

 

if(!$error)
{
$messages="From: $email <br>";
$messages.="Name: $name <br>";
$messages.="Email: $email <br>";
$messages.="Phone: $phone <br>";
$messages.="Message: $message <br>";
$messages.="Quick Quote Request: $quickquote <br>";

$mail = mail($to,$subject,$messages,$headers);	

 

The email also works perfectly with the exception that the data is not there from $quickquote.

 

I've tried all sorts of variations and suggested solutions from the web but nothing I've tried has been successful so far.

 

It's amazing that I've got this far so I don't want to give up on it, but I'm just completely stumped ...

 

All information and help very much appreciated.

 

Cheers

K

Link to comment
Share on other sites

$messages.="Quick Quote Request: " . quickquote() . "<br>";

 

I should add that you never actually called your function in your previous code, also, variables defined and used within functions do not exist outside of that function.

Link to comment
Share on other sites

Hi, thanks for your reply.

 

Isn't <?php echo quickquote(); ?> 'calling' a function?  It displays the output of the function ...

 

But anyway, yes the problem is that I get a 'Fatal error: Call to undefined function quickquote() in /home/xxx/public_html/xxx/quickquote/contact.php on line 110' error.

 

Line 110 is $messages.="Quick Quote Request: " . quickquote() . "<br>";

 

How do I define it?  Why does it 'understand' the function to echo the results but then it's undefined when using in the mail() function?

 

As you can see, I'm out of my depth with this but am desperate to get this resolved.  I've tried adding global $quickquote; in the mail() function.  Is that the correct way to define the function but I've just put it in the wrong spot?

Link to comment
Share on other sites

Isn't <?php echo quickquote(); ?> 'calling' a function?  It displays the output of the function ...

 

Indeed it is. But you don't have that code anywhere.

 

Is the function in the same file is you call to mail() ?

 

I've tried adding global $quickquote; in the mail() function.  Is that the correct way to define the function but I've just put it in the wrong spot?

 

Globals are bad, and calling a function is not the same as defining it. The top piece of code in your post defines the function. quicknote() calls it (which you need to put where I did in my reply..

Link to comment
Share on other sites

Success :D  :D  :D

 

Thank you so much for your replies.  I was ready to scream and this little snippet of wisdom:

 

Is the function in the same file is you call to mail() ?

 

made me sit back and look more methodically as to why I could call it from one page and not another.

 

Thanks again,

K

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.