qakbar
qakbar

Reputation: 53

How to Change PHPMailer class to normal Mail

I am having problems sending emails using the PHPMailer class for my registration form. I was wondering if the below code could be changed into a normal Mail class please.

<?php

require_once('PHPMailer_v5.1/PHPMailer.php');

class Email {

	public $objUrl;
	private $objMailer;

	public function __construct($objUrl = null) {
	
		$this->objUrl = is_object($objUrl) ? $objUrl : new Url();
		
		$this->objMailer = new PHPMailer();
		$this->objMailer->IsSMTP();
		$this->objMailer->SMTPAuth = true;
		$this->objMailer->SMTPKeepAlive = true;
		$this->objMailer->SMTPSecure = 'ssl';
		$this->objMailer->Host = "smtp.gmail.com";
		$this->objMailer->Port = 465;
		$this->objMailer->Username = "email here";
		$this->objMailer->Password = "password here";
		$this->objMailer->SetFrom("email here", "name here");
		$this->objMailer->AddReplyTo("email here ", "name here");
		
		
		
	}

	public function process($case = null, $array = null) {
	
		if (!empty($case)) {
		
			switch($case) {
				
				case 1:
				
				// add url to the array
				$link  = "<a href=\"";
				$link .= SITE_URL.$this->objUrl->href('activate', array('code', $array['hash']));
				$link .= "\">";
				$link .= SITE_URL.$this->objUrl->href('activate', array('code', $array['hash']));
				$link .= "</a>";
				$array['link'] = $link;
				
				$this->objMailer->Subject = "Activate your account";
				
				$this->objMailer->MsgHTML($this->fetchEmail($case, $array));
				$this->objMailer->AddAddress(
					$array['email'], 
					$array['first_name'].' '.$array['last_name']
				);
				
				break;
				
			}
			
			
			// send email
			if ($this->objMailer->Send()) {
				$this->objMailer->ClearAddresses();
				return true;
			}
			return false;
			
		
		}
	
	
	}

	public function fetchEmail($case = null, $array = null) {
	
		if (!empty($case)) {
			
			if (!empty($array)) {			
				foreach($array as $key => $value) {
					${$key} = $value;
				}			
			}
			
			ob_start();
			require_once(EMAILS_PATH.DS.$case.".php");
			$out = ob_get_clean();
			return $this->wrapEmail($out);
		
		}
	
	}

	public function wrapEmail($content = null) {
		if (!empty($content)) {
			return "<div style=\"font-family:Arial,Verdana,Sans-serif;font-size:12px;color:#333;line-height:21px;\">{$content}</div>";
		}
	}
	



}

Any help is much appreciated

Thanks

I have tried to amend the code to the below code but all I get is a blank screen. I have checked on firefox and no error messages are displayed. Can anyone help fix the problem.

Thank you

<?php

//require_once('PHPMailer_v5.1/PHPMailer.php');

class Email {

	public $objUrl;
	//private $objMailer;

	public function __construct($objUrl = null) {
	
		$this->objUrl = is_object($objUrl) ? $objUrl : new Url();
		
		$to = "[email protected]";
		$headers = 'MIME-Version: 1.0'."\r\n";
		$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
		$headers .= "From: [email protected]";
		
		
		
	}

	public function process($case = null, $array = null) {
	
		if (!empty($case)) {
		
			switch($case) {
				
				case 1:
				
				// add url to the array
				$link  = "<a href=\"";
				$link .= SITE_URL.$this->objUrl->href('activate', array('code', $array['hash']));
				$link .= "\">";
				$link .= SITE_URL.$this->objUrl->href('activate', array('code', $array['hash']));
				$link .= "</a>";
				$array['link'] = $link;
				
				$subject = "Activate your account";
				
				$message($this->fetchEmail($case, $array));
				$address(
					$array['email'], 
					$array['first_name'].' '.$array['last_name']
				);
				
				break;
				
			}
			
			
			// send email
			if ($send()) {
				$this->ClearAddresses();
				return true;
			}
			return false;
			
		
		}
	
	
	}

	public function fetchEmail($case = null, $array = null) {
	
		if (!empty($case)) {
			
			if (!empty($array)) {			
				foreach($array as $key => $value) {
					${$key} = $value;
				}			
			}
			
			ob_start();
			require_once(EMAILS_PATH.DS.$case.".php");
			$out = ob_get_clean();
			return $this->wrapEmail($out);
		
		}
	
	}

	public function wrapEmail($content = null) {
		if (!empty($content)) {
			return "<div style=\"font-family:Arial,Verdana,Sans-serif;font-size:12px;color:#333;line-height:21px;\">{$content}</div>";
		}
	}
	



}

Upvotes: 0

Views: 172

Answers (1)

qakbar
qakbar

Reputation: 53

Just an update I have manged to get the PHPMailer ot work after I commented out the below line of code.

$this->objMailer->IsSMTP();

Not sure why but the IsSMTP(); is not letting the emails go through.

Upvotes: 1

Related Questions