mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2026-11-20 23:43:26 +03:00
Merge pull request #958 from arymaynart/patch-2
Refactor smtp_mail for better SMTP support
This commit is contained in:
1 file changed
+95
-64
+95
-64
@@ -1311,84 +1311,115 @@ function enable_socket_crypto($fh)
|
||||
* @param string $password (optional) - Password
|
||||
* @param string $body (optional, but recommended) - mail body
|
||||
* @return bool - true on success, otherwise false
|
||||
* TODO: Replace this with something decent like PEAR::Mail or Zend_Mail.
|
||||
* I did something that is working with 465 and 587. The previous one doesnt work at all
|
||||
* Sorry fot that. Had to do a litle tweak do work with broadcast-message.php
|
||||
* Sorry for the bad english :D
|
||||
*/
|
||||
function smtp_mail($to, $from, $data, $password = "", $body = "")
|
||||
{
|
||||
function smtp_mail($to, $from, $subject_or_data, $body = null) {
|
||||
global $CONF;
|
||||
|
||||
$smtpd_server = $CONF['smtp_server'];
|
||||
$smtpd_port = $CONF['smtp_port'];
|
||||
$smtpd_type = $CONF['smtp_type'];
|
||||
|
||||
$smtp_server = php_uname('n');
|
||||
if (!empty($CONF['smtp_client'])) {
|
||||
$smtp_server = $CONF['smtp_client'];
|
||||
}
|
||||
$errno = 0;
|
||||
$errstr = "0";
|
||||
$server = $CONF['smtp_server'];
|
||||
$port = (int)$CONF['smtp_port'];
|
||||
$timeout = 30;
|
||||
|
||||
if ($body != "") {
|
||||
$maildata =
|
||||
"To: " . $to . "\n"
|
||||
. "From: " . $from . "\n"
|
||||
. "Subject: " . encode_header($data) . "\n"
|
||||
. "MIME-Version: 1.0\n"
|
||||
. "Date: " . date('r') . "\n"
|
||||
. "Content-Type: text/plain; charset=utf-8\n"
|
||||
. "Content-Transfer-Encoding: 8bit\n"
|
||||
. "\n"
|
||||
. $body;
|
||||
$helo = !empty($CONF['smtp_client'])
|
||||
? $CONF['smtp_client']
|
||||
: php_uname('n');
|
||||
|
||||
$username = $CONF['smtp_username'] ?? '';
|
||||
$password = $CONF['admin_smtp_password'] ?? '';
|
||||
$type = $CONF['smtp_type'] ?? 'starttls';
|
||||
|
||||
// Building of SMTP payload
|
||||
if ($body === null) {
|
||||
// RAW MOD for Broadcast (broadcast-message.php)
|
||||
$maildata = $subject_or_data;
|
||||
} else {
|
||||
$maildata = $data;
|
||||
// NORMAL MOD (sendmail.php)
|
||||
$maildata =
|
||||
"To: $to\r\n" .
|
||||
"From: $from\r\n" .
|
||||
"Subject: " . encode_header($subject_or_data) . "\r\n" .
|
||||
"MIME-Version: 1.0\r\n" .
|
||||
"Date: " . date('r') . "\r\n" .
|
||||
"Content-Type: text/plain; charset=utf-8\r\n" .
|
||||
"Content-Transfer-Encoding: 8bit\r\n\r\n" .
|
||||
$body;
|
||||
}
|
||||
|
||||
$fh = @fsockopen($smtpd_server, $smtpd_port, $errno, $errstr, $timeout);
|
||||
|
||||
if (!$fh) {
|
||||
error_log("fsockopen failed - errno: $errno - errstr: $errstr");
|
||||
return false;
|
||||
// Connection
|
||||
if ($type === 'tls' || $type === 'ssl') {
|
||||
$socket = stream_socket_client(
|
||||
"ssl://{$server}:{$port}",
|
||||
$errno,
|
||||
$errstr,
|
||||
$timeout,
|
||||
STREAM_CLIENT_CONNECT
|
||||
);
|
||||
} else {
|
||||
if ($smtpd_type === "tls") {
|
||||
enable_socket_crypto($fh);
|
||||
$socket = fsockopen($server, $port, $errno, $errstr, $timeout);
|
||||
}
|
||||
|
||||
if (!$socket) {
|
||||
error_log("smtp_mail(): socket error $errno - $errstr");
|
||||
return false;
|
||||
}
|
||||
|
||||
smtp_expect($socket, 220);
|
||||
|
||||
fputs($socket, "EHLO $helo\r\n");
|
||||
smtp_expect($socket, 250);
|
||||
|
||||
if ($type === 'starttls') {
|
||||
fputs($socket, "STARTTLS\r\n");
|
||||
smtp_expect($socket, 220);
|
||||
stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
|
||||
fputs($socket, "EHLO $helo\r\n");
|
||||
smtp_expect($socket, 250);
|
||||
}
|
||||
|
||||
// AUTH
|
||||
fputs($socket, "AUTH LOGIN\r\n");
|
||||
smtp_expect($socket, 334);
|
||||
fputs($socket, base64_encode($username) . "\r\n");
|
||||
smtp_expect($socket, 334);
|
||||
fputs($socket, base64_encode($password) . "\r\n");
|
||||
smtp_expect($socket, 235);
|
||||
|
||||
// SMTP FLOW
|
||||
fputs($socket, "MAIL FROM:<$from>\r\n");
|
||||
smtp_expect($socket, 250);
|
||||
fputs($socket, "RCPT TO:<$to>\r\n");
|
||||
smtp_expect($socket, 250);
|
||||
fputs($socket, "DATA\r\n");
|
||||
smtp_expect($socket, 354);
|
||||
|
||||
fputs($socket, $maildata . "\r\n.\r\n");
|
||||
smtp_expect($socket, 250);
|
||||
|
||||
fputs($socket, "QUIT\r\n");
|
||||
fclose($socket);
|
||||
|
||||
return true;
|
||||
}
|
||||
//end
|
||||
// auxiliary function
|
||||
function smtp_expect($socket, $code) {
|
||||
$response = '';
|
||||
while ($line = fgets($socket, 515)) {
|
||||
$response .= $line;
|
||||
if (preg_match('/^\d{3}\s/', $line)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
smtp_get_response($fh);
|
||||
|
||||
if ($smtpd_type === "starttls") {
|
||||
fputs($fh, "STARTTLS\r\n");
|
||||
smtp_get_response($fh);
|
||||
enable_socket_crypto($fh);
|
||||
}
|
||||
|
||||
fputs($fh, "EHLO $smtp_server\r\n");
|
||||
smtp_get_response($fh);
|
||||
|
||||
if (!empty($password)) {
|
||||
fputs($fh, "AUTH LOGIN\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, base64_encode($from) . "\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, base64_encode($password) . "\r\n");
|
||||
smtp_get_response($fh);
|
||||
}
|
||||
|
||||
fputs($fh, "MAIL FROM:<$from>\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, "RCPT TO:<$to>\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, "DATA\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, "$maildata\r\n.\r\n");
|
||||
smtp_get_response($fh);
|
||||
fputs($fh, "QUIT\r\n");
|
||||
smtp_get_response($fh);
|
||||
fclose($fh);
|
||||
if ((int)substr($response, 0, 3) !== $code) {
|
||||
error_log("smtp_mail(): especting $code, received: " . trim($response));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//end commit
|
||||
/**
|
||||
* smtp_get_admin_email
|
||||
* Action: Get configured email address or current user if nothing configured
|
||||
|
||||
Reference in new issue
Block a user