PHP Class for sending Email and preventing Email Spam
Posted by admin
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 | Comments (0) | October 03, 2009 at 03:26 pm
Leave a Reply
Latest News (2)
iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.
Posted on October 02, 2009
On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".
continue reading
Launching of the updated iWebProvider.com
Posted on October 02, 2009
Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.
continue reading
Wise Words
"It's not about how many you have learned but how much you have utilized and mastered what you have learned."
- by: Mark Lester

0 Responses
No Records Yet.