Skip to content

Tareq Hasan

Posted on

Sending mail with Gmail's SMTP server with fsockopen

Today I was trying to find a way to send mail from my localhost (xampp) with Gmail’s SMTP server. I found some code in popular and also fastest forum engine - punBB.

I got the codes and did some cleanup for working fine as an external script. Here are the codes -

<?php

//this functin processes the server return codes and generates errors if needed
function server_parse($socket, $expected_response)
{
	$server_response = '';
	while (substr($server_response, 3, 1) != ' ')
	{
		if (!($server_response = fgets($socket, 256)))
			echo 'Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__;
	}

	if (!(substr($server_response, 0, 3) == $expected_response))
		echo 'Unable to send e-mail. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__;
}

//
// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
// -------> This message is from punBB developer
//
function smtp_mail($to, $subject, $message, $headers = '')
{
	$recipients = explode(',', $to);
	$user = '<your mail id>';
	$pass = '<your password>';
	$smtp_host = 'ssl://smtp.gmail.com';
	$smtp_port = 465;

	if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
		echo "Could not connect to smtp host '$smtp_host' ($errno) ($errstr)", __FILE__, __LINE__;

	server_parse($socket, '220');

	fwrite($socket, 'EHLO '.$smtp_host."\r\n");
	server_parse($socket, '250');

	fwrite($socket, 'AUTH LOGIN'."\r\n");
	server_parse($socket, '334');

	fwrite($socket, base64_encode($user)."\r\n");
	server_parse($socket, '334');

	fwrite($socket, base64_encode($pass)."\r\n");
	server_parse($socket, '235');

	fwrite($socket, 'MAIL FROM: <example@gmail.com>'."\r\n");
	server_parse($socket, '250');

	foreach ($recipients as $email)
	{
		fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
		server_parse($socket, '250');
	}

	fwrite($socket, 'DATA'."\r\n");
	server_parse($socket, '354');

	fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");

	fwrite($socket, '.'."\r\n");
	server_parse($socket, '250');

	fwrite($socket, 'QUIT'."\r\n");
	fclose($socket);

	return true;
}

// Send the mail
if(smtp_mail('example@gmail.com', 'Hi! Test Mail', 'This is a test mail from xampp with fsockopen()'))
{
	echo "Mail sent";
}
else
{
	echo "Some error occured";
}
?>

13 thoughts on “Sending mail with Gmail's SMTP server with fsockopen”

  1. Thanks for sharing this. Just what i was looking for. I cant get mail function working on my web host provider (spam filters), so i will try this method. I have tried via Telnet, but gmail - smtp disconnected me after a few seconds :)

  2. Amazing code, thank you! I'm using CodeIgniter and its Email class wasn't able to use Gmail's SMTP, but your code works beautifully.

  3. Dear,

    Its working. But there is a small issue i.e. i have set

    $user = 'Gmail User ID';
    and
    fwrite($socket, 'MAIL FROM: '."\r\n");

    But when i run this code, it emails but from email address is my $user not My other Email Address. Is it possible that the from Email Address is anything different from $user which i mention??? Waiting for your reply.

  4. Greate pieces. Keep writing such kind of information on your
    blog. Im really impressed by it.
    Hey there, You've performed a fantastic job. I'll definitely digg it and in my view suggest to my
    friends. I'm confident they'll be benefited from this site.

  5. Great job !
    It works fine for me except my email still in junk folder :/
    Anyway thank you for sharing !