• Web 2.0 platform and Programming Language

    We are promoting Web 2.0 platform in our template design, xHTML in our scripting and PHP programming language in our server side programming. So try our services and be satisfied with the outcome.

  • HTML/xHTML Standard

    This site passed and uses the W3C XHTML 1.0 Transitional standard, CSS level 2.1 driven, Javascript and with a latest trend of Table-less xHTML scripting by not using <table> tag in making our script.

  • Website History

    This website was founded last October 18, 2007, fully bought the domain February 2008 and has been continually updating. We are located at heart of The City of Golden Friendship Cagayan de Oro the City.

PHP Class for sending Email and preventing Email Spam

I have created a simple PHP class that would send email and with the proper emai header format it would not be a spam email.

The following are condition to make email message not to appear as Spam emails:

1. Neader To, From, Reply-To, Cc and Bcc emails must have an existing/running domain. for example email address info@iwebprovider.com, iwebprovider.com domain must still be exist/running domain or website.

2. Inserting standard headers like MIME-Version, Content-type, To and From.

3. Subject must not have always the same content. Sample if you are sending 100 email notification they must have atleast unique subject. You could create unique subject by inserting the name of the person/company you are suppose to send your email, sample subject “Notification message to John from iWeb”, “Notification message to Kate from iWeb” and etc. Sample repeated subject is “Notification message from iWeb”.

interface intEmail {

	public function send();
	public function is_email( $email );
	public function send_mail( $send_to, $subject, $message, $headers );
	public function extract_send_to_email( $send_to_email );

}

class clsEmail implements intEmail {

	public $send = FALSE;
	public $type = NULL;
	public $charset = 'utf-8'; //iso-8859-1

	public $send_to = NULL;
	public $subject = NULL;
	public $headers = NULL;
	public $message = NULL;

	public $send_to_many = array();
	public $email_from = NULL;
	public $name_from = NULL;
	public $reply_to = NULL;

	public $cc = NULL;
	public $bcc = NULL;

	public function send(){

		$send_to = $this->send_to;
		$subject = $this->subject;
		$message = $this->message;
		if($send_to != '' && $subject != '' && $message != '' && $this->name_from != '' && $this->email_from != ''){
			if($this->type == 'html'){

				$send_to_email = $this->extract_send_to_email( $this->send_to );

				// To send HTML mail, the Content-type header must be set
				$headers  = 'MIME-Version: 1.0'."\r\n";
				$headers .= 'Content-type: text/html; charset='.$this->charset.''."\r\n";
				// Additional h	eaders
				$headers .= 'To: '.$send_to_email.''."\r\n";
				$headers .= 'From: '.$this->name_from.' <'.$this->email_from.'>'."\r\n";
				if($this->reply_to != ''){
					$headers .= 'Reply-To: '.$this->reply_to.''."\r\n";
				}
				if($this->cc != ''){
					$headers .= 'Cc: '.$this->cc.''."\r\n";
				}
				if($this->bcc != ''){
					$headers .= 'Bcc: '.$this->bcc.''."\r\n";
				}
			} else {
				$headers .= 'From: '.$this->name_from.' <'.$this->email_from.'>'."\r\n";
				if($this->reply_to != ''){
					$headers .= 'Reply-To: '.$this->reply_to.''."\r\n";
				}
				$headers .= 'X-Mailer: PHP/'.phpversion();
			}

			if($send_to_email != FALSE){

				$this->send = $this->send_mail( $send_to, $subject, $message, $headers );

				return $this->send;
			} else {
				//Error if no email to or wrong email to format
				return FALSE;
			}
		} else {
			//Error variable send_to, subject, message, name_from and email_from are blank.
			return FALSE;
		}
	}

	public function extract_send_to_email( $send_to_email ){

		$email_array = explode(',',str_replace(' ','',$send_to_email));
		$check_email = array();

		$email_name = '';
		$email_string = '';

		if(count($email_array) > 0){
			$cnt = 1;
			foreach($email_array as $email_value){
				if($this->is_email( $email_value ) == TRUE){

					$comma = $cnt > 1 ? ',' : '';

					$email_name_array = explode('@',$email_value);
					$email_name = $email_name_array[0];

					$email_string .= $comma.$email_name.' <'.$email_value.'>';

					$check_email[] = 1;

					$cnt++;
				} else {
					$check_email[] = 0;
				}
			}

			if(in_array(0, $check_email) == FALSE){

				return $email_string;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}

	public function send_mail( $send_to, $subject, $message, $headers ){

		$mail = @mail( trim($send_to), strip_tags($subject), stripslashes($message), $headers );

		return $mail;
	}

	//Validate if proper email format
	public function is_email( $email ){
		if(preg_match("/^[^@]*@[^@]*\.[^@]*$/", $email)){
			return $email;
		} else {
			return FALSE;
		}
	}
} ?>
  //Application of this class
   $send_to = 'to_email_1@domain.com,to_email_2@domain.com'
   $message = '

Hello,

This is a sample email

Thanks,

   ';

   $objEmail = new clsEmail();

   $objEmail->Email->type = 'html';
   $objEmail->Email->send_to = $send_to;
   $objEmail->Email->subject = 'Sample email';
   $objEmail->Email->name_from = 'nameofsender';
   $objEmail->Email->email_from = 'emailfrom@domain.com';
   $objEmail->Email->reply_to = 'emailfrom@domain.com';
   $objEmail->Email->cc = 'email_cc@domain.com';
   $objEmail->Email->bcc = 'email_bcc@domain.com';
   $objEmail->Email->message = $message;
   $objEmail->Email->send();
Back to list 7 Comments Jan 19, 11 at 09:41 am

7 Responses to PHP Class for sending Email and preventing Email Spam

  1. best products says:

    I enjoy all…

    i’m contemplating about commencing my quite own blog….

  2. Watch 11-11-11 Full Movie says:

    2011…

    I think this is among the most significant information for me. And i am glad reading your article. But want to remark on few general things, The web site style is great, the articles is really excellent : D. Good job, cheers…

  3. Asian Tiger Mosquito says:

    2011…

    I do not even know how I ended up here, but I thought this post was good. I don’t know who you are but definitely you’re going to a famous blogger if you aren’t already ;) Cheers!…

  4. Accounting Basics says:

    2011…

    Valuable information. Lucky me I found your site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it….

  5. seattle organic seo says:

    2011…

    hey there and thank you for your info – I have certainly picked up something new from right here. I did however expertise several technical points using this website, since I experienced to reload the website a lot of times previous to I could get it t…

  6. Accounting Basics says:

    2011…

    My brother suggested I might like this website. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!…

  7. Accounting Basics says:

    2011…

    Great line up. We will be linking to this great article on our site. Keep up the good writing….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

5 × seven =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

My Blogs

  • Configuring CodeIgniter for website with HTTPS protocol view more...
    Posted: Jan 05, 12 at 11:27 am
  • Keyword “Cagayan de Oro PHP Programmer” iWebProvider.com is Number 1 at Google Search view more...
    Posted: Feb 15, 11 at 05:54 pm
  • PHP get root URL and root Directory path view more...
    Posted: Feb 15, 11 at 02:47 am
  • PHP Object Oriented Programming (OOP) using Model View Controller (MVC) approach view more...
    Posted: Feb 12, 11 at 01:56 pm
  • Launching of the new 2011 iWebProvider.com Template Design view more...
    Posted: Feb 03, 11 at 08:17 pm

Most Popular Blogs

  • Search Engine Friendly URL to convert .php and replace with .html file extension using .htaccess view more...
    Posted: Jan 19, 11 at 09:44 am
  • Simple PHP Secure Session Login System view more...
    Posted: Jan 19, 11 at 09:43 am
  • PHP Function to Limit String output or display view more...
    Posted: Jan 19, 11 at 09:39 am
  • PHP Class for sending Email and preventing Email Spam view more...
    Posted: Jan 19, 11 at 09:41 am

Wise Words

"Hard work surpasses genius"

by: Mark

"It’s not about how many you have learned but how much you have utilized and mastered what you have learned."

by: Mark

"The function of good software is to make the complex appear to be simple."

by: Grady Booch

Latest Comments

Digg... While checking out DIGG today I found this...

by: Trade Engine Oil

2011... Wow! This can be one particular of the most helpful blogs We have ever arrive across on thi...

by: Sifisdndsk

2011... great post, very informative. I wonder why the other experts of this sector do not notice t...

by: Sifisdndsk

Keep in Touch

If you would like to find out more about us, just email us at inquiry[AT]iwebprovider.com