I took a mail function from the open source punbb forum software.
Here it is:
function smtp_mail($to, $subject, $message, $headers, $username, $password, $host, $use_ssl){
global $forum_config;
$recipients = explode(',', $to);
// Sanitize the message
$message = str_replace("
.", "
..", $message);
$message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
// Are we using port 25 or a custom port?
if (strpos($host, ':') !== false){
list($smtp_host, $smtp_port) = explode(':', $host);
}else{
$smtp_host = $host;
$smtp_port = 25;
}
if ($use_ssl == '1'){
$smtp_host = 'ssl://'.$smtp_host;
}
if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15))){
error('Could not connect to smtp host "'.$host.'" ('.$errno.') ('.$errstr.').', __FILE__, __LINE__);
}
server_parse($socket, '220');
if ($username != '' && $password != ''){
fwrite($socket, 'EHLO '.$smtp_host."
");
server_parse($socket, '250');
fwrite($socket, 'AUTH LOGIN'."
");
server_parse($socket, '334');
fwrite($socket, base64_encode($username)."
");
server_parse($socket, '334');
fwrite($socket, base64_encode($password)."
");
server_parse($socket, '235');
}else{
fwrite($socket, 'HELO '.$smtp_host."
");
server_parse($socket, '250');
}
fwrite($socket, 'MAIL FROM: <'.$username.'>'."
");
server_parse($socket, '250');
foreach ($recipients as $email){
fwrite($socket, 'RCPT TO: <'.$email.'>'."
");
server_parse($socket, '250');
}
fwrite($socket, 'DATA'."
");
server_parse($socket, '354');
fwrite($socket, 'Subject: '.$subject."
".'To: <'.implode('>, <', $recipients).'>'."
".$headers."
".$message."
");
fwrite($socket, '.'."
");
server_parse($socket, '250');
fwrite($socket, 'QUIT'."
");
fclose($socket);
return true;
}
function server_parse($socket, $expected_response){
$server_response = '';
while (substr($server_response, 3, 1) != ' '){
if (!($server_response = fgets($socket, 256))){
error('Couldn\'t get mail server response codes.<br />Please contact the forum administrator.', __FILE__, __LINE__);
}
}
if (!(substr($server_response, 0, 3) == $expected_response)){
error('Unable to send e-mail.<br />Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
}
}
function error($m){
die($m);
}
It works most of the time, but when sending to any gmail addresses the mail bounces with the following error code (modifications removing ip addresses, email addresses, etc):
This message was created automatically by mail delivery software.
A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:
someone@gmail.com
SMTP error from remote mail server after end of data:
host gmail-smtp-in.l.google.com [173.194.64.26]:
550-5.7.1 [1.1.1.1 11] Our system has detected that this message is
550-5.7.1 not RFC 2822 compliant. To reduce the amount of spam sent to Gmail,
550-5.7.1 this message has been blocked. Please review
550 5.7.1 RFC 2822 specifications for more information. sm4si11387798obb.202 - gsmtp
------ This is a copy of the message, including all the headers. ------
Return-path: <someone@terratekkit.org>
Received: from [1.1.1.1] (port=50336 helo=ssl://mail.example.com)
by mail.example.com with esmtpsa (UNKNOWN:DHE-RSA-AES256-GCM-SHA384:256)
(Exim 4.80.1)
(envelope-from <someone@example.com>)
id 1WPQMJ-0001mk-Q4
for someone@gmail.com; Mon, 17 Mar 2014 00:45:11 -0500
Subject: test email
To: <someone@gmail.com>
this is a test
I do not know what the error is, I have attempted to look at the mentioned specification, but it is just a bunch of useless text that makes no sense to me :/.