Skip to content

Instantly share code, notes, and snippets.

@pascut
Forked from xeoncross/index.php
Created March 31, 2020 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascut/6a3ea3dced4d257f954b96a129c2ea31 to your computer and use it in GitHub Desktop.
Save pascut/6a3ea3dced4d257f954b96a129c2ea31 to your computer and use it in GitHub Desktop.
Tiny, SMTP client in PHP
<?php
/*
This is a very tiny proof-of-concept SMTP client. Currently it's over 320 characters (if the var names are compressed). Think you can build one smaller?
*/
ini_set('default_socket_timeout', 3);
$user = 'you@gmail.com';
$pass = '';
$host = 'ssl://smtp.gmail.com';
//$host = 'ssl://email-smtp.us-east-1.amazonaws.com'; //Amazon SES
$port = 465;
$to = 'him@gmail.com';
$from = 'you@gmail.com';
$template = "Subject: =?UTF-8?B?VGVzdCBFbWFpbA==?=\r\n"
."To: <him@gmail.com>\r\n"
."From: you@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"
."PGgxPlRlc3QgRW1haWw8L2gxPjxwPkhlbGxvIFRoZXJlITwvcD4=\r\n.";
if(smtp_mail($to, $from, $template, $user, $pass, $host, $port))
{
echo "Mail sent\n\n";
}
else
{
echo "Some error occured\n\n";
}
<?php
/**
* Connecto to an SMTP and send the given message
*/
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment