Email to Send Depending on Page id

Hello. I am trying to get a specific email function to work but can not. My site has several contact forms depending on the department you are trying to contact. So far it sends from the same controller php file with no differences. I want the email form to send to different people depending on the page the person sent the email form. So far the controller file calls the group ID it needs, but I need it to send it to certain emails depending on that id.

This is what I have so far that doesn’t work:

<?php
class ContactsController extends AppController {
		var $name = 'Contacts';

		
	 
		function thanks()
		{
		$this->layout = 'scgsah_default';
		}
		
		function ask($id)
		{
			$this->layout = 'scgsah_default';	
			$this->set('groupname', $this->requestAction('groups/getGroupName/'.$id));
			$this->set('groups', $this->requestAction('groups/getGroupList'));
		
			if (empty($this->data))
			{

			}
			else
			{
				$this->cleanUpFields();
				if ($this->Contact->save($this->data))
				{
					$this->Session->setFlash($this->data['Contact']['firstname'].' '.$this->data['Contact']['lastname'] . ', your message has been sent.');		
					$this->redirect('/contacts/thanks'); 


					//SIMPLE PHP EMAIL - Added by Nicole 10.14.08
					$to = "example@mail.com" if ($id !=19) {"anotheremail@example.com"};
					$subject = "Ask A Student Form";
					$message = 'Type of Question: '.$this->data['Contact']['groupname']."\
\
".'Email: '.$this->data['Contact']['email']."\
\
".'Name: '.$this->data['Contact']['firstname'].' '.$this->data['Contact']['lastname']."\
\
".'Address: '.$this->data['Contact']['address']."\
\
". 'City: '.$this->data['Contact']['city']."\
\
". 'State: '.$this->data['Contact']['state']."\
\
". 'Zip Code: '.$this->data['Contact']['zip']."\
\
". 'Phone: '.$this->data['Contact']['phone']."\
\
". 'Current School: '.$this->data['Contact']['organization']."\
\
". 'Subscribed to Newsletter: '.$this->data['Contact']['newsletter']."\
\
". 'Question: '.$this->data['Contact']['message'];
					$from = $this->data['Contact']['email'];
					$headers = "From: $from";mail($to,$subject,$message,$headers);echo "Mail Sent.";

				}	
				else
				{
					$this->Session->setFlash($this->data['Contact']['firstname'].', '.$this->data['Contact']['firstname'] . ' has not been added.');
					$this->redirect('/contacts/thanks');
				}
					
			}			
		}
		
		function subscribe()
		{
			$this->layout = 'scgsah_default';		
			if (empty($this->data))
			{

			}
			else
			{
				$this->cleanUpFields();
				if ($this->Contact->save($this->data))
				{
					$this->Session->setFlash($this->data['Contact']['firstname'].', '.$this->data['Contact']['lastname'] . '! Your message has been sent.');		
					$this->redirect('/contacts/thanks'); 
				}	
				else
				{
					$this->Session->setFlash($this->data['Contact']['firstname'].', '.$this->data['Contact']['firstname'] . ' has not been added.');
					$this->redirect('/contacts/thanks');
				}
					
			}			
		}		
}
?>

Any help would be appreciated.

$to = “example@mail.com” if ($id !=19) {“anotheremail@example.com”};

I’m not sure what this line is trying to achieve here but from what I read on your post you should be changing the $to address? If so and assuming from your code id for example@mail.com is 19 then:

$to should be set as:


if ($id ==19){
$to ="example@mail.com";
} else {
$to ="anotheremail@example.com";
}

If you wanted to send to both email addresses you would have to build up $to to include a comma between email addresses:


if ($id ==19){
$to ="example@mail.com";
} else {
$to ="example@mail.com , anotheremail@example.com";
}

Hope this helps.
Matt

web design macclesfield
branding and design macclesfield

It also seems like you’ll be using many email addresses? I’d store the emails in a table with a foreign key of the id.

Then the function should do a “in_array()” function (PHP: in_array - Manual) against that table to see if it should be sent to a specefic email, else send it to your default email.