/** * Replacement or substitute for PHP's mail command */ function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '') { global $config, $user; // Fix any bare linefeeds in the message to make it RFC821 Compliant. $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); if ($headers != '') { if (is_array($headers)) { $headers = sizeof($headers) > 1 ? join("\n", $headers) : $headers[0]; } $headers = chop($headers); // Make sure there are no bare linefeeds in the headers $headers = preg_replace('#(?<!\\r)\\n#si', "\r\n", $headers); // Ok this is rather confusing all things considered, // but we have to grab bcc and cc headers and treat them differently // Something we really didn't take into consideration originally $header_array = explode("\r\n", $headers); $headers = ''; foreach ($header_array as $header) { if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0) { $header = ''; } $headers .= $header != '' ? $header . "\r\n" : ''; } $headers = chop($headers); } if (trim($subject) == '') { $err_msg = isset($user->lang['NO_EMAIL_SUBJECT']) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified'; return false; } if (trim($message) == '') { $err_msg = isset($user->lang['NO_EMAIL_MESSAGE']) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank'; return false; } $mail_rcpt = $mail_to = $mail_cc = array(); // Build correct addresses for RCPT TO command and the client side display (TO, CC) if (isset($addresses['to']) && sizeof($addresses['to'])) { foreach ($addresses['to'] as $which_ary) { $mail_to[] = $which_ary['name'] != '' ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; $mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>'; } } if (isset($addresses['bcc']) && sizeof($addresses['bcc'])) { foreach ($addresses['bcc'] as $which_ary) { $mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>'; } } if (isset($addresses['cc']) && sizeof($addresses['cc'])) { foreach ($addresses['cc'] as $which_ary) { $mail_cc[] = $which_ary['name'] != '' ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>'; $mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>'; } } $smtp = new smtp_class(); $errno = 0; $errstr = ''; $smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']); // Ok we have error checked as much as we can to this point let's get on it already. ob_start(); $smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20); $error_contents = ob_get_clean(); if (!$smtp->socket) { if ($errstr) { $errstr = utf8_convert_message($errstr); } $err_msg = isset($user->lang['NO_CONNECT_TO_SMTP_HOST']) ? sprintf($user->lang['NO_CONNECT_TO_SMTP_HOST'], $errno, $errstr) : "Could not connect to smtp host : {$errno} : {$errstr}"; $err_msg .= $error_contents ? '<br /><br />' . htmlspecialchars($error_contents) : ''; return false; } // Wait for reply if ($err_msg = $smtp->server_parse('220', __LINE__)) { $smtp->close_session($err_msg); return false; } // Let me in. This function handles the complete authentication process if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method'])) { $smtp->close_session($err_msg); return false; } // From this point onward most server response codes should be 250 // Specify who the mail is from.... $smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>'); if ($err_msg = $smtp->server_parse('250', __LINE__)) { $smtp->close_session($err_msg); return false; } // Specify each user to send to and build to header. $to_header = implode(', ', $mail_to); $cc_header = implode(', ', $mail_cc); // Now tell the MTA to send the Message to the following people... [TO, BCC, CC] $rcpt = false; foreach ($mail_rcpt as $type => $mail_to_addresses) { foreach ($mail_to_addresses as $mail_to_address) { // Add an additional bit of error checking to the To field. if (preg_match('#[^ ]+\\@[^ ]+#', $mail_to_address)) { $smtp->server_send("RCPT TO:{$mail_to_address}"); if ($err_msg = $smtp->server_parse('250', __LINE__)) { // We continue... if users are not resolved we do not care if ($smtp->numeric_response_code != 550) { $smtp->close_session($err_msg); return false; } } else { $rcpt = true; } } } } // We try to send messages even if a few people do not seem to have valid email addresses, but if no one has, we have to exit here. if (!$rcpt) { $user->session_begin(); $err_msg .= '<br /><br />'; $err_msg .= isset($user->lang['INVALID_EMAIL_LOG']) ? sprintf($user->lang['INVALID_EMAIL_LOG'], htmlspecialchars($mail_to_address)) : '<strong>' . htmlspecialchars($mail_to_address) . '</strong> possibly an invalid email address?'; $smtp->close_session($err_msg); return false; } // Ok now we tell the server we are ready to start sending data $smtp->server_send('DATA'); // This is the last response code we look for until the end of the message. if ($err_msg = $smtp->server_parse('354', __LINE__)) { $smtp->close_session($err_msg); return false; } // Send the Subject Line... $smtp->server_send("Subject: {$subject}"); // Now the To Header. $to_header = $to_header == '' ? 'undisclosed-recipients:;' : $to_header; $smtp->server_send("To: {$to_header}"); // Now the CC Header. if ($cc_header != '') { $smtp->server_send("CC: {$cc_header}"); } // Now any custom headers.... $smtp->server_send("{$headers}\r\n"); // Ok now we are ready for the message... $smtp->server_send($message); // Ok the all the ingredients are mixed in let's cook this puppy... $smtp->server_send('.'); if ($err_msg = $smtp->server_parse('250', __LINE__)) { $smtp->close_session($err_msg); return false; } // Now tell the server we are done and close the socket... $smtp->server_send('QUIT'); $smtp->close_session($err_msg); return true; }