<?php
/**
 * Connecto to an SMTP and send the given message
 */

ini_set('default_socket_timeout', 3);
/*
$user = 'jothivaradha@kvcodes.com';
$pass = 'Honeykutty';
$host = 'ssl://sg3plcpnl0027.prod.sin3.secureserver.net';
//$host = 'ssl://email-smtp.us-east-1.amazonaws.com'; //Amazon SES
$port = 465;*/

$user = 'kvcodes@gmail.com';
$pass = 'kvcodes@123';
$host = 'ssl://mail.gmail.com';
//$host = 'ssl://email-smtp.us-east-1.amazonaws.com'; //Amazon SES
$port = 465;

$to = 'kvvaradha@gmail.com';
$from = 'kvcodes@gmail.com';
$template = "Subject: Test email\r\n"
."To: <kvvaradha@gmail.com>\r\n"
."From: kvcodes@gmail.com\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: text/html; charset=utf-8\r\n"
."Content-Transfer-Encoding: base64\r\n\r\n"
."Hello text body content=\r\n.
Test words";


if(smtp_mail($to, $from, $template, $user, $pass, $host, $port))
{
	echo "Mail sent\n\n";
}
else
{
	echo "Some error occured\n\n";
}


function smtp_mail($to, $from, $message, $user, $pass, $host, $port){
	
	if ($h = fsockopen($host, $port)){
		$data = array(
			0,
			"EHLO $host",
			'AUTH LOGIN',
			base64_encode($user),
			base64_encode($pass),
			"MAIL FROM: <$from>",
			"RCPT TO: <$to>",
			'DATA',
			$message
		);

		foreach($data as $c){
			$c && fwrite($h, "$c\r\n");
			while(substr(fgets($h, 256), 3, 1) != ' '){}
		}
		
		fwrite($h, "QUIT\r\n");

		return fclose($h);
	}
}