Created
March 8, 2012 17:31
-
-
Save xeoncross/2002233 to your computer and use it in GitHub Desktop.
Tiny, SMTP client in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 = '[email protected]'; | |
$pass = ''; | |
$host = 'ssl://smtp.gmail.com'; | |
//$host = 'ssl://email-smtp.us-east-1.amazonaws.com'; //Amazon SES | |
$port = 465; | |
$to = '[email protected]'; | |
$from = '[email protected]'; | |
$template = "Subject: =?UTF-8?B?VGVzdCBFbWFpbA==?=\r\n" | |
."To: <[email protected]>\r\n" | |
."From: [email protected]\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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Really neat code golf here, but do not use this in production.
- Requires pre-quoted mailboxes
- No dot stuffing on message content
- Possible infinite loop on the
while
line
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks! very easy to understand. (ahm, except the foreach loop)
ini_set('default_socket_timeout', 3);
, the 3 should actually be quoted like'3'
, else PHP7 will complain about incompatible types (string vs int) in php7'sstrict_mode
- and it is actually supposed to be a string even in PHP5, its just that PHP5 silently convert it (as will php7 when strict_mode is disabled)also.. this won't work by default on gmail anymore, see https://security.googleblog.com/2014/04/new-security-measures-will-affect-older.html for info.
and here's something i threw together to debug why it wasn't working on gmail...: