Jump to content

Email class help


montyMagic

Recommended Posts

Hi everyone,

 

I can't pin down why an auto responder isn't being sent using an email class that has previously worked fine.

 

I've tested the mail() function and it's fine so I'm presuming it's not the server (though I have contacted the host to check on recent changes).

 

Here's an example of the ajax we're using to process everything...

		// EXPRESS INTEREST
			
	case "expressInterest":
		// Make sure the user is logged in
		if(!isCrewLoggedIn())
		{
			$return["message"] = "Your session has expired";
			send();				
		}
		
		// Make sure all required fields are present
		$required = array("event_id", "crew_id");
		
		if(missingFields($required, $missing_fields))
		{
			// Fields are missing
			$return["message"] = "The following fields are missing: " . arrayToErrorString($missing_fields);
			send();
		}
		
		// All fields are present and accounted for.
		//require_once("classes/producer.class.php");
		require_once("classes/event.class.php");
		require_once("classes/email.class.php");
		
		 //Load the event object for the current event_id
		$objEvent = new Event($conn);
		$event = $objEvent->get($_POST["event_id"]);
		
		$_POST["eventname"] = $event->eventname;
		$_POST["eventdate"] = $event->eventdate;
		$_POST["eventstate"] = $event->eventstate;
		$_POST["eventlocation"] = $event->eventlocation;
		
		// Add the application into the database
		$event_id = $objEvent->addApplication($_POST);
		if(!$event_id)
		{
			$return["message"] = "An error occured whilst expressing interest.";
			send();				
		}
		
		// Send the event_id back to the client.
		$return["event_id"] = $event_id;		
		
						
		// Send the autoresponder 
		$email_data = array();
		$email_data["firstname"] = $_SESSION["firstname"];
		$email_data["surname"] = $_SESSION["surname"];
		$email_data["email"] = $_SESSION["email"];
		$email_data["event_id"] = $_POST["event_id"];
		$email_data["eventname"] = $event->eventname;
		$email_data["eventdate"] = $event->eventdate;
		$email_data["eventstate"] = $event->eventstate;
		$email_data["eventlocation"] = $event->eventlocation;
		
		
		//send to Producer
		$objEmail = new Email($conn, ABSOLUTE_PATH . "email_templates/expressInterest_crew.xml", $email_data);
		$objEmail->sendEmail($email_data);
		
		//send to HQ too
		
		$objEmail = new Email($conn, ABSOLUTE_PATH . "email_templates/expressInterest_sc.xml", $email_data);
		$objEmail->sendEmail($email_data);	
		
		
		// Everything processed OK
		$return["status"] = "OK";
		
		break;

And here's the email class

<?php
class Email
{
	private $conn;			// Database connection
	private $template;		// The template file to use
	private $email_data;	// An associate array containing email data
	private $doc;			// An XML document object
	private $subject;		// The email subject
	private $from;			// Who the email is from
	private $recipients;	// An array containing all recipient email addresses
	private $message;		// The plain text or HTML message to send
	private $isHTML;		// Set to true if this should be send as a HTML email message
	
	public function __construct($conn, $template, $email_data)
	{
		// Class  constructor	
		// Set class variables
		$this->conn = $conn;
		$this->template = $template;
		$this->email_data = $email_data;
		
		$this->recipients = array();
		$this->isHTML = false;	// Default to plain text.
	}
	
	public function sendEmail($data)
	{
		// Load and parse the XML email template file
    	if(!$this->loadTemplate())
    	{
			return false;
    	}
    	
    	// Compose the EMAIL from header
    	$headers = "FROM: " . $this->from . "\r\nReply-To: " . $this->from;
    	
    	// Send the email to all recipients designated in the XML file.
    	foreach($this->recipients as $recipient)
    	{
			mail($recipient, $this->subject, $this->message, $headers);
    	}
    	
    	// All done.
    	return true;
	}
	
	private function replaceTemplateVars($xml)
	{
		if(!is_array($this->email_data))
		{
			return;
		}
		
		foreach($this->email_data as $key => $value)
		{
			$xml = str_ireplace("{" . $key . "}", $value, $xml);
		}
		
		return $xml;
	}
	
	private function loadTemplate()
	{
		if(($this->template == "") || (!file_exists($this->template)))
		{
			return false;
		}
		
		// Load the xml template
		$xml = file_get_contents($this->template);
		
		// Replace any placeholder variables that with values defined in
		// the email_data array.
		$xml = $this->replaceTemplateVars($xml);
		
		// Create a new XML document from the XML data.
		$this->doc = new DOMDocument();
		if(!$this->doc->loadXML($xml))
		{
			return false;
		}
		
		// Email Subject
		$node = $this->doc->getElementsByTagName("subject");
		if($node->length != 1) return false;
		$this->subject = trim($node->item(0)->textContent);
		
		// Email From
		$node = $this->doc->getElementsByTagName("from");
		if($node->length != 1) return false;
		$this->from = trim($node->item(0)->textContent);		
		
		// Email Message
		$node = $this->doc->getElementsByTagName("message");
		if($node->length != 1) return false;
		$this->message = trim($node->item(0)->textContent);		
		
		// Recipients
		$node = $this->doc->getElementsByTagName("recipient");
		if($node->length == 0) return false;		
		
		for($r = 0; $r < $node->length; $r++)
		{
			$this->recipients[] = trim($node->item($r)->textContent);
		}
		
		// All done - the template has been loaded and parsed.
		return true;
	}
}

Everything is working fine except for the autoresponder.  I have also tested by placing a basic mail() function directly after the db queries and it send perfectly.

 

I'm not great at php but I've got this far without probs and this has got me stumped.

 

If the complete files are needed, message me and I'll email.

 

Any help would be hugely appreciated.

 

Cheers

Link to comment
Share on other sites

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.