Hi guys,

Here a new PHP class that helps you to send emails securely. This may be so helpful for those who need a contact us or newsletter forms.
This class can send emails using three different ways (via SMTP server, via qMail MTA or via sendmail command).

This differs from the ordinary php mail function. If anyone tried before to use php mail function you’ll find your message in the junk not inbox and you may wonder why this happens. It happens because the mail server did not trust the sender so it see’s your message as spam message and places it in the junk.
PHPMailer uses a secure way to send emails. Here i’ll show you the most important functions that you may use it frequently.

All You need to configure PHPMailer is to download the source files from this link

Then extract the files and copy these three files to the place you will include them

class.phpmailer.php
class.pop3.php
class.smtp.php

Then just include this class.phpmailer.php file and the configuration is done.

require_once 'class.phpmailer.php';

First you need to create and instance of the PHPMailer class as follows

$email = new PHPMailer();

To send email via “SMTP” you need to call this member function

$email->IsSMTP();

To send email via “qMail MTA” you need to call this member function

$email->IsQmail();

To send email via “sendmail command” you need to call this member function

$email->IsSendmail();

Then if you need to enable email authentication you can call this member function

$email->SMTPAuth = true;

Some mail servers will require "SSL" for secure email sending (like Google) so you can call this member function to enable "SSL"
$email->SMTPSecure = 'ssl';

For "SMPT" you need to specify the "SMPT server" so you can call this function

$email->Host = 'smtp.gmail.com';

Actually PHPMailer uses the 465 port to send emails so you can specify it using this function

$email->Port = 465;

In case if you had an error in sending email you can view this error using this function

$email->ErrorInfo;

Here i had written a function that you can use to send emails using PHPMailer all you need is to pass it some usual arguments to specify the sender and the reciever emails, body, subject and sender name


<?php 

	require_once 'class.phpmailer.php';
	
        //setting up Authentication info.
	$authMail = 'YourMailGoesHere@gmail.com';
	$authPass = 'Your Password Goes Here';
	
	define('mail' , $authMail);
	define('password' , $authPass);
	
	function sendEmail($to, $from, $from_Name, $body, $subject)
	{
		$email = new PHPMailer();
		
		$email->IsSMTP(); // enable SMTP to 
		$email->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
		$email->SMTPAuth = true;  // authentication enabled
		$email->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
		$email->Host = 'smtp.gmail.com'; //specifying SMTP mail server
		$email->Port = 465; 
		$email->Username = mail;  
		$email->Password = password;           
		$email->SetFrom(mail, $from_Name);
		$email->Subject = $subject;
		$email->Body = $body;
		$email->AddAddress(mail);
		if(!$email->Send()) { //call $email->Send() to send emails
			$error = 'Mail error: '.$email->ErrorInfo; 
			echo $error;
			
			return false;
		} else {
			$error = 'Message sent!';
			echo $error;
			
			return true;
		}
		
		
		
	}
	
	
	

?>

Hope you enjoyed 🙂

Advertisements

2 responses »

  1. mai says:

    good work, thanks a lot 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s