Skip to content

Instantly share code, notes, and snippets.

@Jiri-Mihal
Created April 10, 2017 20:32
Show Gist options
  • Select an option

  • Save Jiri-Mihal/5ad97d1cd391ff381f4861ea2e1942ee to your computer and use it in GitHub Desktop.

Select an option

Save Jiri-Mihal/5ad97d1cd391ff381f4861ea2e1942ee to your computer and use it in GitHub Desktop.
Simple PHP example of sending email via SMTP server.
<?php
// Configure SMTP
$host = 'smtp.yoursmtphost.tld';
$port = 25;
$username = '[email protected]';
$password = 'senderPassword';
$from = '[email protected]';
$name = 'Sender Name';
// Message
$to = '[email protected]';
$subject = 'Your subject comes here';
$message = 'Your message comes here.';
// Send command to stream
function send($fp, $msg)
{
fwrite($fp, $msg . "\r\n");
$timeout = microtime(1) + 0.100;
stream_set_timeout($fp, 0, 0.100);
$answer = '';
while (!feof($fp) && microtime(1) <= $timeout) {
$answer .= fgets($fp, 2048);
}
return $answer;
}
// Open stream with SMTP server
$fp = stream_socket_client($host . ':' . $port, $errno, $errstr);
if (!$fp) {
// Can't create stream...
echo "$errstr ($errno)<br />\n";
} else {
// Stream has been successfully created...
echo 'Connected<br/>';
// Follow steps required for Envelope...
// Read more here: https://utcc.utoronto.ca/usg/technotes/smtp-intro.html
// Say HELO to SMTP server
$answer = send($fp, 'HELO localhost');
echo nl2br($answer);
// Authenticate user with SMTP server
// Example of Auth with CRAM-MD5
// All auth types are: PLAIN, LOGIN, NTLM, CRAM-MD5, XOAUTH2
// Tell to SMTP server that we will authenticate user with CRAM-MD5
$answer = send($fp, 'AUTH CRAM-MD5');
echo nl2br($answer);
// Send back auth credentials to SMTP server
$challenge = base64_decode(substr($answer, 4));
$response = base64_encode($username . ' ' . hash_hmac('md5', $challenge, $password));
$answer = send($fp, $response);
echo nl2br($answer);
// Tell to SMTP server the sender email address
$answer = send($fp, 'MAIL FROM: <' . $from . '>');
echo nl2br($answer);
// Tell to SMTP server the recipient email address
$answer = send($fp, 'RCPT TO: <' . $to . '>');
echo nl2br($answer);
// Tell to SMTP server that we will send email message...
$answer = send($fp, 'DATA');
echo nl2br($answer);
// Prepare headers and message
$name = '=?UTF-8?B?' . base64_encode($name) . '?=';
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$message = base64_encode(iconv(mb_detect_encoding($message, mb_detect_order(), true), 'UTF-8', $message));
$headers = [];
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-Type: text/html; charset=utf-8';
$headers[] = 'Content-Transfer-Encoding: base64';
$headers[] = 'From: ' . $name . ' <' . $from . '>';
$headers[] = 'To: <' . $to . '>';
$headers[] = 'Subject: ' . $subject;
// Send headers with message to SMTP server
$answer = send($fp, implode("\r\n", $headers) . "\r\n" . $message);
echo nl2br($answer);
// Message is complete, so tell it to SMTP server
$answer = send($fp, '.');
echo nl2br($answer);
// That's all, tell to SMTP, that we finish communication
$answer = send($fp, 'QUIT');
echo nl2br($answer);
// Close stream
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment