6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
25 var $vars, $msg, $extra_headers, $replyto, $from, $subject;
26 var $addresses = array();
28 var $mail_priority = MAIL_NORMAL_PRIORITY;
29 var $use_queue = true;
32 var $tpl_msg = array();
38 function messenger($use_queue = true)
42 $this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
45 // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
46 $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
47 $this->eol = (!$this->eol) ? "\n" : $this->eol;
51 * Resets all the data (address, template file, etc etc) to default
55 $this->addresses = $this->extra_headers = array();
56 $this->vars = $this->msg = $this->replyto = $this->from = '';
57 $this->mail_priority = MAIL_NORMAL_PRIORITY;
61 * Sets an email address to send to
63 function to($address, $realname = '')
72 $pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
74 $this->addresses['to'][$pos]['email'] = trim($address);
76 // If empty sendmail_path on windows, PHP changes the to line
77 if (!$config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\')
79 $this->addresses['to'][$pos]['name'] = '';
83 $this->addresses['to'][$pos]['name'] = trim($realname);
88 * Sets an cc address to send to
90 function cc($address, $realname = '')
97 $pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
98 $this->addresses['cc'][$pos]['email'] = trim($address);
99 $this->addresses['cc'][$pos]['name'] = trim($realname);
103 * Sets an bcc address to send to
105 function bcc($address, $realname = '')
112 $pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
113 $this->addresses['bcc'][$pos]['email'] = trim($address);
114 $this->addresses['bcc'][$pos]['name'] = trim($realname);
118 * Sets a im contact to send to
120 function im($address, $realname = '')
122 // IM-Addresses could be empty
128 $pos = isset($this->addresses['im']) ? sizeof($this->addresses['im']) : 0;
129 $this->addresses['im'][$pos]['uid'] = trim($address);
130 $this->addresses['im'][$pos]['name'] = trim($realname);
134 * Set the reply to address
136 function replyto($address)
138 $this->replyto = trim($address);
142 * Set the from address
144 function from($address)
146 $this->from = trim($address);
150 * set up subject for mail
152 function subject($subject = '')
154 $this->subject = trim($subject);
158 * set up extra mail headers
160 function headers($headers)
162 $this->extra_headers[] = trim($headers);
166 * Adds X-AntiAbuse headers
168 * @param array $config Configuration array
169 * @param user $user A user object
173 function anti_abuse_headers($config, $user)
175 $this->headers('X-AntiAbuse: Board servername - ' . mail_encode($config['server_name']));
176 $this->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
177 $this->headers('X-AntiAbuse: Username - ' . mail_encode($user->data['username']));
178 $this->headers('X-AntiAbuse: User IP - ' . $user->ip);
182 * Set the email priority
184 function set_mail_priority($priority = MAIL_NORMAL_PRIORITY)
186 $this->mail_priority = $priority;
190 * Set email template to use
192 function template($template_file, $template_lang = '', $template_path = '')
194 global $config, $phpbb_root_path, $user;
196 if (!trim($template_file))
198 trigger_error('No template file for emailing set.', E_USER_ERROR);
201 if (!trim($template_lang))
203 // fall back to board default language if the user's language is
204 // missing $template_file. If this does not exist either,
205 // $tpl->set_custom_template will do a trigger_error
206 $template_lang = basename($config['default_lang']);
209 // tpl_msg now holds a template object we can use to parse the template file
210 if (!isset($this->tpl_msg[$template_lang . $template_file]))
212 $this->tpl_msg[$template_lang . $template_file] = new template();
213 $tpl = &$this->tpl_msg[$template_lang . $template_file];
215 $fallback_template_path = false;
219 $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
220 $template_path .= $template_lang . '/email';
222 // we can only specify default language fallback when the path is not a custom one for which we
223 // do not know the default language alternative
224 if ($template_lang !== basename($config['default_lang']))
226 $fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/';
227 $fallback_template_path .= basename($config['default_lang']) . '/email';
231 $tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path);
233 $tpl->set_filenames(array(
234 'body' => $template_file . '.txt',
238 $this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file];
239 $this->vars = &$this->tpl_obj->_rootref;
246 * assign variables to email template
248 function assign_vars($vars)
250 if (!is_object($this->tpl_obj))
255 $this->tpl_obj->assign_vars($vars);
258 function assign_block_vars($blockname, $vars)
260 if (!is_object($this->tpl_obj))
265 $this->tpl_obj->assign_block_vars($blockname, $vars);
269 * Send the mail out to the recipients set previously in var $this->addresses
271 function send($method = NOTIFY_EMAIL, $break = false)
273 global $config, $user;
275 // We add some standard variables we always use, no need to specify them always
276 if (!isset($this->vars['U_BOARD']))
278 $this->assign_vars(array(
279 'U_BOARD' => generate_board_url(),
283 if (!isset($this->vars['EMAIL_SIG']))
285 $this->assign_vars(array(
286 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])),
290 if (!isset($this->vars['SITENAME']))
292 $this->assign_vars(array(
293 'SITENAME' => htmlspecialchars_decode($config['sitename']),
297 // Parse message through template
298 $this->msg = trim($this->tpl_obj->assign_display('body'));
300 // Because we use \n for newlines in the body message we need to fix line encoding errors for those admins who uploaded email template files in the wrong encoding
301 $this->msg = str_replace("\r\n", "\n", $this->msg);
303 // We now try and pull a subject from the email body ... if it exists,
304 // do this here because the subject may contain a variable
307 if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
309 $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
310 $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
314 $this->subject = (($this->subject != '') ? $this->subject : $user->lang['NO_EMAIL_SUBJECT']);
319 $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
330 $result = $this->msg_email();
334 $result = $this->msg_jabber();
338 $result = $this->msg_email();
348 * Add error message to log
350 function error($type, $msg)
352 global $user, $phpEx, $phpbb_root_path, $config;
354 // Session doesn't exist, create it
355 if (!isset($user->session_id) || $user->session_id === '')
357 $user->session_begin();
360 $calling_page = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
366 $message = '<strong>EMAIL/' . (($config['smtp_delivery']) ? 'SMTP' : 'PHP/' . $config['email_function_name'] . '()') . '</strong>';
370 $message = "<strong>$type</strong>";
374 $message .= '<br /><em>' . htmlspecialchars($calling_page) . '</em><br /><br />' . $msg . '<br />';
375 add_log('critical', 'LOG_ERROR_' . $type, $message);
381 function save_queue()
385 if ($config['email_package_size'] && $this->use_queue && !empty($this->queue))
387 $this->queue->save();
393 * Return email header
395 function build_header($to, $cc, $bcc)
399 // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
402 $headers[] = 'From: ' . $this->from;
406 $headers[] = 'Cc: ' . $cc;
411 $headers[] = 'Bcc: ' . $bcc;
414 $headers[] = 'Reply-To: ' . $this->replyto;
415 $headers[] = 'Return-Path: <' . $config['board_email'] . '>';
416 $headers[] = 'Sender: <' . $config['board_email'] . '>';
417 $headers[] = 'MIME-Version: 1.0';
418 $headers[] = 'Message-ID: <' . md5(unique_id(time())) . '@' . $config['server_name'] . '>';
419 $headers[] = 'Date: ' . date('r', time());
420 $headers[] = 'Content-Type: text/plain; charset=UTF-8'; // format=flowed
421 $headers[] = 'Content-Transfer-Encoding: 8bit'; // 7bit
423 $headers[] = 'X-Priority: ' . $this->mail_priority;
424 $headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High'));
425 $headers[] = 'X-Mailer: phpBB3';
426 $headers[] = 'X-MimeOLE: phpBB3';
427 $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
429 if (sizeof($this->extra_headers))
431 $headers = array_merge($headers, $this->extra_headers);
442 global $config, $user;
444 if (empty($config['email_enable']))
449 // Addresses to send to?
450 if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc'])))
452 // Send was successful. ;)
457 if ($config['email_package_size'] && $this->use_queue)
459 if (empty($this->queue))
461 $this->queue = new queue();
462 $this->queue->init('email', $config['email_package_size']);
467 if (empty($this->replyto))
469 $this->replyto = '<' . $config['board_contact'] . '>';
472 if (empty($this->from))
474 $this->from = '<' . $config['board_contact'] . '>';
477 $encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol;
479 // Build to, cc and bcc strings
480 $to = $cc = $bcc = '';
481 foreach ($this->addresses as $type => $address_ary)
488 foreach ($address_ary as $which_ary)
490 $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']);
495 $headers = $this->build_header($to, $cc, $bcc);
500 $mail_to = ($to == '') ? 'undisclosed-recipients:;' : $to;
503 if ($config['smtp_delivery'])
505 $result = smtpmail($this->addresses, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $err_msg, $headers);
509 $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg);
514 $this->error('EMAIL', $err_msg);
520 $this->queue->put('email', array(
522 'addresses' => $this->addresses,
523 'subject' => $this->subject,
525 'headers' => $headers)
533 * Send jabber message out
535 function msg_jabber()
537 global $config, $db, $user, $phpbb_root_path, $phpEx;
539 if (empty($config['jab_enable']) || empty($config['jab_host']) || empty($config['jab_username']) || empty($config['jab_password']))
544 if (empty($this->addresses['im']))
546 // Send was successful. ;)
551 if ($config['jab_package_size'] && $this->use_queue)
553 if (empty($this->queue))
555 $this->queue = new queue();
556 $this->queue->init('jabber', $config['jab_package_size']);
561 $addresses = array();
562 foreach ($this->addresses['im'] as $type => $uid_ary)
564 $addresses[] = $uid_ary['uid'];
566 $addresses = array_unique($addresses);
570 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
571 $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
573 if (!$this->jabber->connect())
575 $this->error('JABBER', $user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->jabber->get_log());
579 if (!$this->jabber->login())
581 $this->error('JABBER', $user->lang['ERR_JAB_AUTH'] . '<br />' . $this->jabber->get_log());
585 foreach ($addresses as $address)
587 $this->jabber->send_message($address, $this->msg, $this->subject);
590 $this->jabber->disconnect();
594 $this->queue->put('jabber', array(
595 'addresses' => $addresses,
596 'subject' => $this->subject,
606 * handling email and jabber queue
612 var $queue_data = array();
613 var $package_size = 0;
614 var $cache_file = '';
622 global $phpEx, $phpbb_root_path;
624 $this->data = array();
625 $this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx";
627 // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac)
628 $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL;
629 $this->eol = (!$this->eol) ? "\n" : $this->eol;
633 * Init a queue object
635 function init($object, $package_size)
637 $this->data[$object] = array();
638 $this->data[$object]['package_size'] = $package_size;
639 $this->data[$object]['data'] = array();
643 * Put object in queue
645 function put($object, $scope)
647 $this->data[$object]['data'][] = $scope;
651 * Obtains exclusive lock on queue cache file.
652 * Returns resource representing the lock
656 // For systems that can't have two processes opening
657 // one file for writing simultaneously
658 if (file_exists($this->cache_file . '.lock'))
667 $lock_fp = @fopen($this->cache_file . '.lock', $mode);
673 // Two processes may attempt to create lock file at the same time.
674 // Have the losing process try opening the lock file again for reading
675 // on the assumption that the winning process created it
677 $lock_fp = @fopen($this->cache_file . '.lock', $mode);
681 // Only need to set mode when the lock file is written
682 @chmod($this->cache_file . '.lock', 0666);
688 @flock($lock_fp, LOCK_EX);
695 * Releases lock on queue cache file, using resource obtained from lock()
697 function unlock($lock_fp)
699 // lock() will return null if opening lock file, and thus locking, failed.
700 // Accept null values here so that client code does not need to check them
703 @flock($lock_fp, LOCK_UN);
714 global $db, $config, $phpEx, $phpbb_root_path, $user;
716 $lock_fp = $this->lock();
718 set_config('last_queue_run', time(), true);
720 if (!file_exists($this->cache_file) || filemtime($this->cache_file) > time() - $config['queue_interval'])
722 $this->unlock($lock_fp);
726 include($this->cache_file);
728 foreach ($this->queue_data as $object => $data_ary)
732 if (!isset($data_ary['package_size']))
734 $data_ary['package_size'] = 0;
737 $package_size = $data_ary['package_size'];
738 $num_items = (!$package_size || sizeof($data_ary['data']) < $package_size) ? sizeof($data_ary['data']) : $package_size;
741 * This code is commented out because it causes problems on some web hosts.
742 * The core problem is rather restrictive email sending limits.
743 * This code is nly useful if you have no such restrictions from the
744 * web host and the package size setting is wrong.
746 // If the amount of emails to be sent is way more than package_size than we need to increase it to prevent backlogs...
747 if (sizeof($data_ary['data']) > $package_size * 2.5)
749 $num_items = sizeof($data_ary['data']);
756 // Delete the email queued objects if mailing is disabled
757 if (!$config['email_enable'])
759 unset($this->queue_data['email']);
765 if (!$config['jab_enable'])
767 unset($this->queue_data['jabber']);
771 include_once($phpbb_root_path . 'includes/functions_jabber.' . $phpEx);
772 $this->jabber = new jabber($config['jab_host'], $config['jab_port'], $config['jab_username'], $config['jab_password'], $config['jab_use_ssl']);
774 if (!$this->jabber->connect())
776 messenger::error('JABBER', $user->lang['ERR_JAB_CONNECT']);
780 if (!$this->jabber->login())
782 messenger::error('JABBER', $user->lang['ERR_JAB_AUTH']);
789 $this->unlock($lock_fp);
793 for ($i = 0; $i < $num_items; $i++)
795 // Make variables available...
796 extract(array_shift($this->queue_data[$object]['data']));
802 $to = (!$to) ? 'undisclosed-recipients:;' : $to;
804 if ($config['smtp_delivery'])
806 $result = smtpmail($addresses, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $err_msg, $headers);
810 $result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg);
815 messenger::error('EMAIL', $err_msg);
821 foreach ($addresses as $address)
823 if ($this->jabber->send_message($address, $msg, $subject) === false)
825 messenger::error('JABBER', $this->jabber->get_log());
833 // No more data for this object? Unset it
834 if (!sizeof($this->queue_data[$object]['data']))
836 unset($this->queue_data[$object]);
839 // Post-object processing
843 // Hang about a couple of secs to ensure the messages are
844 // handled, then disconnect
845 $this->jabber->disconnect();
850 if (!sizeof($this->queue_data))
852 @unlink($this->cache_file);
856 if ($fp = @fopen($this->cache_file, 'wb'))
858 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");
861 phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
865 $this->unlock($lock_fp);
873 if (!sizeof($this->data))
878 $lock_fp = $this->lock();
880 if (file_exists($this->cache_file))
882 include($this->cache_file);
884 foreach ($this->queue_data as $object => $data_ary)
886 if (isset($this->data[$object]) && sizeof($this->data[$object]))
888 $this->data[$object]['data'] = array_merge($data_ary['data'], $this->data[$object]['data']);
892 $this->data[$object]['data'] = $data_ary['data'];
897 if ($fp = @fopen($this->cache_file, 'w'))
899 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");
902 phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
905 $this->unlock($lock_fp);
910 * Replacement or substitute for PHP's mail command
912 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)
914 global $config, $user;
916 // Fix any bare linefeeds in the message to make it RFC821 Compliant.
917 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
919 if ($headers !== false)
921 if (!is_array($headers))
923 // Make sure there are no bare linefeeds in the headers
924 $headers = preg_replace('#(?<!\r)\n#si', "\n", $headers);
925 $headers = explode("\n", $headers);
928 // Ok this is rather confusing all things considered,
929 // but we have to grab bcc and cc headers and treat them differently
930 // Something we really didn't take into consideration originally
931 $headers_used = array();
933 foreach ($headers as $header)
935 if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
939 $headers_used[] = trim($header);
942 $headers = chop(implode("\r\n", $headers_used));
945 if (trim($subject) == '')
947 $err_msg = (isset($user->lang['NO_EMAIL_SUBJECT'])) ? $user->lang['NO_EMAIL_SUBJECT'] : 'No email subject specified';
951 if (trim($message) == '')
953 $err_msg = (isset($user->lang['NO_EMAIL_MESSAGE'])) ? $user->lang['NO_EMAIL_MESSAGE'] : 'Email message was blank';
957 $mail_rcpt = $mail_to = $mail_cc = array();
959 // Build correct addresses for RCPT TO command and the client side display (TO, CC)
960 if (isset($addresses['to']) && sizeof($addresses['to']))
962 foreach ($addresses['to'] as $which_ary)
964 $mail_to[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
965 $mail_rcpt['to'][] = '<' . trim($which_ary['email']) . '>';
969 if (isset($addresses['bcc']) && sizeof($addresses['bcc']))
971 foreach ($addresses['bcc'] as $which_ary)
973 $mail_rcpt['bcc'][] = '<' . trim($which_ary['email']) . '>';
977 if (isset($addresses['cc']) && sizeof($addresses['cc']))
979 foreach ($addresses['cc'] as $which_ary)
981 $mail_cc[] = ($which_ary['name'] != '') ? mail_encode(trim($which_ary['name'])) . ' <' . trim($which_ary['email']) . '>' : '<' . trim($which_ary['email']) . '>';
982 $mail_rcpt['cc'][] = '<' . trim($which_ary['email']) . '>';
986 $smtp = new smtp_class();
991 $smtp->add_backtrace('Connecting to ' . $config['smtp_host'] . ':' . $config['smtp_port']);
993 // Ok we have error checked as much as we can to this point let's get on it already.
994 if (!class_exists('phpbb_error_collector'))
996 global $phpbb_root_path, $phpEx;
997 include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
999 $collector = new phpbb_error_collector;
1000 $collector->install();
1001 $smtp->socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 20);
1002 $collector->uninstall();
1003 $error_contents = $collector->format_errors();
1009 $errstr = utf8_convert_message($errstr);
1012 $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";
1013 $err_msg .= ($error_contents) ? '<br /><br />' . htmlspecialchars($error_contents) : '';
1018 if ($err_msg = $smtp->server_parse('220', __LINE__))
1020 $smtp->close_session($err_msg);
1024 // Let me in. This function handles the complete authentication process
1025 if ($err_msg = $smtp->log_into_server($config['smtp_host'], $config['smtp_username'], $config['smtp_password'], $config['smtp_auth_method']))
1027 $smtp->close_session($err_msg);
1031 // From this point onward most server response codes should be 250
1032 // Specify who the mail is from....
1033 $smtp->server_send('MAIL FROM:<' . $config['board_email'] . '>');
1034 if ($err_msg = $smtp->server_parse('250', __LINE__))
1036 $smtp->close_session($err_msg);
1040 // Specify each user to send to and build to header.
1041 $to_header = implode(', ', $mail_to);
1042 $cc_header = implode(', ', $mail_cc);
1044 // Now tell the MTA to send the Message to the following people... [TO, BCC, CC]
1046 foreach ($mail_rcpt as $type => $mail_to_addresses)
1048 foreach ($mail_to_addresses as $mail_to_address)
1050 // Add an additional bit of error checking to the To field.
1051 if (preg_match('#[^ ]+\@[^ ]+#', $mail_to_address))
1053 $smtp->server_send("RCPT TO:$mail_to_address");
1054 if ($err_msg = $smtp->server_parse('250', __LINE__))
1056 // We continue... if users are not resolved we do not care
1057 if ($smtp->numeric_response_code != 550)
1059 $smtp->close_session($err_msg);
1071 // 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.
1074 $user->session_begin();
1075 $err_msg .= '<br /><br />';
1076 $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?';
1077 $smtp->close_session($err_msg);
1081 // Ok now we tell the server we are ready to start sending data
1082 $smtp->server_send('DATA');
1084 // This is the last response code we look for until the end of the message.
1085 if ($err_msg = $smtp->server_parse('354', __LINE__))
1087 $smtp->close_session($err_msg);
1091 // Send the Subject Line...
1092 $smtp->server_send("Subject: $subject");
1094 // Now the To Header.
1095 $to_header = ($to_header == '') ? 'undisclosed-recipients:;' : $to_header;
1096 $smtp->server_send("To: $to_header");
1098 // Now the CC Header.
1099 if ($cc_header != '')
1101 $smtp->server_send("CC: $cc_header");
1104 // Now any custom headers....
1105 if ($headers !== false)
1107 $smtp->server_send("$headers\r\n");
1110 // Ok now we are ready for the message...
1111 $smtp->server_send($message);
1113 // Ok the all the ingredients are mixed in let's cook this puppy...
1114 $smtp->server_send('.');
1115 if ($err_msg = $smtp->server_parse('250', __LINE__))
1117 $smtp->close_session($err_msg);
1121 // Now tell the server we are done and close the socket...
1122 $smtp->server_send('QUIT');
1123 $smtp->close_session($err_msg);
1130 * Auth Mechanisms originally taken from the AUTH Modules found within the PHP Extension and Application Repository (PEAR)
1131 * See docs/AUTHORS for more details
1136 var $server_response = '';
1138 var $responses = array();
1139 var $commands = array();
1140 var $numeric_response_code = 0;
1142 var $backtrace = false;
1143 var $backtrace_log = array();
1145 function smtp_class()
1147 // Always create a backtrace for admins to identify SMTP problems
1148 $this->backtrace = true;
1149 $this->backtrace_log = array();
1153 * Add backtrace message for debugging
1155 function add_backtrace($message)
1157 if ($this->backtrace)
1159 $this->backtrace_log[] = utf8_htmlspecialchars($message);
1164 * Send command to smtp server
1166 function server_send($command, $private_info = false)
1168 fputs($this->socket, $command . "\r\n");
1170 (!$private_info) ? $this->add_backtrace("# $command") : $this->add_backtrace('# Omitting sensitive information');
1172 // We could put additional code here
1176 * We use the line to give the support people an indication at which command the error occurred
1178 function server_parse($response, $line)
1182 $this->server_response = '';
1183 $this->responses = array();
1184 $this->numeric_response_code = 0;
1186 while (substr($this->server_response, 3, 1) != ' ')
1188 if (!($this->server_response = fgets($this->socket, 256)))
1190 return (isset($user->lang['NO_EMAIL_RESPONSE_CODE'])) ? $user->lang['NO_EMAIL_RESPONSE_CODE'] : 'Could not get mail server response codes';
1192 $this->responses[] = substr(rtrim($this->server_response), 4);
1193 $this->numeric_response_code = (int) substr($this->server_response, 0, 3);
1195 $this->add_backtrace("LINE: $line <- {$this->server_response}");
1198 if (!(substr($this->server_response, 0, 3) == $response))
1200 $this->numeric_response_code = (int) substr($this->server_response, 0, 3);
1201 return (isset($user->lang['EMAIL_SMTP_ERROR_RESPONSE'])) ? sprintf($user->lang['EMAIL_SMTP_ERROR_RESPONSE'], $line, $this->server_response) : "Ran into problems sending Mail at <strong>Line $line</strong>. Response: $this->server_response";
1210 function close_session(&$err_msg)
1212 fclose($this->socket);
1214 if ($this->backtrace)
1216 $message = '<h1>Backtrace</h1><p>' . implode('<br />', $this->backtrace_log) . '</p>';
1217 $err_msg .= $message;
1222 * Log into server and get possible auth codes if neccessary
1224 function log_into_server($hostname, $username, $password, $default_auth_method)
1230 // Here we try to determine the *real* hostname (reverse DNS entry preferrably)
1231 $local_host = $user->host;
1233 if (function_exists('php_uname'))
1235 $local_host = php_uname('n');
1237 // Able to resolve name to IP
1238 if (($addr = @gethostbyname($local_host)) !== $local_host)
1240 // Able to resolve IP back to name
1241 if (($name = @gethostbyaddr($addr)) !== $addr)
1243 $local_host = $name;
1248 // If we are authenticating through pop-before-smtp, we
1249 // have to login ones before we get authenticated
1250 // NOTE: on some configurations the time between an update of the auth database takes so
1251 // long that the first email send does not work. This is not a biggie on a live board (only
1252 // the install mail will most likely fail) - but on a dynamic ip connection this might produce
1253 // severe problems and is not fixable!
1254 if ($default_auth_method == 'POP-BEFORE-SMTP' && $username && $password)
1261 $this->server_send("QUIT");
1262 fclose($this->socket);
1264 $result = $this->pop_before_smtp($hostname, $username, $password);
1265 $username = $password = $default_auth_method = '';
1267 // We need to close the previous session, else the server is not
1268 // able to get our ip for matching...
1269 if (!$this->socket = @fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 10))
1273 $errstr = utf8_convert_message($errstr);
1276 $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";
1281 if ($err_msg = $this->server_parse('220', __LINE__))
1283 $this->close_session($err_msg);
1289 $this->server_send("EHLO {$local_host}");
1290 if ($err_msg = $this->server_parse('250', __LINE__))
1292 // a 503 response code means that we're already authenticated
1293 if ($this->numeric_response_code == 503)
1298 // If EHLO fails, we try HELO
1299 $this->server_send("HELO {$local_host}");
1300 if ($err_msg = $this->server_parse('250', __LINE__))
1302 return ($this->numeric_response_code == 503) ? false : $err_msg;
1306 foreach ($this->responses as $response)
1308 $response = explode(' ', $response);
1309 $response_code = $response[0];
1310 unset($response[0]);
1311 $this->commands[$response_code] = implode(' ', $response);
1314 // If we are not authenticated yet, something might be wrong if no username and passwd passed
1315 if (!$username || !$password)
1320 if (!isset($this->commands['AUTH']))
1322 return (isset($user->lang['SMTP_NO_AUTH_SUPPORT'])) ? $user->lang['SMTP_NO_AUTH_SUPPORT'] : 'SMTP server does not support authentication';
1325 // Get best authentication method
1326 $available_methods = explode(' ', $this->commands['AUTH']);
1328 // Define the auth ordering if the default auth method was not found
1329 $auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5');
1332 if (in_array($default_auth_method, $available_methods))
1334 $method = $default_auth_method;
1338 foreach ($auth_methods as $_method)
1340 if (in_array($_method, $available_methods))
1350 return (isset($user->lang['NO_SUPPORTED_AUTH_METHODS'])) ? $user->lang['NO_SUPPORTED_AUTH_METHODS'] : 'No supported authentication methods';
1353 $method = strtolower(str_replace('-', '_', $method));
1354 return $this->$method($username, $password);
1358 * Pop before smtp authentication
1360 function pop_before_smtp($hostname, $username, $password)
1364 if (!$this->socket = @fsockopen($hostname, 110, $errno, $errstr, 10))
1368 $errstr = utf8_convert_message($errstr);
1371 return (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";
1374 $this->server_send("USER $username", true);
1375 if ($err_msg = $this->server_parse('+OK', __LINE__))
1380 $this->server_send("PASS $password", true);
1381 if ($err_msg = $this->server_parse('+OK', __LINE__))
1386 $this->server_send('QUIT');
1387 fclose($this->socket);
1393 * Plain authentication method
1395 function plain($username, $password)
1397 $this->server_send('AUTH PLAIN');
1398 if ($err_msg = $this->server_parse('334', __LINE__))
1400 return ($this->numeric_response_code == 503) ? false : $err_msg;
1403 $base64_method_plain = base64_encode("\0" . $username . "\0" . $password);
1404 $this->server_send($base64_method_plain, true);
1405 if ($err_msg = $this->server_parse('235', __LINE__))
1414 * Login authentication method
1416 function login($username, $password)
1418 $this->server_send('AUTH LOGIN');
1419 if ($err_msg = $this->server_parse('334', __LINE__))
1421 return ($this->numeric_response_code == 503) ? false : $err_msg;
1424 $this->server_send(base64_encode($username), true);
1425 if ($err_msg = $this->server_parse('334', __LINE__))
1430 $this->server_send(base64_encode($password), true);
1431 if ($err_msg = $this->server_parse('235', __LINE__))
1440 * cram_md5 authentication method
1442 function cram_md5($username, $password)
1444 $this->server_send('AUTH CRAM-MD5');
1445 if ($err_msg = $this->server_parse('334', __LINE__))
1447 return ($this->numeric_response_code == 503) ? false : $err_msg;
1450 $md5_challenge = base64_decode($this->responses[0]);
1451 $password = (strlen($password) > 64) ? pack('H32', md5($password)) : ((strlen($password) < 64) ? str_pad($password, 64, chr(0)) : $password);
1452 $md5_digest = md5((substr($password, 0, 64) ^ str_repeat(chr(0x5C), 64)) . (pack('H32', md5((substr($password, 0, 64) ^ str_repeat(chr(0x36), 64)) . $md5_challenge))));
1454 $base64_method_cram_md5 = base64_encode($username . ' ' . $md5_digest);
1456 $this->server_send($base64_method_cram_md5, true);
1457 if ($err_msg = $this->server_parse('235', __LINE__))
1466 * digest_md5 authentication method
1467 * A real pain in the ***
1469 function digest_md5($username, $password)
1471 global $config, $user;
1473 $this->server_send('AUTH DIGEST-MD5');
1474 if ($err_msg = $this->server_parse('334', __LINE__))
1476 return ($this->numeric_response_code == 503) ? false : $err_msg;
1479 $md5_challenge = base64_decode($this->responses[0]);
1481 // Parse the md5 challenge - from AUTH_SASL (PEAR)
1483 while (preg_match('/^([a-z-]+)=("[^"]+(?<!\\\)"|[^,]+)/i', $md5_challenge, $matches))
1485 // Ignore these as per rfc2831
1486 if ($matches[1] == 'opaque' || $matches[1] == 'domain')
1488 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
1492 // Allowed multiple "realm" and "auth-param"
1493 if (!empty($tokens[$matches[1]]) && ($matches[1] == 'realm' || $matches[1] == 'auth-param'))
1495 if (is_array($tokens[$matches[1]]))
1497 $tokens[$matches[1]][] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
1501 $tokens[$matches[1]] = array($tokens[$matches[1]], preg_replace('/^"(.*)"$/', '\\1', $matches[2]));
1504 else if (!empty($tokens[$matches[1]])) // Any other multiple instance = failure
1511 $tokens[$matches[1]] = preg_replace('/^"(.*)"$/', '\\1', $matches[2]);
1514 // Remove the just parsed directive from the challenge
1515 $md5_challenge = substr($md5_challenge, strlen($matches[0]) + 1);
1519 if (empty($tokens['realm']))
1521 $tokens['realm'] = (function_exists('php_uname')) ? php_uname('n') : $user->host;
1525 if (empty($tokens['maxbuf']))
1527 $tokens['maxbuf'] = 65536;
1530 // Required: nonce, algorithm
1531 if (empty($tokens['nonce']) || empty($tokens['algorithm']))
1535 $md5_challenge = $tokens;
1537 if (!empty($md5_challenge))
1540 for ($i = 0; $i < 32; $i++)
1542 $str .= chr(mt_rand(0, 255));
1544 $cnonce = base64_encode($str);
1546 $digest_uri = 'smtp/' . $config['smtp_host'];
1548 $auth_1 = sprintf('%s:%s:%s', pack('H32', md5(sprintf('%s:%s:%s', $username, $md5_challenge['realm'], $password))), $md5_challenge['nonce'], $cnonce);
1549 $auth_2 = 'AUTHENTICATE:' . $digest_uri;
1550 $response_value = md5(sprintf('%s:%s:00000001:%s:auth:%s', md5($auth_1), $md5_challenge['nonce'], $cnonce, md5($auth_2)));
1552 $input_string = sprintf('username="%s",realm="%s",nonce="%s",cnonce="%s",nc="00000001",qop=auth,digest-uri="%s",response=%s,%d', $username, $md5_challenge['realm'], $md5_challenge['nonce'], $cnonce, $digest_uri, $response_value, $md5_challenge['maxbuf']);
1556 return (isset($user->lang['INVALID_DIGEST_CHALLENGE'])) ? $user->lang['INVALID_DIGEST_CHALLENGE'] : 'Invalid digest challenge';
1559 $base64_method_digest_md5 = base64_encode($input_string);
1560 $this->server_send($base64_method_digest_md5, true);
1561 if ($err_msg = $this->server_parse('334', __LINE__))
1566 $this->server_send(' ');
1567 if ($err_msg = $this->server_parse('235', __LINE__))
1577 * Encodes the given string for proper display in UTF-8.
1579 * This version is using base64 encoded data. The downside of this
1580 * is if the mail client does not understand this encoding the user
1581 * is basically doomed with an unreadable subject.
1583 * Please note that this version fully supports RFC 2045 section 6.8.
1585 * @param string $eol End of line we are using (optional to be backwards compatible)
1587 function mail_encode($str, $eol = "\r\n")
1589 // define start delimimter, end delimiter and spacer
1590 $start = "=?UTF-8?B?";
1592 $delimiter = "$eol ";
1594 // Maximum length is 75. $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $delimiter . $end)!!!
1596 $encoded_str = base64_encode($str);
1598 // If encoded string meets the limits, we just return with the correct data.
1599 if (strlen($encoded_str) <= $split_length)
1601 return $start . $encoded_str . $end;
1604 // If there is only ASCII data, we just return what we want, correctly splitting the lines.
1605 if (strlen($str) === utf8_strlen($str))
1607 return $start . implode($end . $delimiter . $start, str_split($encoded_str, $split_length)) . $end;
1610 // UTF-8 data, compose encoded lines
1611 $array = utf8_str_split($str);
1614 while (sizeof($array))
1618 while (sizeof($array) && intval((strlen($text . $array[0]) + 2) / 3) << 2 <= $split_length)
1620 $text .= array_shift($array);
1623 $str .= $start . base64_encode($text) . $end . $delimiter;
1626 return substr($str, 0, -strlen($delimiter));
1630 * Wrapper for sending out emails with the PHP's mail function
1632 function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg)
1634 global $config, $phpbb_root_path, $phpEx;
1636 // We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used...
1637 // Reference: http://bugs.php.net/bug.php?id=15841
1638 $headers = implode($eol, $headers);
1640 if (!class_exists('phpbb_error_collector'))
1642 include($phpbb_root_path . 'includes/error_collector.' . $phpEx);
1645 $collector = new phpbb_error_collector;
1646 $collector->install();
1648 // On some PHP Versions mail() *may* fail if there are newlines within the subject.
1649 // Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8.
1650 // Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space (Use '' as parameter to mail_encode() results in SPACE used)
1651 $result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers);
1653 $collector->uninstall();
1654 $err_msg = $collector->format_errors();