SMTP PHP Simple Mailer [Example on Bluehost ]

This is the best mail method you can use to send your clients emails. 

Despite many free and paid mailer scripts, this method is powerful and fast. 

In this article we will install the script on Bluehost.com hosting company, You can follow the same steps if your website hosted on bluehost, either you should find closest steps depends on your hosting control panel. 

 

Steps

A. Create new email ( we will use this email to send emails to clients )

B. Install SMTP PHP script and configure it. 

 

A. So first go to your control panel, find " Email Manager " section, for bluehost users follow this link : https://my.bluehost.com/hosting/email_manager

 Continue creating email address by choosing name, password, storage space .. etc. hit create when done. 

 

B. Install SMPT-PHP Script.

Create php file name it " SMTPconfig.php ", copy and paste this code inside : 

<?php
/**
 * PHP Simple Mail (SMTP)
 * @author Hamid Samak <This email address is being protected from spambots. You need JavaScript enabled to view it.>
 * @copyright 2016 Hamid Samak
 * @license MIT License
 */
class SimpleMail {
	// smtp host
	public $host;
	// smtp port
	public $port;
	// smtp username
	public $user;
	// smtp password
	public $pass;
	// security mode (ssl or tls)
	public $security;
	// mail subject
	public $subject;
	// message content
	public $message;
	// mail content type
	public $type = 'text/html';
	// mail encoding
	public $encoding = 'UTF-8';
	// error message
	public $error;
	// print results
	public $debug = false;
	// mail from
	private $from;
	// recipient(s)
	private $to = array();
	/**
	 * set sender
	 * @param  string $address email address
	 * @param  string $name    sender name
	 * @return void
	 */
	public function from($address, $name = null) {
		if (empty($name))
			$this->from = '<' . $address . '>';
		else
			$this->from = '"' . $name . '" <' . $address . '>';
	}
	/**
	 * set recipients
	 * @param  string $address email address
	 * @param  string $name    sender name
	 * @return void
	 */
	public function to($address, $name = null) {
		if (empty($name))
			$this->to[] = '<' . $address . '>';
		else
			$this->to[] = '"' . $name . '" <' . $address . '>';
	}
	/**
	 * send mail
	 * @return boolean
	 */
	public function send() {
		$host = $this->host;
		if ($this->security == 'ssl')
			$host = 'ssl://' . $host;
		$socket = fsockopen($host, $this->port, $errno, $errstr);
		if ($socket === false) {
			$this->error = $errno . ' ' . $errstr;
			return false;
		} else if ($this->parse_result($socket, 220) === false)
			return false;
		$commands = array(
			'EHLO ' . $this->host => 250
		);
		if ($this->security == 'tls')
			$commands = array_merge($commands, array(
				'STARTTLS' => 220,
				'EHLO  ' . $this->host => 250
			));
		$commands = array_merge($commands, array(
			'AUTH LOGIN' => 334,
			base64_encode($this->user) => 334,
			base64_encode($this->pass) => 235,
			'MAIL FROM: ' . strstr($this->from, '<') => 250,
		));
		foreach ($this->to as $to)
			$commands['RCPT TO: ' . strstr($to, '<')] = 250;
		$commands = array_merge($commands, array(
			'DATA' => 354,
			'Subject: ' . $this->subject . "\r\n" .
				'To: ' . implode(', ', $this->to) . "\r\n" .
				'From: ' . $this->from . "\r\n" .
				'Content-Type: ' . $this->type . "\r\n" .
				'Content-Encoding: ' . $this->encoding . "\r\n\r\n" .
				$this->message => -1,
			'.' => 250,
			'QUIT' => 0
		));
		foreach ($commands as $command => $code) {
			fwrite($socket, $command . "\r\n");
			if ($code > -1 && $this->parse_result($socket, $code) === false) {
				$this->error .= ' (' . $command . ')';
				return false;
			}
			if ($command == 'STARTTLS' && stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT) === false) {
				$this->error .= 'Unable to start TLS encryption. (' . $command . ')';
				return false;
			}
		}
		fclose($socket);
		return true;
	}
	/**
	 * parse request result and check result with expected code
	 * @param  resource $socket connection
	 * @param  integer  $code   expected code
	 * @return boolean
	 */
	private function parse_result($socket, $code) {
		$result = '';
		while (substr($result, 3, 1) != ' ')
			$result = fgets($socket, 256);
		if ($this->debug === true)
			echo $result . '<br>' . "\n";
		if (empty($code) || substr($result, 0, 3) == $code)
			return true;
		$this->error = $result;
		return false;
	}
}

$mail = new SimpleMail();
$mail->host = '127.0.0.1';
$mail->user = 'This email address is being protected from spambots. You need JavaScript enabled to view it.';
$mail->pass = "YourEmailPasswordHere";
$mail->port = 26; 
$mail->security = ''; 

?>

 

In the above code you should edit those settings only : 

$mail->host = '127.0.0.1';

The SMTP host you want to use, in this example we use 127.0.0.1 cause we want to login to our created email on step A.

$mail->user = 'This email address is being protected from spambots. You need JavaScript enabled to view it.';

Your created email address in step A.

$mail->pass = "YourEmailPasswordHere";

Your email password created in step A.

$mail->port = 26; SMTP posrt number, 26 is the port number for Bluehost, read: https://my.bluehost.com/cgi/help/email-application-setup

$mail->security = ''; read: https://my.bluehost.com/cgi/help/email-application-setup

 

SSL or TLS , it works if you left it blank for none SSL settings use google to find your hosting SMTP Settings

 

Now in your PHP script, where you want to send emails for your clients, copy and paste this code : 

include('SMTPconfig.php');

$mail->subject = 'Mail Subject';
$mail->message = "Mail Message";
$mail->from('This email address is being protected from spambots. You need JavaScript enabled to view it.', 'NoReply');
$mail->to("client Email", "Client Name");
if ($mail->send())
echo "Mail Sent";
else echo $mail->error;

 

The above code is clear, edit it with your own settings. 

 

If you found this articel is useful, share it with your friends

 

Leave a comment