Jump to content

I need help with sending class

- - - - -

  • Please log in to reply
6 replies to this topic

#1
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Hi wrote some simple class, look like this:

<?php


class Mailer	{

	

	protected $_mails = array();

	public $file_path;

	public $error_message;

	public $errors = array();

	public $subject;

	public $p_mails;

	

	//constructor

	function __construct($file)

	{

		$this->file_path = $file;

		

		if (file_exists($this->file_path))	{

			$this->_mails = file($file);

		} else	{

			die();

		} 

	}

	

	//setter

	public function __set($name, $value)

	{

		$this->$name = $value;

	}

	

	//function returns all e-mails

	public function showAllMails()

	{

		if (count($this->_mails) = 0) {

			return false;

		} else {

                     	return $this->_mails;

		}

	}

	

	//function which add content of message

	public function insertMessage($message)

	{

		$message = htmlspecialchars($message);

		$this->message = $message;

	}

	

	//function which add subject

	public function insertSubject($subject);

	{

		$sub = htmlsepcialchars($subject);

		$this->subject = $sub;

	}

	

	//function prepares mails to send

	public function prepareMails($array)

	{

		$this->p_mails = $array;

	}

	

	public function sendMails()

	{

		for($i=0;$i<=count($this->_mails);$i++)

		{

			if (count($this->p_mails) == 0 || $this->subject == '' || $this->message == '')

			{

				return false;

			} else {

				$try = mail($this->p_mails[$i], $this->subject, $this->message);

				if(!$try)

				{

					$this->errors['mail'][] = $this->mails[$i];

				}

			}

		}

	}

	

	//this function returns mails, which does not send

	public function showDoNotSend()

	{

		return $this->errors['mails'];

	}


}


?>


I need your help because i do not know if function sends mails is capacity, because this method will send about 2000 mails in once. I`m programming just 1,5 years and I have never learned write perfect application.

Second, I write simple configure file which is common to all files in application, precisely to php files which are executing while some user is browsing my page, something like controller in php`s framework as look as : localhost/send.php localhost/add_mails.php. My problem is in speed of executing this file. It look like this:

<?php

/*

************************************

* system mailowy stworzony przez:  *

*        AdrianWierciochPHP        *

************************************

*

* Plik konfiguracyjny

*

*/


//check if configuration file exists

if (!file_exists('config.ini')) {

	die('<p>Główny plik konfiguracyjny nie istnieje! Skontaktuj się z <a mailto="wiercioch.adrian1993@gmail.com">wiercioch.adrian1993@gmail.com</a>!</p>');

}


$config = parse_ini_file('config.ini');


//definiowanie stałych

define('SMARTY_DIR', $config['smarty.dir']);

define('TEMPLATE_DIR', $config['smarty.dir.template']);

define('COMPILE_DIR', $config['smarty.dir.compile']);

define('CONFIG_DIR', $config['smarty.dir.config']);

define('CACHE_DIR', $config['smarty.dir.cache']);

define('EMAIL_ADDRESS', $config['author.mail']);

define('FILE_SET_LANG', $config['file.set.language']); /* do not need */

define('FILE_STACK_MAILS', $config['file.stack.mails']);

define('EXT_T', $config['smarty.templates.extension']);

define('SMARTY_DISPLAY_INDEX', $config['smarty.display.index']);

define('SMARTY_DISPLAY_SEND', $config['smarty.display.send']);

define('SMARTY_DISPLAy_ERROR', $config['smarty.display.error']);

define('LANGUAGE', $config['language']);

define('LIBRARY', $config['library']);

define('MARK', $config['main.mark']);


//including SMARTY class

include_once(SMARTY_DIR . 'Smarty.class.php');


//configure language of application

require_once('lib/Language.class.php');

$lang = new Language('./languages/');

$lang->loadFile(LANGUAGE) and die('File pl.php not found'); /* including language file */


//SMARTY`s class

class smartyMailer extends Smarty {

	

	//constract

	public function __construct()

	{

		

		//simple configuration of smarty

		$this->Smarty();

		$this->template_dir =	TEMPLATE_DIR;

		$this->compile_dir	=	COMPILE_DIR;

		$this->config_dir	=	CONFIG_DIR;

		$this->cache_dir	=	CACHE_DIR;

		

	}

}


/* END OF CONFIGURE.PHP */


?>


I know it`s very slow and I ask you how to create rapid configuration of my application. IT is very important for me because I must have prepared application for two days in Sunday.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hi,

Have you profiled your code to see what is slowing down your application?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Can you precisely your qustion? :p
Sorry, but I do not understand it, because i`m from poland and I learn anglish just since one year.

#4
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I thing that this application execute to many scripts.
First I wrote config.ini like above, next script configure.php parses config.ini file and defines constants and in application i use constants to take a configure. It isn`t to many?

#5
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
and I have one more question
how to use declared configuration in *.ini file? Assuming I have this file:


;conf.ini


directory.library = './lib/'

directory.smarty = './lib/smarty/'


and I want to use directory.library in second directory.smarty . Can I?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hi, I assume that is correct.

Your definitions are small, it is most likely not the portion of your code that slows it down.

Your includes may take the longest time, maybe try benchmarking your configuration script to see how long it takes, try this:
 <?php 
$timer_start = microtime(true);
/* 
************************************ 
* system mailowy stworzony przez:  * 
*        AdrianWierciochPHP        * 
************************************ 
* 
* Plik konfiguracyjny 
* 
*/ 

//check if configuration file exists 
if (!file_exists('config.ini')) { 
    die('<p>Główny plik konfiguracyjny nie istnieje! Skontaktuj się z <a mailto="wiercioch.adrian1993@gmail.com">wiercioch.adrian1993@gmail.com</a>!</p>'); 
} 

$config = parse_ini_file('config.ini'); 

//definiowanie stałych 
define('SMARTY_DIR', $config['smarty.dir']); 
define('TEMPLATE_DIR', $config['smarty.dir.template']); 
define('COMPILE_DIR', $config['smarty.dir.compile']); 
define('CONFIG_DIR', $config['smarty.dir.config']); 
define('CACHE_DIR', $config['smarty.dir.cache']); 
define('EMAIL_ADDRESS', $config['author.mail']); 
define('FILE_SET_LANG', $config['file.set.language']); /* do not need */ 
define('FILE_STACK_MAILS', $config['file.stack.mails']); 
define('EXT_T', $config['smarty.templates.extension']); 
define('SMARTY_DISPLAY_INDEX', $config['smarty.display.index']); 
define('SMARTY_DISPLAY_SEND', $config['smarty.display.send']); 
define('SMARTY_DISPLAy_ERROR', $config['smarty.display.error']); 
define('LANGUAGE', $config['language']); 
define('LIBRARY', $config['library']); 
define('MARK', $config['main.mark']); 

//including SMARTY class 
include_once(SMARTY_DIR . 'Smarty.class.php'); 

//configure language of application 
require_once('lib/Language.class.php'); 
$lang = new Language('./languages/'); 
$lang->loadFile(LANGUAGE) and die('File pl.php not found'); /* including language file */ 

//SMARTY`s class 
class smartyMailer extends Smarty { 
     
    //constract 
    public function __construct() 
    { 
         
        //simple configuration of smarty 
        $this->Smarty(); 
        $this->template_dir =    TEMPLATE_DIR; 
        $this->compile_dir    =    COMPILE_DIR; 
        $this->config_dir    =    CONFIG_DIR; 
        $this->cache_dir    =    CACHE_DIR; 
         
    } 
} 

/* END OF CONFIGURE.PHP */ 
$timer_end = microtime(true);
printf("configure.php took %0.2d seconds to complete.<br/>\n", $timer_start - $timer_end);
?>  
You can benchmark each part of your script to see the execution times.
$timer_start = microtime(true);
//your code here that you wish to benchmark
$timer_end = microtime(true);
printf("Function took %0.2d seconds to complete.<br/>\n", $timer_start - $timer_end);

Edited by Alexander, 01 January 2011 - 06:55 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hi, I had fixed the code to work proper, try it out.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users